"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prompts = exports.getGenericSchema = exports.readApp = void 0;
exports.generateSchema = generateSchema;
exports.exportConfig = exportConfig;
exports.exportConfigEntityByPath = exportConfigEntityByPath;
exports.deleteConfigEntityByPath = deleteConfigEntityByPath;
exports.importConfig = importConfig;
exports.importProject = importProject;
exports.importProjectSchema = importProjectSchema;
exports.getApiVersion = getApiVersion;
exports.generateCustomExtension = generateCustomExtension;
/**
 * This file implements the interface 'Specification' from ./types and serves as API to specification
 */
const ux_specification_types_1 = require("@sap/ux-specification-types");
const generate_1 = require("./sync/v2/generate");
const generate_2 = require("./sync/v4/generate");
const exportPage_1 = require("./sync/v2/export/exportPage");
const import_1 = require("./sync/v2/import");
const pageAccess_1 = require("./sync/v2/genericSchemaHandling/pages/pageAccess"); // Can be switched for manual tests to use ListReportNew by switching useGenericSchemaHandling in pageAccess.ts from false to true
const export_1 = require("./sync/v4/export/export");
const import_2 = require("./sync/v4/import");
const common_1 = require("./sync/common");
const fe_fpm_writer_1 = require("@sap-ux/fe-fpm-writer");
const package_json_1 = __importDefault(require("../package.json"));
const view_controller_generator_1 = require("./sync/v2/export/view-controller-generator");
const ftfs_1 = require("./ftfs");
const extensionLogger_1 = require("./extensionLogger");
const path_1 = require("path");
const fpmWriterMap = new Map([
    [ux_specification_types_1.CustomExtensionType.CustomPage, fe_fpm_writer_1.generateCustomPage],
    [ux_specification_types_1.CustomExtensionType.CustomColumn, fe_fpm_writer_1.generateCustomColumn],
    [ux_specification_types_1.CustomExtensionType.CustomSection, fe_fpm_writer_1.generateCustomSection],
    [ux_specification_types_1.CustomExtensionType.ObjectPage, fe_fpm_writer_1.generateObjectPage],
    [ux_specification_types_1.CustomExtensionType.ListReport, fe_fpm_writer_1.generateListReport],
    [ux_specification_types_1.CustomExtensionType.CustomAction, fe_fpm_writer_1.generateCustomAction],
    [ux_specification_types_1.CustomExtensionType.ActionMenu, fe_fpm_writer_1.generateActionMenu],
    [ux_specification_types_1.CustomExtensionType.CustomView, fe_fpm_writer_1.generateCustomView],
    [ux_specification_types_1.CustomExtensionType.ControllerExtension, fe_fpm_writer_1.generateControllerExtension],
    [ux_specification_types_1.CustomExtensionType.CustomSubSection, fe_fpm_writer_1.generateCustomSubSection],
    [ux_specification_types_1.CustomExtensionType.CustomFilterField, fe_fpm_writer_1.generateCustomFilter],
    [ux_specification_types_1.CustomExtensionType.CustomHeaderSection, fe_fpm_writer_1.generateCustomHeaderSection],
    [ux_specification_types_1.CustomExtensionType.CustomField, fe_fpm_writer_1.generateCustomField]
]);
var ftfs_2 = require("./ftfs");
Object.defineProperty(exports, "readApp", { enumerable: true, get: function () { return ftfs_2.readApp; } });
/**
 * Return the generic (project independent) JSON schema.
 *
 * @param specification - reference to spec
 * @param version - Fiori elements version (v2 or v4)
 * @param schemaType - type of the schema, e.g. ListReport, ObjectPage
 * @returns the generic schema for the given Fiori Elements version and schema type
 */
var schemaAccess_1 = require("./specification/schemaAccess");
Object.defineProperty(exports, "getGenericSchema", { enumerable: true, get: function () { return schemaAccess_1.getGenericSchema; } });
/**
 * API class to request and handle prompts for fpm-writer features.
 */
exports.prompts = {
    api: fe_fpm_writer_1.PromptsAPI,
    types: fe_fpm_writer_1.PromptsType
};
/**
 * Generate JSON schema for a given Fiori elements version and page type.
 *
 * @param generateParameters the input structure that contains the version, schema type, and parameters
 * @returns the app-specific schema for the given schema type and page
 */
