"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerResourceGenerator = registerResourceGenerator;
const vscode = __importStar(require("vscode"));
const fs_1 = require("fs");
const path = __importStar(require("path"));
let extensionTemplatePath = "";
let includeFeature = false; // ✅ boolean flag
const InputData = {
    resourceCode: {
        title: "Resource Code",
        placeholder: "Used for the folder name: GOD, GSR, SELAPI, ...",
        nextInput: "resourceName",
        value: ""
    },
    resourceName: {
        title: "Resource name",
        placeholder: "B_Jobs, Phone, esx_garage, ...",
        nextInput: "description",
        value: ""
    },
    description: {
        title: "Resource description",
        placeholder: "This resource is ...",
        nextInput: "",
        value: ""
    }
};
const startInput = "resourceCode";
// ================= ENTRY FUNCTION =================
function registerResourceGenerator(context) {
    extensionTemplatePath = path.join(context.extensionPath, "template");
    const disposable = vscode.commands.registerCommand("kj4lxc.fivem.generate-resource", (uri) => {
        loadInputs(uri?.fsPath);
    });
    context.subscriptions.push(disposable);
}
// ================= INPUT FLOW =================
function loadInputs(resourcePath) {
    for (const key in InputData) {
        const field = InputData[key];
        field.input = vscode.window.createInputBox();
        field.input.title = field.title;
        field.input.placeholder = field.placeholder;
        field.input.onDidAccept(() => {
            if (field.input.value.trim().length > 0) {
                field.value = field.input.value;
                field.input.hide();
                if (field.nextInput) {
                    InputData[field.nextInput].input.show();
                }
                else {
                    askIncludeFeature(resourcePath); // ✅ NEW STEP
                }
            }
            else {
                vscode.window.showErrorMessage(`You need to complete this input (${field.title}).`);
            }
        });
        if (key === startInput) {
            field.input.show();
        }
    }
}
// ================= BOOLEAN PROMPT =================
async function askIncludeFeature(resourcePath) {
    const result = await vscode.window.showQuickPick(["Yes", "No"], {
        placeHolder: "Include UI Elements?"
    });
    if (!result) {
        vscode.window.showWarningMessage("Operation cancelled.");
        return;
    }
    includeFeature = result === "Yes";
    startProcessing(resourcePath);
}
// ================= PROCESSING =================
function startProcessing(basePath) {
    if (!basePath) {
        vscode.window.showErrorMessage("No folder selected.");
        return;
    }
    const folder = getGeneratedFolder("");
    const resourcePath = path.join(basePath, InputData.resourceCode.value);
    (0, fs_1.mkdirSync)(resourcePath);
    writeFolder(resourcePath, folder);
    vscode.window.showInformationMessage(`You just created a new FiveM Lua Resource named '${InputData.resourceName.value}'.`);
}
function Folder(url) {
    return {
        url,
        files: {},
        folders: {}
    };
}
// ================= TEMPLATE PARSER =================
function getGeneratedFolder(url) {
    const folder = Folder(url);
    // ✅ SWITCH TEMPLATE BASED ON BOOLEAN
    const baseTemplatePath = includeFeature
        ? path.join(extensionTemplatePath, "withFeature")
        : path.join(extensionTemplatePath, "base");
    const files = (0, fs_1.readdirSync)(path.join(baseTemplatePath, url), {
        withFileTypes: true
    });
    for (const file of files) {
        if (file.isDirectory()) {
            folder.folders[file.name] = getGeneratedFolder(path.join(url, file.name));
        }
        else {
            let content = (0, fs_1.readFileSync)(path.join(baseTemplatePath, url, file.name), "utf-8");
            for (const key in InputData) {
                content = content.replace(new RegExp(`\\$\\{${key}\\}`, "g"), InputData[key].value);
            }
            folder.files[file.name] = content;
        }
    }
    return folder;
}
// ================= FILE WRITER =================
function writeFolder(url, folder) {
    for (const key in folder.files) {
        (0, fs_1.writeFileSync)(path.join(url, key), folder.files[key], "utf-8");
    }
    for (const key in folder.folders) {
        const subFolderPath = path.join(url, key);
        (0, fs_1.mkdirSync)(subFolderPath);
        writeFolder(subFolderPath, folder.folders[key]);
    }
}
//# sourceMappingURL=resourceGenerator.js.map