"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExportDriver = void 0;
class ExportDriver {
    /**
     * Create export in file
     *
     * @param sourceFile
     * @param modules
     * @param path
     */
    static createExportItems(sourceFile, path, items) {
        const exportPaths = ExportDriver.getExportPaths(sourceFile);
        // declare export * from ''
        if (!exportPaths.includes(path) && !items)
            sourceFile.addExportDeclaration({ moduleSpecifier: path });
        // return void to try declare export * from '' and already exists
        if (!items)
            return;
        const itemsToExport = ExportDriver.getUniqueExportItems(sourceFile, items);
        // need at less one item to export
        if (itemsToExport.length === 0)
            return;
        // if exist path add item to export
        if (exportPaths.includes(path)) {
            // check this export not exist yet
            const exportElement = sourceFile.getExportDeclaration(path);
            const itemsToExportChecked = [];
            for (const itemToExport of itemsToExport) {
                let existItem = false;
                for (const exportedElement of (exportElement === null || exportElement === void 0 ? void 0 : exportElement.getNamedExports()) ? exportElement === null || exportElement === void 0 ? void 0 : exportElement.getNamedExports() : []) {
                    if (exportedElement.getName() === itemToExport)
                        existItem = true;
                }
                if (!existItem)
                    itemsToExportChecked.push(itemToExport);
            }
            // add export after check that is not repeated
            if (itemsToExportChecked.length > 0)
                exportElement === null || exportElement === void 0 ? void 0 : exportElement.addNamedExports(itemsToExportChecked);
        }
        // create new export
        else {
            sourceFile.addExportDeclaration({
                namedExports: itemsToExport,
                moduleSpecifier: path
            });
        }
    }
    /**
     * From the items array, only non-repeating items are returned.
     *
     * @param sourceFile
     * @param moduleNames
     */
    static getUniqueExportItems(sourceFile, items) {
        // get export to avoid duplicities
        const exports = sourceFile.getExportDeclarations();
        // get names exported
        const itemsExported = exports.flatMap(i => i.getNamedExports().map(j => j.getName()));
        return items.filter(item => !itemsExported.includes(item));
    }
    /**
     * Return export paths from source
     *
     * @param sourceFile
     */
    static getExportPaths(sourceFile) {
        const exports = sourceFile.getExportDeclarations();
        const paths = [];
        for (const exportObj of exports) {
            const exportModule = exportObj.getModuleSpecifier();
            if (exportModule)
                paths.push(exportModule.getLiteralValue());
        }
        return paths;
    }
}
exports.ExportDriver = ExportDriver;
