"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reviewOverwrites = void 0;
const utils_1 = require("../../utils");
const store_1 = require("../../store");
const config_1 = require("../../config");
const fs = require("node:fs");
const path = require("node:path");
const shell = require("node:child_process");
const logSymbols = require("log-symbols");
const chalk = require("chalk");
const emoji = require("node-emoji");
const reviewOverwrites = async (generateCommandState) => {
    // get existing origin files
    const originFiles = store_1.GlobalState.getValue('originFiles', [])
        .filter((file) => fs.existsSync(path.join(process.cwd(), file)));
    if (Array.isArray(originFiles) && originFiles.length > 0) {
        generateCommandState.command.log(`
********************************************
***              ATTENTION!              ***
********************************************`);
        generateCommandState.command.log('%s %s %s There are files that have not been overwritten because they were modified, the following origin files have been created.', logSymbols.warning, chalk.yellow.bold('WARNING'), emoji.get('small_red_triangle'));
        for (const originFile of originFiles) {
            generateCommandState.command.log(`%s ${originFile}`, emoji.get('question'));
        }
        let deleteOriginFiles = true;
        let fileToManage = '';
        let actionResponse = '';
        let indexToCompare = -1;
        // request if you want compare files
        if ((await utils_1.Prompter.promptManageOriginFiles()).hasCompareOriginFile) {
            fileToManage = (await utils_1.Prompter.promptSelectOriginFileToManage(originFiles)).fileToManage;
            indexToCompare = originFiles.indexOf(fileToManage);
            shell.exec(`code --diff ${fileToManage} ${fileToManage.replace('.origin', '')}`, (error, stdout, stderr) => { });
            while (actionResponse !== config_1.cliterConfig.compareActions.finish) {
                if (originFiles.length > 0) {
                    // select action
                    actionResponse = (await utils_1.Prompter.promptSelectManagementAction()).compareAction;
                    switch (actionResponse) {
                        case config_1.cliterConfig.compareActions.selectFile:
                            fileToManage = (await utils_1.Prompter.promptSelectOriginFileToManage(originFiles)).fileToManage;
                            break;
                        case config_1.cliterConfig.compareActions.deleteOriginAndLoadNext:
                            // delete origin file
                            fs.unlinkSync(fileToManage);
                            originFiles.splice(indexToCompare, 1);
                            indexToCompare = indexToCompare + 1 > originFiles.length ? indexToCompare = originFiles.length - 1 : indexToCompare;
                            if (indexToCompare === -1)
                                break;
                            // get next file
                            fileToManage = originFiles[indexToCompare];
                            break;
                        case config_1.cliterConfig.compareActions.ignore:
                            if (!fileToManage)
                                break;
                            fs.writeFileSync(fileToManage.replace('.origin', ''), (fileToManage.endsWith('.origin.graphql') ?
                                '# ignored file\r\n' :
                                (fileToManage.endsWith('.origin.html') ?
                                    '<!-- ignored file -->\r\n' :
                                    '// ignored file\r\n')) + fs.readFileSync(fileToManage.replace('.origin', ''), 'utf8'), 'utf8');
                            // delete origin file
                            fs.unlinkSync(fileToManage);
                            originFiles.splice(indexToCompare, 1);
                            indexToCompare = indexToCompare + 1 > originFiles.length ? indexToCompare = originFiles.length - 1 : indexToCompare;
                            if (indexToCompare === -1)
                                break;
                            // get next file
                            fileToManage = originFiles[indexToCompare];
                            break;
                    }
                    if (fileToManage && originFiles.length > 0)
                        shell.exec(`code --diff ${fileToManage} ${fileToManage.replace('.origin', '')}`, (error, stdout, stderr) => { });
                }
                else {
                    generateCommandState.command.log('[INFO] All files have been reviewed');
                    deleteOriginFiles = false;
                    break;
                }
            }
        }
        if (deleteOriginFiles) {
            utils_1.FileManager.deleteOriginFiles(process.cwd());
            generateCommandState.command.log(chalk.redBright.bold('[INFO] Origin files deleted!'));
        }
    }
};
exports.reviewOverwrites = reviewOverwrites;