function generateSchema(generateParameters) {
    let schema;
    if (generateParameters[ux_specification_types_1.FioriElementsVersion.v2]) {
        /**
         * Handle v2 schema generation
         */
        const v2Params = generateParameters[ux_specification_types_1.FioriElementsVersion.v2];
        if (v2Params[ux_specification_types_1.SchemaType.ListReport]) {
            const { genericSchema, entitySet, annotations, manifest, fragments, flex, logger } = v2Params[ux_specification_types_1.SchemaType.ListReport];
            schema = (0, generate_1.generateListReportInterfaceV2)(genericSchema, entitySet, annotations, manifest, fragments, flex, logger);
        }
        else if (v2Params[ux_specification_types_1.SchemaType.ObjectPage]) {
            const { genericSchema, entitySet, annotations, manifest, fragments, flex, logger } = v2Params[ux_specification_types_1.SchemaType.ObjectPage];
            schema = (0, generate_1.generateObjectPageInterfaceV2)(genericSchema, entitySet, annotations, manifest, fragments, flex, logger);
        }
        else if (v2Params[ux_specification_types_1.SchemaType.OverviewPage]) {
            const { genericSchema, manifest } = v2Params[ux_specification_types_1.SchemaType.OverviewPage];
            schema = (0, generate_1.generateOVPInterfaceV2)(genericSchema, manifest);
        }
        else if (v2Params[ux_specification_types_1.SchemaType.AnalyticalListPage]) {
            const { genericSchema, entitySet, annotations, manifest, fragments, flex, logger } = v2Params[ux_specification_types_1.SchemaType.AnalyticalListPage];
            schema = (0, generate_1.generateAnalyticalListPageInterfaceV2)(genericSchema, entitySet, annotations, manifest, fragments, flex, logger);
        }
    }
    else if (generateParameters[ux_specification_types_1.FioriElementsVersion.v4]) {
        /**
         * Handle v4 schema generation
         */
        const v4Params = generateParameters[ux_specification_types_1.FioriElementsVersion.v4];
        if (v4Params[ux_specification_types_1.SchemaType.ListReport]) {
            const { genericSchema, entitySet, annotations, logger, manifest, contextPath } = v4Params[ux_specification_types_1.SchemaType.ListReport];
            schema = (0, generate_2.generateListReportInterfaceV4)(ux_specification_types_1.TemplateType.ListReportObjectPageV4, genericSchema, entitySet, annotations, logger, manifest, contextPath);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.ObjectPage]) {
            const { genericSchema, entitySet, annotations, manifest, logger, fragments, contextPath, genericMacrosSchema } = v4Params[ux_specification_types_1.SchemaType.ObjectPage];
            schema = (0, generate_2.generateObjectPageInterfaceV4)(genericSchema, entitySet, annotations, manifest, logger, fragments, contextPath, genericMacrosSchema, generateParameters.featureToggles);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.AnalyticalListPage]) {
            const { genericSchema, entitySet, annotations, logger, manifest, contextPath } = v4Params[ux_specification_types_1.SchemaType.AnalyticalListPage];
            //As of now, the LR logic can also be applied to ALP
            schema = (0, generate_2.generateListReportInterfaceV4)(ux_specification_types_1.TemplateType.AnalyticalListPageV4, genericSchema, entitySet, annotations, logger, manifest, contextPath);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.FreestylePage]) {
            const { genericSchema, entitySet, annotations, logger, manifest } = v4Params[ux_specification_types_1.SchemaType.FreestylePage];
            schema = (0, generate_2.generateFreestyleInterfaceV4)(ux_specification_types_1.TemplateType.FreestylePageV4, genericSchema, annotations, manifest, entitySet, logger);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.FPMCustomPage]) {
            const { genericSchema, routingId, manifest, views, logger, annotations } = v4Params[ux_specification_types_1.SchemaType.FPMCustomPage];
            schema = (0, generate_2.generateFPMCustomPageInterfaceV4)(genericSchema, routingId, manifest, views, annotations, generateParameters.featureToggles, logger);
        }
    }
    return schema;
}
/**
 * Exports the whole config (src/*.json, src/pages/*.json) to flex changes and/or manifest settings.
 *
 * @param exportParameters - parameters to control the export
 * @returns a bundle of all actual objects of any artifact type
 */
