"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getManifestId = getManifestId;
exports.getDefinitionId = getDefinitionId;
exports.createObjectPageConfigV4 = createObjectPageConfigV4;
const ux_specification_types_1 = require("@sap/ux-specification-types");
const utils_1 = require("../utils");
const common_1 = require("../../../common");
const i18next_1 = __importDefault(require("i18next"));
const extensionLogger_1 = require("../../../../extensionLogger");
const i18n_1 = require("../../../../i18n/i18n");
const factory_1 = require("../../export/factory");
const fpmCustomPage_1 = require("./fpmCustomPage");
const v4_1 = require("@sap/ux-specification-types/src/v4");
const utils_2 = require("../../utils/utils");
/**
 * Converts section ID to manifest ID (replaces '::' with ['/', '#']).
 *
 * @param sectionId - converted ID of the given object page section
 * @param splitOnLt - split ID and get only part in <...>
 * @param annotationTerm - annotationTerm to split Id on
 * @returns manifest ID
 */
function getManifestId(sectionId, splitOnLt = false, annotationTerm) {
    const id = splitOnLt ? sectionId.split('<')[1]?.slice(0, -1) : sectionId;
    if (annotationTerm) {
        const pathParts = id.split(`@${annotationTerm}`);
        return `${pathParts[0]?.replace(/::/g, '/')}@${annotationTerm}${pathParts[1]?.replace(/::/g, '#')}`;
    }
    return `${sectionId?.replace(/::/g, '/').replace(/::/g, '#')}`;
}
/**
 * Converts manifest ID to definition ID (replaces ['#', '/'] with '::').
 *
 * @param manifestId - ID of the given object page section in manifest
 * @returns definition ID
 */
function getDefinitionId(manifestId) {
    return manifestId.replace(/#/g, '::').replace(/\//g, '::');
}
/**
 * Fills the section table columns in a V4 config.
 *
 * @param importParameters  - object comprising all input data
 * @param manifestSection - section of the manifest.json
 * @param configPart - the given part of the object page config
 * @param sectionId - converted ID of the given object page section
 * @param {MetadataInstanceInterface} factory - factory, for generating the access to reflect-metadata
 * @param manifestSectionId - ID of the given object page section in manifest
 */
function fillTableColumnsInConfig(importParameters, manifestSection, configPart, sectionId, factory, manifestSectionId) {
    if (manifestSection['columns']) {
        configPart[sectionId].table.columns = {};
        let tableDefinition;
        const lineItems = importParameters.jsonSchema.definitions[getDefinitionId(manifestSectionId)];
        if (lineItems) {
            for (const columnKey in lineItems.properties) {
                const columnInSchema = lineItems.properties[columnKey];
                if (columnInSchema.$ref.indexOf(ux_specification_types_1.DefinitionName.CustomColumnOP) > -1) {
                    tableDefinition = ux_specification_types_1.DefinitionName.CustomColumnOP;
                }
                else {
                    tableDefinition = ux_specification_types_1.DefinitionName.ObjectPageTableColumn;
                }
                const columnInConfig = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, tableDefinition);
                (0, utils_1.importSettingsOfObject)(columnInConfig, importParameters.manifest, importParameters.jsonSchema.definitions[tableDefinition], importParameters.routingId, [columnKey], manifestSectionId);
                if (Object.keys(columnInConfig).length > 0) {
                    configPart[sectionId].table.columns[columnKey] = columnInConfig;
                }
            }
        }
    }
}
/**
 * Fills table actions and custom section actions in config.json.
 *
 * @param {ImportObjectPageV4Parameters} importParameters - object comprising all input data
 * @param {Definition} actionsDefinition - actions definition from schema
 * @param {string} sectionId - ID of the current section
 * @param {object} configPart - the given part of the object page config
 * @param {MetadataInstanceInterface} factory - factory, for generating the access to reflect-metadata
 * @param manifestSectionId - section id in manifest
 */
