"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePwshModule = void 0;
const plugin_create_inline_properties_1 = require("../plugins/plugin-create-inline-properties");
const plugin_tweak_model_1 = require("../plugins/plugin-tweak-model");
const plugin_tweak_model_azure_v2_1 = require("../plugins/plugin-tweak-model-azure-v2");
const create_commands_v2_1 = require("../plugins/create-commands-v2");
const cs_namer_v2_1 = require("../plugins/cs-namer-v2");
const ps_namer_v2_1 = require("../plugins/ps-namer-v2");
const llcsharp_v2_1 = require("../plugins/llcsharp-v2");
const powershell_v2_1 = require("../plugins/powershell-v2");
const modifiers_v2_1 = require("../plugins/modifiers-v2");
const plugin_tweak_m4_model_1 = require("../plugins/plugin-tweak-m4-model");
const model_state_1 = require("./model-state");
const tsp_host_1 = require("./tsp-host");
const uri_1 = require("@azure-tools/uri");
const path_1 = require("path");
async function clearOutputFiles(state) {
    if (await state.getValue('clear-output-folder', false)) {
        const outputFolderConfig = await state.getValue('output-folder', './generated');
        const outputFolder = (0, uri_1.createFolderUri)((0, path_1.isAbsolute)(outputFolderConfig) ? outputFolderConfig : (0, path_1.join)((0, path_1.resolve)(), outputFolderConfig));
        const psd1 = await state.getValue('psd1');
        const customFolder = await state.getValue('custom-cmdlet-folder');
        const testFolder = await state.getValue('test-folder');
        const docsFolder = await state.getValue('docs-folder');
        const examplesFolder = await state.getValue('examples-folder');
        const resourcesFolder = await state.getValue('resources-folder');
        const uxFolder = await state.getValue('ux-folder');
        const readme = './README.md';
        const tempTypeSpecFiles = './TempTypeSpecFiles';
        const tspLocation = './tsp-location.yaml';
        const protectFiles = new Set();
        protectFiles.add(psd1);
        protectFiles.add(customFolder);
        protectFiles.add(testFolder);
        protectFiles.add(docsFolder);
        protectFiles.add(examplesFolder);
        protectFiles.add(resourcesFolder);
        protectFiles.add(uxFolder);
        protectFiles.add(readme);
        protectFiles.add(tempTypeSpecFiles);
        protectFiles.add(tspLocation);
        await (0, uri_1.clearFolder)(outputFolder, [...protectFiles].map((each) => (0, uri_1.resolveUri)(outputFolder, each)));
    }
    return;
}
async function generatePwshModule(pwshModel, emitterOptions) {
    const tspService = new tsp_host_1.TspHostImpl(emitterOptions);
    const state = await new model_state_1.ModelState(tspService);
    // clear output folder if set, but protect certain files
    await clearOutputFiles(state);
    state.model = pwshModel;
    await (0, plugin_tweak_m4_model_1.tweakModelForTsp)(state);
    await (0, plugin_tweak_model_1.tweakModelV2)(state);
    await (0, plugin_tweak_model_azure_v2_1.tweakModel)(state);
    await (await new create_commands_v2_1.Inferrer(state).init()).createCommands();
    await (0, plugin_create_inline_properties_1.createVirtuals)(state);
    await (0, cs_namer_v2_1.nameStuffRight)(state);
    await (0, ps_namer_v2_1.tweakModel)(state);
    await (0, modifiers_v2_1.tweakModelForTsp)(state);
    await (0, llcsharp_v2_1.llcsharpV2)(tspService, state);
    await (0, powershell_v2_1.powershellV2)(tspService, state);
    return;
}
exports.generatePwshModule = generatePwshModule;
//# sourceMappingURL=tsp-generator.js.map