"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEditorSchemas = getEditorSchemas;
/* eslint-disable no-case-declarations */
const util_1 = require("../../../util");
const filterSchemaToUiSchema_1 = require("./filterSchemaToUiSchema");
const getCardEditorSchemas_1 = require("./getCardEditorSchemas");
const addDynamicSlugValidation_1 = require("./addDynamicSlugValidation");
const SiteSchema_1 = require("../../../sites/_internal/SiteSchema");
const DiscussionSchema_1 = require("../../../discussions/_internal/DiscussionSchema");
const ChannelSchema_1 = require("../../../channels/_internal/ChannelSchema");
const ProjectSchema_1 = require("../../../projects/_internal/ProjectSchema");
const InitiativeSchema_1 = require("../../../initiatives/_internal/InitiativeSchema");
const PageSchema_1 = require("../../../pages/_internal/PageSchema");
const ContentSchema_1 = require("../../../content/_internal/ContentSchema");
const TemplateSchema_1 = require("../../../templates/_internal/TemplateSchema");
const GroupSchema_1 = require("../../../groups/_internal/GroupSchema");
const InitiativeTemplateSchema_1 = require("../../../initiative-templates/_internal/InitiativeTemplateSchema");
const buildSiteUiSchemaEdit = require("../../../sites/_internal/SiteUiSchemaEdit");
const buildSiteUiSchemaCreate = require("../../../sites/_internal/SiteUiSchemaCreate");
const buildSiteUiSchemaFollowers = require("../../../sites/_internal/SiteUiSchemaFollowers");
const buildSiteUiSchemaSettings = require("../../../sites/_internal/SiteUiSchemaSettings");
const buildSiteUiSchemaAssistant = require("../../../sites/_internal/SiteUiSchemaAssistant");
const buildSiteUiSchemaDiscussionSettings = require("../../../core/schemas/internal/discussions/EntityUiSchemaDiscussionsSettings");
const buildDiscussionUiSchemaEdit = require("../../../discussions/_internal/DiscussionUiSchemaEdit");
const buildDiscussionUiSchemaCreate = require("../../../discussions/_internal/DiscussionUiSchemaCreate");
const buildDiscussionUiSchemaSettings = require("../../../discussions/_internal/DiscussionUiSchemaSettings");
const buildDiscussionUiSchemaDiscussionSettings = require("../../../core/schemas/internal/discussions/EntityUiSchemaDiscussionsSettings");
const buildChannelUiSchemaEdit = require("../../../channels/_internal/ChannelUiSchemaEdit");
const buildChannelUiSchemaCreate = require("../../../channels/_internal/ChannelUiSchemaCreate");
const buildProjectUiSchemaEdit = require("../../../projects/_internal/ProjectUiSchemaEdit");
const buildProjectUiSchemaCreate = require("../../../projects/_internal/ProjectUiSchemaCreate");
const buildProjectUiSchemaCreate2 = require("../../../projects/_internal/ProjectUiSchemaCreate2");
const buildProjectUiSchemaMetrics = require("./metrics/ProjectUiSchemaMetrics");
const buildProjectUiSchemaSettings = require("../../../projects/_internal/ProjectUiSchemaSettings");
const buildInitiativeUiSchemaEdit = require("../../../initiatives/_internal/InitiativeUiSchemaEdit");
const buildInitiativeUiSchemaCreate = require("../../../initiatives/_internal/InitiativeUiSchemaCreate");
const buildInitiativeUiSchemaCreate2 = require("../../../initiatives/_internal/InitiativeUiSchemaCreate2");
const buildInitiativeUiSchemaMetrics = require("./metrics/InitiativeUiSchemaMetrics");
const buildInitiativeUiSchemaAssociations = require("../../../initiatives/_internal/InitiativeUiSchemaAssociations");
const buildInitiativeUiSchemaSettings = require("../../../initiatives/_internal/InitiativeUiSchemaSettings");
const buildPageUiSchemaEdit = require("../../../pages/_internal/PageUiSchemaEdit");
const buildPageUiSchemaCreate = require("../../../pages/_internal/PageUiSchemaCreate");
const buildContentUiSchemaEdit = require("../../../content/_internal/ContentUiSchemaEdit");
const buildContentUiSchemaSettings = require("../../../content/_internal/ContentUiSchemaSettings");
const buildContentUiSchemaDiscussionSettings = require("../../../core/schemas/internal/discussions/EntityUiSchemaDiscussionsSettings");
const buildContentUiSchemaDiscussionSettingsCompact = require("../../../core/schemas/internal/discussions/EntityUiSchemaDiscussionsSettingsCompact");
const buildTemplateUiSchemaEdit = require("../../../templates/_internal/TemplateUiSchemaEdit");
const buildGroupUiSchemaCreateFollowers = require("../../../groups/_internal/GroupUiSchemaCreateFollowers");
const builddGroupUiSchemaCreateAssociation = require("../../../groups/_internal/GroupUiSchemaCreateAssociation");
const builddGroupUiSchemaCreateView = require("../../../groups/_internal/GroupUiSchemaCreateView");
const builddGroupUiSchemaCreateEdit = require("../../../groups/_internal/GroupUiSchemaCreateEdit");
const builddGroupUiSchemaCreate = require("../../../groups/_internal/GroupUiSchemaCreate");
const builddGroupUiSchemaEdit = require("../../../groups/_internal/GroupUiSchemaEdit");
const builddGroupUiSchemaSettings = require("../../../groups/_internal/GroupUiSchemaSettings");
const builddGroupUiSchemaDiscussionSettings = require("../../../core/schemas/internal/discussions/EntityUiSchemaDiscussionsSettings");
const buildEventSchemaCreate = require("../../../events/_internal/EventSchemaCreate");
const buildEventSchemaEdit = require("../../../events/_internal/EventSchemaEdit");
const buildEventSchemaRegistrants = require("../../../events/_internal/EventSchemaAttendeesSettings");
const buildEventUiSchemaCreate = require("../../../events/_internal/EventUiSchemaCreate");
const buildEventUiSchemaEdit = require("../../../events/_internal/EventUiSchemaEdit");
const buildEventUiSchemaRegistrants = require("../../../events/_internal/EventUiSchemaAttendeesSettings");
const buildInitiativeTemplateUiSchemaEdit = require("../../../initiative-templates/_internal/InitiativeTemplateUiSchemaEdit");
const buildUserUiSchemaSettings = require("../../../users/_internal/UserUiSchemaSettings");
const UserSchema_1 = require("../../../users/_internal/UserSchema");
/**
 * get the editor schema and uiSchema defined for an editor (either an entity or a card).
 * The schema and uiSchema that are returned can be used to
 * render a form UI (using the configuration editor)
 *
 * @param i18nScope translation scope to be interpolated into the uiSchema
 * @param type editor type - corresonds to the returned uiSchema
 * @param options optional hash of dynamic uiSchema element options
 * @returns
 */