function fillSectionActionsInConfig(importParameters, actionsDefinition, sectionId, configPart, factory, manifestSectionId) {
    if (typeof actionsDefinition === 'object') {
        for (const actionKey in actionsDefinition['properties']) {
            const actionDefinition = actionsDefinition.properties[actionKey];
            if (typeof actionDefinition === 'object' && actionDefinition.$ref) {
                const definitionKey = (0, common_1.getDefinitionKey)(actionDefinition.$ref);
                const definition = actionsDefinition?.['properties']?.[actionKey];
                const schemaPropertyName = definitionKey.split('<')[0];
                let configActionsPart;
                if (schemaPropertyName.includes(ux_specification_types_1.DefinitionName.CustomSectionActionOP)) {
                    // custom section actions
                    configActionsPart = configPart['actions'];
                }
                else {
                    // table toolBar actions
                    configActionsPart = configPart[sectionId].table.toolBar.actions;
                }
                const importActionParams = {
                    actionPropertyDefinition: definition,
                    appSchema: importParameters.jsonSchema,
                    factory,
                    actionsInConfig: configActionsPart,
                    actionKey,
                    manifest: importParameters.manifest,
                    routingId: importParameters.routingId,
                    manifestSectionId
                };
                (0, utils_2.importActionSettingsOfReference)(importActionParams);
            }
        }
    }
}
/**
 * Fills form  actions in config.json.
 *
 * @param importParameters - object comprising all input data
 * @param manifestSection - section of the manifest.json containing the relevant settings
 * @param sectionId - ID of the current section
 * @param configPart - the given part of the object page config
 * @param factory - factory, for generating the access to reflect-metadata
 * @param manifestSectionId - ID of the section in the manifest
 */
function fillFormActionsInConfig(importParameters, manifestSection, sectionId, configPart, factory, manifestSectionId) {
    if (manifestSection['actions']) {
        const configActionsPart = (configPart[sectionId].form.actions = {});
        const forms = importParameters.jsonSchema.definitions[`${ux_specification_types_1.DefinitionName.ObjectPageSectionForm}<${getDefinitionId(manifestSectionId)}>`];
        const formReference = forms.properties.form['$ref'].split(common_1.DEFINITION_LINK_PREFIX)[1];
        const form = importParameters.jsonSchema.definitions[formReference];
        const actions = form.properties['actions'];
        const actionsReference = actions.$ref.split(common_1.DEFINITION_LINK_PREFIX)[1];
        const actionsDefinition = importParameters.jsonSchema.definitions[actionsReference];
        for (const actionKey in actionsDefinition['properties']) {
            const actionDefinition = actionsDefinition.properties[actionKey];
            const definitionKey = (0, common_1.getDefinitionKey)(actionDefinition.$ref);
            const formActionDefinition = importParameters.jsonSchema.definitions[definitionKey];
            if (formActionDefinition['annotationType'] === "com.sap.vocabularies.UI.v1.DataFieldForAction" /* UIAnnotationTypes.DataFieldForAction */ ||
                formActionDefinition['annotationType'] === "com.sap.vocabularies.UI.v1.DataFieldForActionGroup" /* UIAnnotationTypes.DataFieldForActionGroup */ ||
                formActionDefinition.description === 'Custom Action') {
                const importActionParams = {
                    actionPropertyDefinition: actionDefinition,
                    appSchema: importParameters.jsonSchema,
                    factory,
                    actionsInConfig: configActionsPart,
                    actionKey,
                    manifest: importParameters.manifest,
                    routingId: importParameters.routingId,
                    manifestSectionId
                };
                (0, utils_2.importActionSettingsOfReference)(importActionParams);
            }
        }
    }
}
/**
 * Fills custom fields in config.json.
 *
 * @param importParameters - object comprising all input data
 * @param manifestSection - section of the manifest.json containing the relevant settings
 * @param sectionId - ID of the current section
 * @param configPart - the given part of the object page config
 * @param factory - factory, for generating the access to reflect-metadata
 * @param manifestSectionId - ID of the section in the manifest
 * @param sectionDefinitionName - definition key for fields section
 */
