import { normalizePostTypeConfig } from "./normalizers";

export const applyNonPublicPostTypeRestrictions = (settings, postType) => {
    if (!postType || postType.public !== false) {
        return settings;
    }

    return {
        ...settings,
        archives_enabled: false,
        taxonomy_archives_enabled: false,
        expand_post_types: [],
        propagation: {
            ...settings.propagation,
            first_publish_single_fanout_enabled: false,
        },
    };
};

export const buildPostTypesPayload = (postTypeSettings, postTypeCatalogBySlug) => {
    const out = {};

    Object.entries(postTypeSettings || {}).forEach(([postType, config]) => {
        const normalizedConfig = applyNonPublicPostTypeRestrictions(
            normalizePostTypeConfig(config),
            postTypeCatalogBySlug[postType],
        );

        out[postType] = normalizedConfig;
    });

    return out;
};
