"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GLOBAL_COST = void 0;
exports.jsComplexityByPath = jsComplexityByPath;
exports.baseComplexityByPath = baseComplexityByPath;
exports.refreshComplexity = refreshComplexity;
const esbuild = require("esbuild");
const fs_1 = require("fs");
const fs_extra_1 = require("fs-extra");
const glob = require("glob");
const path_1 = require("path");
const path_2 = require("../path");
const complexity_1 = require("../spec/complexity");
const _specs_1 = require("../system/_specs");
const removeLastSegment_1 = require("../util/removeLastSegment");
function getBundleLength(path) {
    const result = esbuild.buildSync({
        entryPoints: [path],
        bundle: true,
        splitting: false,
        minify: false,
        platform: 'node',
        minifyWhitespace: false,
        minifyIdentifiers: false,
        minifySyntax: false,
        sourcemap: false,
        // target: ['chrome58', 'firefox57', 'safari11', 'edge16'],
        // outfile: 'out.js',
        write: false,
    });
    const text = result.outputFiles[0].text;
    return text.length;
}
const PATH_SRC_CLASS_UNIT = (0, path_1.join)(path_2.PATH_SRC, 'Class/Unit/index.ts');
const UNIT_LENGTH = getBundleLength(PATH_SRC_CLASS_UNIT);
function jsComplexityByPath(spec_path) {
    const bundleLength = getBundleLength(spec_path);
    return bundleLength / UNIT_LENGTH;
}
function baseComplexityByPath(folder_path) {
    let complexity = 0;
    const index = `${folder_path}/index.ts`;
    const has_index = (0, fs_extra_1.pathExistsSync)(index);
    if (has_index) {
        complexity += jsComplexityByPath(index);
    }
    const component = `${folder_path}/Component.ts`;
    const has_component = (0, fs_extra_1.pathExistsSync)(component);
    if (has_component) {
        complexity += jsComplexityByPath(component);
    }
    return complexity;
}
exports.GLOBAL_COST = 9;
function refreshComplexity(specs, cwd) {
    glob
        .sync(`**/**/index.ts`, {
        cwd,
    })
        .map((path) => (0, removeLastSegment_1.removeLastSegment)(path))
        .forEach((_) => {
        const folder_path = `${cwd}/${_}`;
        const spec_path = `${folder_path}/spec.json`;
        const spec = (0, fs_extra_1.readJSONSync)(spec_path);
        const { id, metadata: { globals = [] }, } = spec;
        let complexity;
        complexity = baseComplexityByPath(folder_path);
        let global_complexity = 0;
        for (const global_id of globals) {
            global_complexity += exports.GLOBAL_COST;
        }
        complexity += complexity + global_complexity;
        complexity = Math.round(complexity);
        console.log(folder_path, complexity);
        spec.metadata = spec.metadata || {};
        spec.metadata.complexity = complexity;
        specs[id] = spec;
        (0, fs_1.writeFileSync)(spec_path, JSON.stringify(spec, null, 2));
    });
    glob
        .sync(`**/**/spec.json`, {
        cwd,
    })
        .map((path) => (0, removeLastSegment_1.removeLastSegment)(path))
        .forEach((_) => {
        const folder_path = `${cwd}/${_}`;
        const spec_path = `${folder_path}/spec.json`;
        const spec = (0, fs_extra_1.readJSONSync)(spec_path);
        const { id, base } = spec;
        if (base) {
            //
        }
        else {
            const complexity = (0, complexity_1.treeComplexity)(specs, spec, {});
            console.log(folder_path, complexity);
            spec.metadata = spec.metadata || {};
            spec.metadata.complexity = complexity;
            specs[id] = spec;
            (0, fs_1.writeFileSync)(spec_path, JSON.stringify(spec, null, 2));
        }
    });
}
const specs = { ..._specs_1.default };
refreshComplexity(specs, path_2.PATH_SRC_SYSTEM);
//# sourceMappingURL=complexity.js.map