"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSuitFile = exports.skipSuit = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const prettier_1 = __importDefault(require("prettier"));
const parser_babel_1 = __importDefault(require("prettier/parser-babel"));
const cachePath = process.env['HOME'] + '/.webf_test_tools.json';
;
const GENERATED_LABEL = '/*auto generated*/';
let testMap = {};
if (fs_1.default.existsSync(cachePath)) {
    testMap = JSON.parse(fs_1.default.readFileSync(cachePath, { encoding: 'utf-8' }));
}
setInterval(() => {
    fs_1.default.writeFileSync(cachePath, JSON.stringify(testMap));
}, 50000);
process.on('beforeExit', () => {
    fs_1.default.writeFileSync(cachePath, JSON.stringify(testMap));
});
function suitTemplate(suit) {
    let specCode = suit.specs.map(spec => specTemplate(spec));
    return `${GENERATED_LABEL}\ndescribe('${suit.name}', () => {
${specCode.join('\n')}
});`;
}
function specTemplate(spec) {
    return spec.content;
}
function skipSuit(targetDir, spec) {
    if (!testMap.hasOwnProperty(spec.suitName)) {
        return false;
    }
    let suit = testMap[spec.suitName];
    let existSpecIndex = suit.specs.findIndex(item => item.name == spec.name);
    if (existSpecIndex == -1) {
        return false;
    }
    suit.specs.splice(existSpecIndex, 1);
    writeSuit(targetDir, suit, spec);
    return true;
}
exports.skipSuit = skipSuit;
function generateSuitFile(targetDir, spec, code) {
    if (!testMap.hasOwnProperty(spec.suitName)) {
        testMap[spec.suitName] = {
            name: spec.suitName,
            specs: []
        };
    }
    let suit = testMap[spec.suitName];
    let existSpecIndex = suit.specs.findIndex(item => item.name == spec.name);
    spec.content = code;
    if (existSpecIndex >= 0) {
        suit.specs[existSpecIndex] = spec;
    }
    else {
        testMap[spec.suitName].specs.push(spec);
    }
    return writeSuit(targetDir, suit, spec);
}
exports.generateSuitFile = generateSuitFile;
function writeSuit(targetDir, suit, spec) {
    let targetFilePath = path_1.default.join(targetDir, spec.suitName + '.ts').toLowerCase();
    let source = suitTemplate(suit);
    if (fs_1.default.existsSync(targetFilePath)) {
        let existSource = fs_1.default.readFileSync(targetFilePath, { encoding: 'utf-8' }).split('\n');
        if (existSource[0] !== GENERATED_LABEL) {
            console.log(chalk_1.default.yellow(`found exist test file: ${targetFilePath}`));
            return `found exist test file: ${targetFilePath}`;
        }
    }
    fs_1.default.writeFileSync(targetFilePath, prettier_1.default.format(source, {
        singleQuote: true,
        tabWidth: 2,
        parser: 'babel',
        plugins: [parser_babel_1.default]
    }));
    console.log(chalk_1.default.green(`generate file: ${targetFilePath}`));
    return `generate file: ${targetFilePath}`;
}