function exportConfig(exportParameters) {
    let exportResults;
    const ui5Config = { ui5Version: exportParameters.ui5Version, layer: exportParameters.layer };
    if (exportParameters[ux_specification_types_1.FioriElementsVersion.v2]) {
        /**
         * Handle v2 export
         */
        const v2Params = exportParameters[ux_specification_types_1.FioriElementsVersion.v2];
        exportResults = (0, exportPage_1.exportPageV2)(v2Params, ui5Config);
    }
    else if (exportParameters[ux_specification_types_1.FioriElementsVersion.v4]) {
        /**
         * Handle v4 export
         */
        const v4Params = exportParameters[ux_specification_types_1.FioriElementsVersion.v4];
        exportResults = (0, export_1.exportPageV4)(v4Params, ui5Config);
    }
    return exportResults;
}
/**
 * Exports a part of the config, specified by a propertyPath, to flex changes and/or manifest settings.
 *
 * @param exportParameters - parameters to control the export
 * @returns a bundle of all changed objects of any artifact type
 */
function exportConfigEntityByPath(exportParameters) {
    let exportResults = ux_specification_types_1.defaultExportResult;
    const ui5Config = { ui5Version: exportParameters.ui5Version, layer: exportParameters.layer };
    if (exportParameters[ux_specification_types_1.FioriElementsVersion.v2]) {
        /**
         * Handle v2 export
         */
        const v2Params = exportParameters[ux_specification_types_1.FioriElementsVersion.v2];
        exportResults = (0, exportPage_1.exportConfigEntityByPathV2)(v2Params, ui5Config);
    }
    else if (exportParameters[ux_specification_types_1.FioriElementsVersion.v4]) {
        /**
         * Handle v4 export
         */
        const v4Params = exportParameters[ux_specification_types_1.FioriElementsVersion.v4];
        exportResults = (0, export_1.exportConfigEntityByPathV4)(v4Params, ui5Config);
    }
    return exportResults;
}
/**
 * Exports a part of the config, specified by a propertyPath, to flex changes and/or manifest settings.
 *
 * @param exportParameters - parameters to control the export
 * @returns a bundle of all changed objects of any artifact type
 */
function deleteConfigEntityByPath(exportParameters) {
    let exportResults = ux_specification_types_1.defaultExportResult;
    const ui5Config = { ui5Version: exportParameters.ui5Version, layer: exportParameters.layer };
    if (exportParameters[ux_specification_types_1.FioriElementsVersion.v2]) {
        /**
         * Handle v2 export
         */
        const v2Params = exportParameters[ux_specification_types_1.FioriElementsVersion.v2];
        exportResults = (0, exportPage_1.exportConfigEntityByPathV2)(v2Params, ui5Config, true);
    }
    else if (exportParameters[ux_specification_types_1.FioriElementsVersion.v4]) {
        /**
         * Handle v4 export
         */
        const v4Params = exportParameters[ux_specification_types_1.FioriElementsVersion.v4];
        exportResults = (0, export_1.exportConfigEntityByPathV4)(v4Params, ui5Config, true);
    }
    return exportResults;
}
/**
 * Imports manifest settings and flex changes to a config (src/*.json, src/pages/*.json).
 *
 * @param importParameters - parameters to control the import
 * @returns the generated config json file
 */