function fillFieldInConfig(importParameters, manifestSection, sectionId, configPart, factory, manifestSectionId, sectionDefinitionName = ux_specification_types_1.DefinitionName.ObjectPageSectionForm) {
    if (manifestSection['fields']) {
        const forms = importParameters.jsonSchema.definitions[`${sectionDefinitionName}<${getDefinitionId(manifestSectionId)}>`];
        const formReference = forms.properties.form['$ref'].split(common_1.DEFINITION_LINK_PREFIX)[1];
        const form = importParameters.jsonSchema.definitions[formReference];
        const fields = form.properties['fields'];
        const fieldsReference = fields.$ref.split(common_1.DEFINITION_LINK_PREFIX)[1];
        const fieldsDefinition = importParameters.jsonSchema.definitions[fieldsReference];
        for (const fieldKey in fieldsDefinition['properties']) {
            const fieldDefinition = fieldsDefinition.properties[fieldKey];
            const definitionKey = (0, common_1.getDefinitionKey)(fieldDefinition.$ref);
            const formFieldDefinition = importParameters.jsonSchema.definitions[definitionKey];
            if (!('annotationPath' in formFieldDefinition) && formFieldDefinition.description === 'Custom Field') {
                configPart[sectionId].form ??= {};
                configPart[sectionId].form.fields ??= {};
                const schemaPropertyName = definitionKey.split('<')[0];
                const fieldInstance = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, schemaPropertyName);
                configPart[sectionId].form.fields[fieldKey] = fieldInstance;
                (0, utils_1.importSettingsOfObject)(fieldInstance, importParameters.manifest, formFieldDefinition, importParameters.routingId, [fieldKey], sectionId, undefined);
            }
        }
    }
}
/**
 * Retrieves the table definition name based on the provided table type.
 *
 * @param tableType - The type of the table for which the definition name is required.
 * @returns - The corresponding table definition name, or a default name if the table type is not found.
 */
function getTableDefinitionNameByType(tableType) {
    const tableDefinitionsMap = new Map([
        [v4_1.TableTypeV4.AnalyticalTable, ux_specification_types_1.DefinitionName.ObjectPageAnalyticalTableV4],
        [v4_1.TableTypeV4.GridTable, ux_specification_types_1.DefinitionName.ObjectPageGridTableV4],
        [v4_1.TableTypeV4.ResponsiveTable, ux_specification_types_1.DefinitionName.ObjectPageResponsiveTableV4],
        [v4_1.TableTypeV4.TreeTable, ux_specification_types_1.DefinitionName.ObjectPageTreeTableV4]
    ]);
    return tableDefinitionsMap.get(tableType) ?? ux_specification_types_1.DefinitionName.ObjectPageResponsiveTableV4;
}
/**
 * Fills the table settings in config.json.
 *
 * @param importParameters  - object comprising all input data
 * @param manifestSettings - object page settings section of the manifest
 * @param configPart - the given part of the object page config
 * @param sectionId - ID of the current section
 * @param factory - factory, for generating the access to reflect-metadata
 * @param tableId - ID of the table in the manifest, if available
 */
function fillTableInConfig(importParameters, manifestSettings, configPart, sectionId, factory, tableId) {
    const manifestSectionId = tableId ? tableId : getManifestId(sectionId, false, "com.sap.vocabularies.UI.v1.LineItem" /* UIAnnotationTerms.LineItem */);
    const manifestSection = manifestSettings.controlConfiguration?.[manifestSectionId];
    if (manifestSection) {
        const { tableSettings = {} } = manifestSection;
        const tableType = tableSettings['type'];
        const tableDefinitionName = getTableDefinitionNameByType(tableType);
        // Data needs to be transferred
        configPart[sectionId] = {};
        configPart[sectionId].table = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, tableDefinitionName);
        const sectionDefinition = `${tableDefinitionName}<${tableId ? getDefinitionId(tableId) : sectionId.replace('#', '::')}>`;
        (0, utils_1.importSettingsOfObject)(configPart[sectionId].table, importParameters.manifest, importParameters.jsonSchema.definitions[sectionDefinition], importParameters.routingId, [], manifestSectionId);
        configPart[sectionId].table.toolBar = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ToolBar, {
            actions: {}
        });
        const actionsDefinition = importParameters.jsonSchema.definitions[`${ux_specification_types_1.DefinitionName.Actions}<${getDefinitionId(manifestSectionId)}>`];
        fillSectionActionsInConfig(importParameters, actionsDefinition, sectionId, configPart, factory, manifestSectionId);
        fillTableColumnsInConfig(importParameters, manifestSection, configPart, sectionId, factory, manifestSectionId);
    }
}
/**
 * Fills the form settings in config.json.
 *
 * @param importParameters  - object comprising all input data
 * @param manifestSettings - object page settings section of the manifest
 * @param configPart - the given part of the object page config
 * @param sectionId - ID of the current section
 * @param factory - factory, for generating the access to reflect-metadata
 * @param includeActions - Whether to include action configurations in the form(default is 'true')
 */
