"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayDriver = void 0;
const ts_morph_1 = require("ts-morph");
class ArrayDriver {
    /**
     * Add items in array
     *
     * @param sourceFile
     * @param item          item to add to array
     * @param array         name array or Array literal where will be added the item
     */
    static addArrayItems(sourceFile, items, array) {
        let arrayLiteralExpression;
        if (typeof array === 'string') {
            const routesDeclaration = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getVariableDeclaration(array);
            arrayLiteralExpression = routesDeclaration === null || routesDeclaration === void 0 ? void 0 : routesDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression);
            // if array not exist return void
            if (!arrayLiteralExpression)
                return;
        }
        else {
            arrayLiteralExpression = array;
        }
        const itemsToImport = ArrayDriver.getUniqueArrayItems(arrayLiteralExpression, items);
        if (itemsToImport.length > 0)
            arrayLiteralExpression === null || arrayLiteralExpression === void 0 ? void 0 : arrayLiteralExpression.addElements(itemsToImport, { useNewLines: true });
    }
    /**
     * Add item in array
     *
     * @param sourceFile
     * @param item          item to add to array
     * @param array         name array or Array literal where will be added the item
     */
    static addArrayItem(sourceFile, item, array, finder) {
        let arrayLiteralExpression;
        if (typeof array === 'string') {
            const routesDeclaration = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getVariableDeclaration(array);
            arrayLiteralExpression = routesDeclaration === null || routesDeclaration === void 0 ? void 0 : routesDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression);
        }
        else {
            arrayLiteralExpression = array;
        }
        if (!ArrayDriver.isDuplicateArrayValue(arrayLiteralExpression, item, finder))
            arrayLiteralExpression === null || arrayLiteralExpression === void 0 ? void 0 : arrayLiteralExpression.addElement(item, { useNewLines: true });
    }
    static isDuplicateArrayValue(array, item, finder) {
        // format string to avoid break spaces and extra white spaces
        const arrayItems = array === null || array === void 0 ? void 0 : array.getElements().map(i => i.getText()).map(j => j.replace(/(\r\n|\n|\r|\s)/gm, ''));
        if (finder)
            return finder(item, array);
        if (Array.isArray(arrayItems))
            return arrayItems.includes(item.replace(/(\r\n|\n|\r|\s)/gm, ''));
        return false;
    }
    /**
     * From the items array, only non-repeating items are returned.
     *
     * @param sourceFile
     * @param moduleNames
     */
    static getUniqueArrayItems(array, items) {
        // format string to avoid break spaces and extra white spaces
        const arrayItems = array === null || array === void 0 ? void 0 : array.getElements().map(i => i.getText()).map(j => j.replace(/(\r\n|\n|\r|\s)/gm, ''));
        // filter only items where is not in array
        return items.filter(item => !arrayItems.includes(item));
    }
}
exports.ArrayDriver = ArrayDriver;
