"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.macrosCorrection = void 0;
exports.applyCorrections = applyCorrections;
const types_1 = require("./types");
const ux_specification_types_1 = require("@sap/ux-specification-types");
exports.macrosCorrection = {
    'sap.fe.macros.Page': {
        defaultAggregation: 'items'
    },
    'sap.fe.macros.Table': {
        defaultAggregation: 'columns',
        removeAggregations: ['noData'],
        aggregations: [
            {
                name: 'actions',
                singularName: 'action',
                type: 'sap.fe.macros.table.Action',
                cardinality: '0..n',
                visibility: types_1.UI5Visibility.public,
                description: 'Table actions',
                methods: [],
                linkEnabled: true
            },
            {
                name: 'columns',
                singularName: 'column',
                type: 'sap.fe.macros.table.Column',
                cardinality: '0..n',
                visibility: types_1.UI5Visibility.public,
                description: 'Table columns',
                methods: [],
                linkEnabled: true
            }
        ],
        properties: [
            {
                name: 'title',
                description: 'The text that will be displayed for table title',
                group: 'Misc',
                methods: [],
                visibility: types_1.UI5Visibility.public,
                types: [
                    {
                        value: 'string'
                    }
                ],
                bindable: true,
                defaultValue: ''
            }
        ]
    },
    'sap.fe.macros.table.Table': {
        removeAggregations: ['noData']
    },
    'sap.fe.macros.TreeTable': {
        removeAggregations: ['noData']
    },
    'sap.fe.macros.table.Column': {
        removeAggregations: ['exportSettings']
    },
    'sap.fe.macros.table.TreeTable': {
        removeAggregations: ['noData']
    },
    'sap.fe.macros.ActionGroup': {
        defaultAggregation: 'actions',
        aggregations: [
            {
                name: 'actions',
                singularName: 'action',
                type: 'sap.fe.macros.table.Action',
                cardinality: '0..n',
                visibility: types_1.UI5Visibility.public,
                description: 'Table actions',
                methods: [],
                linkEnabled: true
            }
        ],
        properties: [
            {
                defaultValue: '',
                description: 'The text that will be displayed for this action group',
                group: 'Misc',
                methods: [],
                name: 'text',
                types: [
                    {
                        value: 'string'
                    }
                ],
                visibility: types_1.UI5Visibility.public
            },
            {
                defaultValue: '',
                description: 'Defines where this action group should be placed relative to the defined anchor',
                group: 'Misc',
                methods: [],
                name: 'placement',
                types: [
                    {
                        value: 'string'
                    }
                ],
                visibility: types_1.UI5Visibility.public
            },
            {
                defaultValue: '',
                description: 'Reference to the key of another action or action group already displayed in the toolbar to properly place this one',
                group: 'Misc',
                methods: [],
                name: 'anchor',
                types: [
                    {
                        value: 'string'
                    }
                ],
                visibility: types_1.UI5Visibility.public
            },
            {
                defaultValue: '',
                description: 'Unique identifier of the ActionGroup',
                group: 'Misc',
                methods: [],
                name: 'key',
                types: [
                    {
                        value: 'string'
                    }
                ],
                visibility: types_1.UI5Visibility.public
            }
        ],
        metadata: {
            kind: types_1.SYMBOL_KIND_CLASS,
            name: 'sap.fe.macros.ActionGroup',
            basename: 'ActionGroup',
            resource: '',
            module: '',
            static: true,
            visibility: types_1.UI5Visibility.public,
            description: 'Definition of a actions group to be used inside the table toolbar',
            displayName: 'sap.fe.macros.ActionGroup',
            title: 'class sap.fe.macros.ActionGroup',
            subTitle: ''
        }
    },
    'sap.fe.macros.FilterBar': {
        aggregations: [
            {
                cardinality: '0..n',
                description: '<p>Aggregate filter fields of the FilterBar building block</p>',
                linkEnabled: true,
                methods: [],
                name: 'filterFields',
                singularName: 'filterFields',
                type: 'sap.fe.macros.FilterField',
                visibility: types_1.UI5Visibility.public
            }
        ]
    },
    'sap.fe.macros.richtexteditor.ButtonGroup': {
        properties: [
            {
                name: 'visible',
                extend: true,
                // Correct from string to boolean while fix is not applied from fe team
                type: 'boolean'
            }
        ]
    }
};
const placementEnum = [ux_specification_types_1.v4.Placement.After, ux_specification_types_1.v4.Placement.Before];
const enumsEntries = {
    'sap.fe.macros.FilterField': {
        placement: placementEnum
    },
    'sap.fe.macros.table.Action': {
        placement: placementEnum
    },
    'sap.fe.macros.table.Column': {
        placement: placementEnum
    },
    'sap.fe.macros.Table': {
        variantManagement: ['None', 'Page', 'Control']
    }
};
/**
 * Method returns node with "properties" and "aggregation" for passed symbol.
 * If node does not contains "properties" or "aggregation", then it prepares empty objects.
 *
 * @param {ClassSymbol | TypedefSymbol} symbol Symbol from SAPUI5 api.
 * @returns {APICorrection} Correction object which contains "properties" and "aggregation" properties.
 */