function fillFormInConfig(importParameters, manifestSettings, configPart, sectionId, factory, includeActions = true) {
    const manifestSectionId = getManifestId(sectionId);
    const manifestSection = manifestSettings.controlConfiguration?.[manifestSectionId];
    if (manifestSection) {
        configPart[sectionId] = {};
        const initialConfig = includeActions ? { actions: {} } : {};
        configPart[sectionId].form = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageForm, initialConfig);
        if (includeActions) {
            fillFormActionsInConfig(importParameters, manifestSection, sectionId, configPart, factory, manifestSectionId);
        }
        // fill custom fields
        fillFieldInConfig(importParameters, manifestSection, sectionId, configPart, factory, manifestSectionId);
    }
}
/**
 * Fills the custom sub section in config.json.
 *
 * @param importParameters Object comprising all input data
 * @param schemaSection Section schema
 * @param configPart The given part of the object page config
 * @param sectionId ID of the current section
 * @param factory Factory, for generating the access to reflect-metadata
 * @param parentId Parent section id
 */
function fillCustomSubSectionInConfig(importParameters, schemaSection, configPart, sectionId, factory, parentId) {
    const section = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageCustomSubSectionFragment);
    const sectionIdRef = schemaSection['$ref'].split(common_1.DEFINITION_LINK_PREFIX)[1];
    (0, utils_1.importSettingsOfObject)(section, importParameters.manifest, importParameters.jsonSchema.definitions[sectionIdRef], importParameters.routingId, [`subSections/${sectionId}`], parentId);
    if (section.fragmentName) {
        const customPageConfig = (0, fpmCustomPage_1.createCustomFragmentConfig)(importParameters, section.fragmentName);
        section.controls = customPageConfig;
    }
    configPart[sectionId] = section;
}
/**
 * Fills the sections and subsections of the configuration (recursive function).
 *
 * @param {ImportObjectPageV4Parameters} importParameters - object comprising all input data
 * @param sectionProperties - current properties' list (of sections or subsections)
 * @param manifestSettings - object page settings section of the manifest
 * @param configPart - the given part of the object page config
 * @param {MetadataInstanceInterface} factory - factory, for generating the access to reflect-metadata
 * @param parentId - parent section id
 */
function fillSectionsInConfig(importParameters, sectionProperties, manifestSettings, configPart, factory, parentId) {
    for (const sectionId in sectionProperties) {
        const schemaSection = sectionProperties[sectionId];
        let tableManifestId = sectionId;
        if ((sectionId.includes("com.sap.vocabularies.UI.v1.PresentationVariant" /* UIAnnotationTerms.PresentationVariant */) ||
            sectionId.includes("com.sap.vocabularies.UI.v1.SelectionPresentationVariant" /* UIAnnotationTerms.SelectionPresentationVariant */)) &&
            schemaSection.$ref?.startsWith(common_1.DEFINITION_LINK_PREFIX + ux_specification_types_1.DefinitionName.ObjectPageSectionTableV4)) {
            const sectionIdRef = schemaSection['$ref'].split(common_1.DEFINITION_LINK_PREFIX)[1];
            tableManifestId = getManifestId(sectionIdRef, true, "com.sap.vocabularies.UI.v1.LineItem" /* UIAnnotationTerms.LineItem */);
            fillTableInConfig(importParameters, manifestSettings, configPart, sectionId, factory, tableManifestId);
        }
        else if (schemaSection.$ref?.startsWith(common_1.DEFINITION_LINK_PREFIX + ux_specification_types_1.DefinitionName.ObjectPageSectionTableV4)) {
            // Section without subsections
            fillTableInConfig(importParameters, manifestSettings, configPart, sectionId, factory);
        }
        else if (schemaSection.properties && schemaSection.properties.subsections) {
            configPart[sectionId] = {
                subsections: {}
            };
            fillSectionsInConfig(importParameters, schemaSection.properties.subsections['properties'], manifestSettings, configPart[sectionId].subsections, factory, sectionId);
        }
        else if (schemaSection.$ref?.startsWith(common_1.DEFINITION_LINK_PREFIX + ux_specification_types_1.DefinitionName.ObjectPageSectionForm) &&
            (schemaSection.$ref?.includes(`@${"com.sap.vocabularies.UI.v1.FieldGroup" /* UIAnnotationTerms.FieldGroup */}::`) ||
                schemaSection.$ref?.includes(`@${"com.sap.vocabularies.UI.v1.Identification" /* UIAnnotationTerms.Identification */}`))) {
            // Form section
            fillFormInConfig(importParameters, manifestSettings, configPart, sectionId, factory, !schemaSection.$ref.includes(`@${"com.sap.vocabularies.UI.v1.Identification" /* UIAnnotationTerms.Identification */}`));
        }
        else if (schemaSection.$ref?.startsWith(common_1.DEFINITION_LINK_PREFIX + ux_specification_types_1.DefinitionName.ObjectPageCustomSubSectionFragment)) {
            fillCustomSubSectionInConfig(importParameters, schemaSection, configPart, sectionId, factory, parentId);
        }
    }
}
/**
 * Fills the custom sections of the configuration.
 *
 * @param {ImportObjectPageV4Parameters} importParameters - object comprising all input data
 * @param manifestSettings - object page settings section of the manifest
 * @param objectPageConfig - the given part of the object page config
 * @param {MetadataInstanceInterface} factory - factory, for generating the access to reflect-metadata
 */