function importConfig(importParameters) {
    let config;
    if (importParameters[ux_specification_types_1.FioriElementsVersion.v2]) {
        /**
         * Handle v2 import
         */
        const v2Params = importParameters[ux_specification_types_1.FioriElementsVersion.v2];
        if (v2Params[ux_specification_types_1.SchemaType.ListReport]) {
            config = (0, pageAccess_1.createListReportConfig)(v2Params[ux_specification_types_1.SchemaType.ListReport]);
        }
        else if (v2Params[ux_specification_types_1.SchemaType.ObjectPage]) {
            config = (0, import_1.createObjectPageConfigV2)(v2Params[ux_specification_types_1.SchemaType.ObjectPage]);
        }
        else if (v2Params[ux_specification_types_1.SchemaType.OverviewPage]) {
            config = (0, import_1.createOverviewPageConfig)(v2Params[ux_specification_types_1.SchemaType.OverviewPage]);
        }
        else if (v2Params[ux_specification_types_1.SchemaType.AnalyticalListPage]) {
            config = (0, import_1.createAnalyticalListPageConfig)(v2Params[ux_specification_types_1.SchemaType.AnalyticalListPage]);
        }
    }
    else if (importParameters[ux_specification_types_1.FioriElementsVersion.v4]) {
        /**
         * Handle v4 import
         */
        const v4Params = importParameters[ux_specification_types_1.FioriElementsVersion.v4];
        if (v4Params[ux_specification_types_1.SchemaType.ListReport]) {
            config = (0, import_2.createListReportConfig)(v4Params[ux_specification_types_1.SchemaType.ListReport], ux_specification_types_1.SchemaType.ListReport);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.ObjectPage]) {
            config = (0, import_2.createObjectPageConfigV4)(v4Params[ux_specification_types_1.SchemaType.ObjectPage]);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.AnalyticalListPage]) {
            config = (0, import_2.createListReportConfig)(v4Params[ux_specification_types_1.SchemaType.AnalyticalListPage], ux_specification_types_1.SchemaType.AnalyticalListPage);
        }
        else if (v4Params[ux_specification_types_1.SchemaType.FPMCustomPage]) {
            config = (0, import_2.createCustomPageConfig)(v4Params[ux_specification_types_1.SchemaType.FPMCustomPage]);
        }
    }
    return config;
}
/**
 * Evaluates all project files and generates app schemas and configs out of it.
 *
 * @param importParameters - parameters to control the import; here: all relevant project files
 * @returns a list of files, comprising app schemas and configs
 */
async function importProject(importParameters) {
    return (0, common_1.importProjectSchemaAndConfig)(importParameters);
}
/**
 * Evaluates all project files and generates app schemas out of it, but no configs.
 *
 * @param importParameters - parameters to control the import; here: all relevant project files
 * @returns a list of files, comprising all app schemas
 */
async function importProjectSchema(importParameters) {
    return (0, common_1.importProjectSchema)(importParameters);
}
/**
 * Returns specification API version.
 *
 * @returns {ApiVersion} Object of API version.
 */
function getApiVersion() {
    return {
        version: '24',
        fpmWriter: '39',
        moduleVersion: package_json_1.default.version
    };
}
/**
 * Generates a V2 custom section view/controller artifact and writes it to the target folder.
 *
 * @param section - Custom section metadata used for file generation.
 * @param generateId - ID generator function used by view/fragment generation.
 * @param fsEditor - Optional mem-fs editor instance used to write generated content.
 * @returns The mem-fs editor instance containing the generated files.
 */
function generateCustomSectionV2(section, generateId, fsEditor) {
    try {
        const { name, isFragment, namespace } = section;
        const viewName = (0, view_controller_generator_1.getViewFileName)(name, isFragment);
        // get content for view/fragment
        const content = isFragment
            ? (0, view_controller_generator_1.getFragmentContent)(name, namespace, view_controller_generator_1.ViewTypes.XML, generateId, false)
            : (0, view_controller_generator_1.getViewContent)(name, namespace, view_controller_generator_1.ViewTypes.XML, generateId, false, false);
        fsEditor.write((0, path_1.join)(section.folders.targetFolder, viewName), content);
        return fsEditor;
    }
    catch (error) {
        (0, extensionLogger_1.log)(undefined, {
            severity: "error" /* LogSeverity.Error */,
            message: error instanceof Error ? error.message : 'Unknown error while generating custom section.'
        });
        throw error;
    }
}
/**
 * Calls the FPM Writer for generating a custom extension.
 *
 * @param customExtensionParams parameters for generating the custom extension
 * @returns Instance of mem-fs-editor
 */
async function generateCustomExtension(customExtensionParams) {
    const fpmWriterFunction = fpmWriterMap.get(customExtensionParams.customExtension);
    if (fpmWriterFunction && typeof fpmWriterFunction === 'function') {
        (0, fe_fpm_writer_1.enableFPM)(customExtensionParams.basePath, {});
        return fpmWriterFunction(customExtensionParams.basePath, customExtensionParams.data, customExtensionParams.fsEditor);
    }
    else if (customExtensionParams.customExtension === ux_specification_types_1.CustomExtensionType.CustomSectionV2) {
        const fsEditor = customExtensionParams.fsEditor ?? (0, ftfs_1.getFsInstance)();
        const generateId = await (0, fe_fpm_writer_1.createIdGenerator)({ fsEditor, basePath: customExtensionParams.basePath });
        return generateCustomSectionV2(customExtensionParams.data, generateId, fsEditor);
    }
    else {
        return;
    }
}
//# sourceMappingURL=api.js.map