"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.lintVueFiles = exports.wrapTemplate = exports.identifyTemplateNodes = exports.templateWrapperTag = exports.identifyVueFile = exports.getMandatoryFiles = exports.generateContentfulIds = void 0;
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const content_types_1 = require("@ewizardjs/content-types");
const system_settings_1 = require("@ewizardjs/system-settings");
const structure_api_1 = require("@ewizardjs/structure-api");
const eslint_1 = require("eslint");
const cheerio = require("cheerio");
const uuid_1 = require("uuid");
const fs_extra_1 = require("fs-extra");
const vueTemplateCompiler = require("vue-template-compiler");
const getContentType_1 = require("../Api/getContentType");
const generateContentfulIds = (contentPath, idAttribite) => __awaiter(void 0, void 0, void 0, function* () {
    const contentType = yield (0, getContentType_1.default)((type) => type)(contentPath);
    const filesToProcess = yield (0, exports.getMandatoryFiles)(contentPath, contentType);
    return Promise.all(filesToProcess.map((filePath) => (0, exports.identifyVueFile)(filePath, contentPath, idAttribite)));
});
exports.generateContentfulIds = generateContentfulIds;
const getMandatoryFiles = (contentPath, contentType) => __awaiter(void 0, void 0, void 0, function* () {
    const systemSettings = yield (0, system_settings_1.get)(contentPath);
    const mandatoryVueFiles = [];
    const appVue = (0, node_path_1.join)(contentPath, systemSettings.path.rootComponent);
    if (contentType === content_types_1.ContentTypes.EDETAILER) {
        const structureJson = yield (0, fs_extra_1.readJson)((0, node_path_1.join)(contentPath, systemSettings.path.structure.toString()));
        const slidesVueFiles = (0, structure_api_1.getFlatSlides)(structureJson)
            .filter((slide) => slide.template)
            .map((slide) => (0, node_path_1.join)(contentPath, slide.template));
        mandatoryVueFiles.push(...slidesVueFiles);
    }
    mandatoryVueFiles.unshift(appVue);
    return mandatoryVueFiles;
});
exports.getMandatoryFiles = getMandatoryFiles;
const identifyVueFile = (filePath, contentPath, idAttribite) => __awaiter(void 0, void 0, void 0, function* () {
    const fileContent = yield (0, promises_1.readFile)(filePath, 'utf-8');
    const templateData = vueTemplateCompiler.parseComponent(fileContent).template;
    const componentTemplate = templateData.content;
    const indentifiedTemplate = (0, exports.identifyTemplateNodes)(componentTemplate, idAttribite);
    const initialTemplate = fileContent.slice(templateData.start, templateData.end);
    const identifiedFileContent = fileContent.replace(initialTemplate, indentifiedTemplate);
    yield (0, promises_1.writeFile)(filePath, identifiedFileContent);
    yield (0, exports.lintVueFiles)(filePath, contentPath).catch((error) => identifiedFileContent);
    return Promise.resolve();
});
exports.identifyVueFile = identifyVueFile;
exports.templateWrapperTag = 'custom-wrapper';
const identifyTemplateNodes = (template, idAttribite, idProcessor) => {
    const $ = cheerio.load(wrapTemplate(template), { decodeEntities: false, lowerCaseTags: false });
    const allNodes = $(`${exports.templateWrapperTag}`).find(`*:not([${idAttribite}])`);
    allNodes.each((_, node) => {
        const proposedID = (0, uuid_1.v4)();
        return $(node).attr(idAttribite, idProcessor ? idProcessor(proposedID) : proposedID);
    });
    return $(`${exports.templateWrapperTag}`).html();
};
exports.identifyTemplateNodes = identifyTemplateNodes;
function wrapTemplate(templatecontent) {
    return `<${exports.templateWrapperTag}>${templatecontent}</${exports.templateWrapperTag}>`;
}
exports.wrapTemplate = wrapTemplate;
const lintVueFiles = (filePath, cwd) => __awaiter(void 0, void 0, void 0, function* () {
    const linter = new eslint_1.CLIEngine({
        cwd,
        fix: true,
        useEslintrc: true,
    });
    const results = yield linter.executeOnFiles([filePath]);
    yield eslint_1.CLIEngine.outputFixes(results);
    return linter;
});
exports.lintVueFiles = lintVueFiles;
//# sourceMappingURL=generateCotnentfulIds.js.map