function fillCustomSectionsInConfig(importParameters, manifestSettings, objectPageConfig, factory) {
    const customSections = manifestSettings.content?.body?.sections;
    if (customSections) {
        for (const sectionId in customSections) {
            const customSection = customSections[sectionId];
            if ('subSections' in customSection) {
                //'subSections' is reserved property - It is not custom section, but object with custom subsections
                continue;
            }
            const section = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageCustomSectionFragment);
            (0, utils_1.importSettingsOfObject)(section, importParameters.manifest, importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.ObjectPageCustomSectionFragment], importParameters.routingId, [], sectionId);
            // fill custom section with custom actions
            if (customSection.actions) {
                section.actions = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageCustomSectionActions, {});
                const actionsDefinition = importParameters.jsonSchema.definitions[`${ux_specification_types_1.DefinitionName.ObjectPageCustomSectionActions}<${getDefinitionId(sectionId)}>`];
                fillSectionActionsInConfig(importParameters, actionsDefinition, sectionId, section, factory, sectionId);
            }
            const fragmentName = customSection.template;
            if (fragmentName) {
                const customPageConfig = (0, fpmCustomPage_1.createCustomFragmentConfig)(importParameters, fragmentName);
                section.controls = customPageConfig;
            }
            objectPageConfig.sections[sectionId] = section;
        }
    }
}
/**
 * Fills the custom header sections of the configuration.
 *
 * @param {ImportObjectPageV4Parameters} importParameters - object comprising all input data
 * @param {v4.SapUiPageSettingsV4} manifestSettings - object page settings header section of the manifest
 * @param {v4.ObjectPageConfigV4} objectPageConfig - the given part of the object page config
 * @param {MetadataInstanceInterface} factory - factory, for generating the access to reflect-metadata
 */
function fillCustomHeaderSectionsInConfig(importParameters, manifestSettings, objectPageConfig, factory) {
    const header = manifestSettings.content?.header;
    const customHeaderSections = header && 'facets' in header ? header.facets : undefined;
    if (customHeaderSections) {
        for (const sectionId in customHeaderSections) {
            const customHeaderSection = customHeaderSections[sectionId];
            const section = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageCustomHeaderSectionFragment);
            (0, utils_1.importSettingsOfObject)(section, importParameters.manifest, importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.ObjectPageCustomHeaderSectionFragment], importParameters.routingId, [], sectionId);
            // assign fragment contents in config
            const readFragmentName = customHeaderSection.template;
            if (readFragmentName) {
                const customPageConfig = (0, fpmCustomPage_1.createCustomFragmentConfig)(importParameters, readFragmentName);
                section.controls = customPageConfig;
            }
            const editFragmentName = customHeaderSection.templateEdit;
            if (editFragmentName) {
                const customPageConfig = (0, fpmCustomPage_1.createCustomFragmentConfig)(importParameters, editFragmentName);
                section.controlsEditMode = customPageConfig;
            }
            objectPageConfig.header.sections[sectionId] = section;
        }
    }
}
/**
 * Fills the header facets in the config file of a V4 object page.
 *
 * @param appSchema - app specific schema
 * @param objectPageConfig - configuration, to be enhanced
 * @param factory - factory, for generating the access to reflect-metadata
 * @param manifest - manifest.json of the app
 * @param manifestSettings - object page settings section of the manifest
 * @param routingId - routing ID of the page
 */