function getNode(symbol) {
    let node;
    if (symbol.kind === types_1.SYMBOL_KIND_CLASS) {
        if (!symbol[types_1.CLASS_SYMBOL_METADATA_PROPERTY]) {
            symbol[types_1.CLASS_SYMBOL_METADATA_PROPERTY] = {
                aggregations: [],
                properties: [],
                stereotype: ''
            };
        }
        node = symbol[types_1.CLASS_SYMBOL_METADATA_PROPERTY];
    }
    else {
        node = symbol;
    }
    if (!node.aggregations) {
        node.aggregations = [];
    }
    if (!node.properties) {
        node.properties = [];
    }
    return node;
}
/**
 * Method applies correction for passed node.
 *
 * @param {APICorrection} node Original node to update with corrections.
 * @param {APICorrection} correction Correction to apply - "properties" and "aggregation" are applied.
 */
function applyCorrectionForNode(node, correction) {
    for (const aggregation of correction.aggregations || []) {
        if (!node.aggregations.some((existingAggregation) => existingAggregation.name === aggregation.name)) {
            node.aggregations.push(aggregation);
        }
    }
    for (const aggregation of correction.removeAggregations || []) {
        const aggregationIndex = node.aggregations.findIndex((existingAggregation) => existingAggregation.name === aggregation);
        if (aggregationIndex !== -1) {
            node.aggregations.splice(aggregationIndex);
        }
    }
    for (const property of correction.properties || []) {
        const propertyIndex = node.properties.findIndex((existingProperty) => existingProperty.name === property.name);
        // add missing properties or extend existing
        if (propertyIndex !== -1 && 'extend' in property && property.extend) {
            node.properties[propertyIndex] = {
                ...node.properties[propertyIndex],
                ...property
            };
        }
        else if (propertyIndex === -1) {
            node.properties.push(property);
        }
    }
    if (node.defaultAggregation !== correction.defaultAggregation) {
        node.defaultAggregation = correction.defaultAggregation;
    }
}
/**
 * Method applies correction for enum entries.
 *
 * @param {MacrosAPI} api "sap.fe.macros" api object.
 */
function applyCorrectionForEnums(api) {
    for (const name in enumsEntries) {
        const symbol = api.symbols.find((apiSymbol) => apiSymbol.name === name);
        if (![types_1.SYMBOL_KIND_TYPEDEF, types_1.SYMBOL_KIND_CLASS].includes(symbol?.kind)) {
            continue;
        }
        const properties = enumsEntries[name];
        for (const name in properties) {
            let property;
            if (symbol?.kind === types_1.SYMBOL_KIND_TYPEDEF) {
                property = symbol.properties.find((ui5Property) => ui5Property.name === name);
            }
            else if (symbol?.kind === types_1.SYMBOL_KIND_CLASS) {
                property = symbol[types_1.CLASS_SYMBOL_METADATA_PROPERTY].properties.find((ui5Property) => ui5Property.name === name);
            }
            if (property) {
                property.allowedValues = properties[name];
            }
        }
    }
}
/**
 * Method applies missing aggregations and properties for "sap.fe.macros" api object.
 * We notice that api object of "sap.fe.macros" is missing some aggregation and properties information
 * - in result we apply missing data on top of received api object.
 *
 * @param {MacrosAPI} api "sap.fe.macros" api object.
 */
function applyCorrections(api) {
    // Apply missing classes
    for (const name in exports.macrosCorrection) {
        const symbol = api.symbols.find((apiSymbol) => apiSymbol.name === name);
        if (!symbol && exports.macrosCorrection[name].metadata) {
            api.symbols.push({
                ...exports.macrosCorrection[name].metadata
            });
        }
    }
    // Apply missing aggregation and properties
    for (const symbol of api.symbols) {
        const correction = exports.macrosCorrection[symbol.name];
        if (!correction || symbol.kind === types_1.SYMBOL_KIND_NAMESPACE) {
            continue;
        }
        const node = getNode(symbol);
        applyCorrectionForNode(node, correction);
    }
    // Enum entries
    applyCorrectionForEnums(api);
}
//# sourceMappingURL=corrections.js.map