"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.config = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const xplat_1 = require("@nstudio/xplat");
exports.config = {
    addHelperFiles,
    additionalSupport: imports,
};
function addHelperFiles(options) {
    // executed from 'helpers' directory so ensure _files are loaded relative
    const xplatFolderName = xplat_1.XplatHelpers.getXplatFoldername('nativescript');
    return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./imports/_files`), [
        schematics_1.template(Object.assign(Object.assign({}, options), xplat_1.getDefaultTemplateOptions())),
        schematics_1.move(`libs/xplat/${xplatFolderName}/utils/src/lib`),
    ])));
}
function imports(helperChains, options) {
    return (tree, context) => {
        const xplatFolderName = xplat_1.XplatHelpers.getXplatFoldername('nativescript');
        let pathRef = `libs/xplat/${xplatFolderName}/utils/src/lib/@nativescript/*`;
        // update root tsconfig
        helperChains.push(xplat_1.updateTsConfig(tree, (tsConfig) => {
            const updates = {};
            updates[`@nativescript/*`] = [pathRef];
            if (tsConfig) {
                if (!tsConfig.compilerOptions) {
                    tsConfig.compilerOptions = {};
                }
                tsConfig.compilerOptions.paths = Object.assign(Object.assign({}, (tsConfig.compilerOptions.paths || {})), updates);
            }
        }));
        // update all {N} app tsconfig's
        const appsDir = tree.getDir('apps');
        const appFolders = appsDir.subdirs;
        pathRef = `../../${pathRef}`;
        // update {N} apps and configs
        for (const dir of appFolders) {
            // console.log(dir);
            if (dir.indexOf('nativescript-') === 0 ||
                dir.indexOf('-nativescript') === 0) {
                const appDir = `${appsDir.path}/${dir}`;
                // console.log('appDir:', appDir);
                helperChains.push(xplat_1.updateTsConfig(tree, (tsConfig) => {
                    const updates = {};
                    updates[`@nativescript/*`] = [pathRef];
                    if (tsConfig) {
                        if (!tsConfig.compilerOptions) {
                            tsConfig.compilerOptions = {};
                        }
                        tsConfig.compilerOptions.paths = Object.assign(Object.assign({}, (tsConfig.compilerOptions.paths || {})), updates);
                    }
                }, null, `${appDir}/`));
            }
        }
        return tree;
    };
}