function fillHeaderFacets(appSchema, objectPageConfig, factory, manifest, manifestSettings, routingId) {
    const headerSections = appSchema.definitions[ux_specification_types_1.DefinitionName.HeaderSections];
    if (headerSections && headerSections.properties) {
        objectPageConfig.header.sections = {};
        for (const sectionId in headerSections.properties) {
            let sectionDefinition = headerSections.properties[sectionId];
            let factoryKey;
            const sectionDefinitionKey = headerSections.properties[sectionId]['$ref']?.split(common_1.DEFINITION_LINK_PREFIX)[1];
            if (sectionDefinitionKey) {
                sectionDefinition = appSchema.definitions[sectionDefinitionKey];
                factoryKey = sectionDefinitionKey.split('<')[0];
            }
            const isCustomHeaderSection = factoryKey?.includes(ux_specification_types_1.DefinitionName.ObjectPageCustomHeaderSectionFragment);
            if (!isCustomHeaderSection) {
                objectPageConfig.header.sections[sectionId] = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, factoryKey);
                (0, utils_1.importSettingsOfObject)(objectPageConfig.header.sections[sectionId], manifest, sectionDefinition, routingId, [], sectionDefinition?.title?.split(ux_specification_types_1.FACETTITLEPREFIX)[1]);
                // fill custom fields
                const manifestSectionId = getManifestId(sectionId);
                const manifestSection = manifestSettings.controlConfiguration?.[manifestSectionId];
                if (manifestSection) {
                    fillFieldInConfig({
                        jsonSchema: appSchema,
                        manifest,
                        routingId
                    }, manifestSection, sectionId, objectPageConfig.header.sections, factory, manifestSectionId, ux_specification_types_1.DefinitionName.ObjectPageHeaderSectionForm);
                }
            }
        }
    }
}
/**
 * Fills the header and footer actions in the config file of a V4 object page.
 *
 * @param {Definition} appSchema - app specific schema
 * @param {v4.ObjectPageConfigV4} objectPageConfig - configuration, to be enhanced
 * @param {MetadataInstanceInterface} factory - factory, for generating the access to reflect-metadata
 * @param {Manifest} manifest - manifest.json of the app
 * @param {string} routingId - routing id of page
 * @param {DefinitionName} definitionName definition name in the schema
 * @param {PropertyName} actionPropertyName - header or a footer action
 */
function fillHeaderFooterActions(appSchema, objectPageConfig, factory, manifest, routingId, definitionName, actionPropertyName) {
    const actions = appSchema.definitions[definitionName];
    if (typeof actions === 'object') {
        const actionsInConfig = (objectPageConfig[actionPropertyName].actions = {});
        for (const actionKey in actions.properties) {
            const definition = actions.properties[actionKey];
            if (typeof definition === 'object' && definition.$ref) {
                const importActionParams = {
                    actionPropertyDefinition: definition,
                    appSchema,
                    factory,
                    actionsInConfig,
                    actionKey,
                    manifest,
                    routingId
                };
                (0, utils_2.importActionSettingsOfReference)(importActionParams);
            }
        }
    }
}
/**
 * Fills the object page configuration from the given manifest.
 *
 * @param importParameters  - object comprising all input data
 * @param v4Page - current (object) page
 * @param factory - factory, for generating the access to reflect-metadata
 * @returns the object page configuration
 */