async function getEditorSchemas(i18nScope, type, options, context) {
    const editorType = type.split(":")[1];
    // schema and uiSchema are dynamically imported based on
    // the entity type and the provided editor type
    let schema;
    let uiSchema;
    let defaults;
    switch (editorType) {
        case "site": {
            const MODULES = {
                "hub:site:edit": buildSiteUiSchemaEdit,
                "hub:site:create": buildSiteUiSchemaCreate,
                "hub:site:followers": buildSiteUiSchemaFollowers,
                "hub:site:settings": buildSiteUiSchemaSettings,
                "hub:site:assistant": buildSiteUiSchemaAssistant,
                "hub:site:settings:discussions": buildSiteUiSchemaDiscussionSettings,
            };
            const module = MODULES[type];
            // site id is needed for validation of site url
            schema = (0, SiteSchema_1.getSiteSchema)(options.id, context);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when implementing buildDefaults for sites, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "discussion": {
            const MODULES = {
                "hub:discussion:edit": buildDiscussionUiSchemaEdit,
                "hub:discussion:create": buildDiscussionUiSchemaCreate,
                "hub:discussion:settings": buildDiscussionUiSchemaSettings,
                "hub:discussion:settings:discussions": buildDiscussionUiSchemaDiscussionSettings,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(DiscussionSchema_1.DiscussionSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for discussions, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "channel": {
            const MODULES = {
                "hub:channel:edit": buildChannelUiSchemaEdit,
                "hub:channel:create": buildChannelUiSchemaCreate,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(ChannelSchema_1.ChannelSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for discussions, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "project": {
            const MODULES = {
                "hub:project:edit": buildProjectUiSchemaEdit,
                "hub:project:create": buildProjectUiSchemaCreate,
                "hub:project:create2": buildProjectUiSchemaCreate2,
                "hub:project:metrics": buildProjectUiSchemaMetrics,
                "hub:project:settings": buildProjectUiSchemaSettings,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(ProjectSchema_1.ProjectSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "initiative": {
            const MODULES = {
                "hub:initiative:edit": buildInitiativeUiSchemaEdit,
                "hub:initiative:create": buildInitiativeUiSchemaCreate,
                "hub:initiative:create2": buildInitiativeUiSchemaCreate2,
                "hub:initiative:metrics": buildInitiativeUiSchemaMetrics,
                "hub:initiative:associations": buildInitiativeUiSchemaAssociations,
                "hub:initiative:settings": buildInitiativeUiSchemaSettings,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(InitiativeSchema_1.InitiativeSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "page": {
            const MODULES = {
                "hub:page:edit": buildPageUiSchemaEdit,
                "hub:page:create": buildPageUiSchemaCreate,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(PageSchema_1.PageSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for pages, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "content": {
            const MODULES = {
                "hub:content:edit": buildContentUiSchemaEdit,
                "hub:content:settings": buildContentUiSchemaSettings,
                "hub:content:settings:discussions": buildContentUiSchemaDiscussionSettings,
                "hub:content:settings:discussions:compact": buildContentUiSchemaDiscussionSettingsCompact,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(ContentSchema_1.ContentSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for content, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "template": {
            const MODULES = {
                "hub:template:edit": buildTemplateUiSchemaEdit,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(TemplateSchema_1.TemplateSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for templates, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "group": {
            const MODULES = {
                "hub:group:create:followers": buildGroupUiSchemaCreateFollowers,
                "hub:group:create:association": builddGroupUiSchemaCreateAssociation,
                "hub:group:create:view": builddGroupUiSchemaCreateView,
                "hub:group:create:edit": builddGroupUiSchemaCreateEdit,
                "hub:group:create": builddGroupUiSchemaCreate,
                "hub:group:edit": builddGroupUiSchemaEdit,
                "hub:group:settings": builddGroupUiSchemaSettings,
                "hub:group:settings:discussions": builddGroupUiSchemaDiscussionSettings,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(GroupSchema_1.GroupSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "event": {
            const SCHEMA_MODULES = {
                "hub:event:create": buildEventSchemaCreate,
                "hub:event:edit": buildEventSchemaEdit,
                "hub:event:registrants": buildEventSchemaRegistrants,
            };
            const UI_SCHEMA_MODULES = {
                "hub:event:create": buildEventUiSchemaCreate,
                "hub:event:edit": buildEventUiSchemaEdit,
                "hub:event:registrants": buildEventUiSchemaRegistrants,
            };
            schema = SCHEMA_MODULES[type].buildSchema();
            const uiSchemaModule = UI_SCHEMA_MODULES[type];
            uiSchema = await uiSchemaModule.buildUiSchema(i18nScope, options, context);
            break;
        }
        // ----------------------------------------------------
        case "initiativeTemplate": {
            const MODULES = {
                "hub:initiativeTemplate:edit": buildInitiativeTemplateUiSchemaEdit,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(InitiativeTemplateSchema_1.InitiativeTemplateSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for initiative templates, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "user": {
            const MODULES = {
                "hub:user:settings": buildUserUiSchemaSettings,
            };
            const module = MODULES[type];
            schema = (0, util_1.cloneObject)(UserSchema_1.UserSchema);
            uiSchema = await module.buildUiSchema(i18nScope, options, context);
            // if we have the buildDefaults fn, then construct the defaults
            // TODO: when first implementing buildDefaults for users, remove the ignore line
            /* istanbul ignore next @preserve */
            if (module.buildDefaults) {
                defaults = await module.buildDefaults(i18nScope, options, context);
            }
            break;
        }
        // ----------------------------------------------------
        case "card": {
            const result = await (0, getCardEditorSchemas_1.getCardEditorSchemas)(i18nScope, type, options, context);
            schema = result.schema;
            uiSchema = result.uiSchema;
            defaults = result.defaults;
        }
    }
    // filter out properties not used in the UI schema
    schema = (0, filterSchemaToUiSchema_1.filterSchemaToUiSchema)(schema, uiSchema);
    // if schema includes slug, add conditional validation
    schema = (0, addDynamicSlugValidation_1.addDynamicSlugValidation)(schema, options);
    return Promise.resolve({ schema, uiSchema, defaults });
}
//# sourceMappingURL=getEditorSchemas.js.map