function fillObjectPageConfig(importParameters, v4Page, factory) {
    const objectPageConfig = factory.createPageInstance(ux_specification_types_1.PageTypeV4.ObjectPage);
    const manifestSettings = v4Page.options && v4Page.options.settings;
    // Transfer header information
    objectPageConfig.header = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageHeader);
    (0, utils_1.importSettingsOfObject)(objectPageConfig.header, importParameters.manifest, importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.ObjectPageHeader], importParameters.routingId, []);
    // Transfer footer information
    objectPageConfig.footer = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageFooter);
    (0, utils_1.importSettingsOfObject)(objectPageConfig.footer, importParameters.manifest, importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.ObjectPageFooter], importParameters.routingId, []);
    // Transfer layout information
    objectPageConfig.layout = factory.createInstance(ux_specification_types_1.PageTypeV4.ObjectPage, ux_specification_types_1.DefinitionName.ObjectPageLayout);
    (0, utils_1.importSettingsOfObject)(objectPageConfig.layout, importParameters.manifest, importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.ObjectPageLayout], importParameters.routingId, []);
    (0, utils_1.importSettingsOfObject)(objectPageConfig, importParameters.manifest, importParameters.jsonSchema, importParameters.routingId, []);
    if (!objectPageConfig.sections) {
        objectPageConfig.sections = {};
    }
    if (manifestSettings) {
        if ((manifestSettings.controlConfiguration || manifestSettings.content?.body?.sections) &&
            importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.Sections]) {
            // Fill sections and subsections
            objectPageConfig.sections = {};
            fillSectionsInConfig(importParameters, importParameters.jsonSchema.definitions[ux_specification_types_1.DefinitionName.Sections]['properties'], manifestSettings, objectPageConfig.sections, factory);
        }
        // Fill custom sections
        fillCustomSectionsInConfig(importParameters, manifestSettings, objectPageConfig, factory);
        // Fill header facets
        fillHeaderFacets(importParameters.jsonSchema, objectPageConfig, factory, importParameters.manifest, manifestSettings, importParameters.routingId);
        // Fill custom header sections
        fillCustomHeaderSectionsInConfig(importParameters, manifestSettings, objectPageConfig, factory);
        fillHeaderFooterActions(importParameters.jsonSchema, objectPageConfig, factory, importParameters.manifest, importParameters.routingId, ux_specification_types_1.DefinitionName.ObjectPageHeaderActions, ux_specification_types_1.PropertyName.header);
        fillHeaderFooterActions(importParameters.jsonSchema, objectPageConfig, factory, importParameters.manifest, importParameters.routingId, ux_specification_types_1.DefinitionName.FooterActions, ux_specification_types_1.PropertyName.footer);
    }
    return objectPageConfig;
}
/**
 * External API: Create configuration (file content) for a V4 object page.
 *
 * @param importParameters  - object comprising all input data
 * @returns config - the object page configuration
 */
function createObjectPageConfigV4(importParameters) {
    // Initialize i18next
    (0, i18n_1.initI18n)();
    if (!importParameters.manifest[ux_specification_types_1.ManifestSection.ui5] || !importParameters.manifest[ux_specification_types_1.ManifestSection.ui5]['routing']) {
        (0, extensionLogger_1.log)(importParameters.logger, {
            severity: "error" /* LogSeverity.Error */,
            message: i18next_1.default.t('NOROUTING'),
            location: {
                path: ux_specification_types_1.MANIFESTPATH,
                range: [ux_specification_types_1.ManifestSection.ui5]
            }
        });
        return;
    }
    const v4Pages = importParameters.manifest[ux_specification_types_1.ManifestSection.ui5]['routing']['targets'];
    if (!v4Pages) {
        (0, extensionLogger_1.log)(importParameters.logger, {
            severity: "error" /* LogSeverity.Error */,
            message: i18next_1.default.t('NOTARGETS'),
            location: {
                path: ux_specification_types_1.MANIFESTPATH,
                range: [ux_specification_types_1.ManifestSection.ui5, 'routing']
            }
        });
        return;
    }
    const v4Page = v4Pages[importParameters.routingId];
    if (!v4Page) {
        (0, extensionLogger_1.log)(importParameters.logger, {
            severity: "error" /* LogSeverity.Error */,
            message: i18next_1.default.t('NOROUTINGID', { routingId: importParameters.routingId }),
            location: {
                path: ux_specification_types_1.MANIFESTPATH,
                range: [ux_specification_types_1.ManifestSection.ui5, 'routing']
            }
        });
        return;
    }
    const factory = new factory_1.MetadataInstanceFactoryV4();
    //Fill config.json
    return fillObjectPageConfig(importParameters, v4Page, factory);
}
//# sourceMappingURL=objectPage.js.map