
(function () {
    var scriptFile = new File($.fileName);
    var scriptDir = scriptFile.parent;

    var scriptDirPath = scriptDir.fsName.replace(/\\/g, "/");

    var modules = [
        "lib/00_logger",
        "lib/06_versioning",
        "lib/01_json_polyfill",
        "lib/i18n/en_us",
        "lib/i18n/es_hn",
        "lib/07_i18n",
        "lib/05_security",
        "lib/02_settings"
    ];

    var isProduction = true;

    for (var i = 0; i < modules.length; i++) {
        var moduleBin = new File(scriptDirPath + "/" + modules[i] + ".jsxbin");
        var moduleJsx = new File(scriptDirPath + "/" + modules[i] + ".jsx");
        
        var moduleFile = null;
        if (moduleBin.exists) {
            moduleFile = moduleBin;
        } else if (moduleJsx.exists) {
            moduleFile = moduleJsx;
            isProduction = false; // Using .jsx = dev mode
        } else {
            alert("Error: Required module not found.\n\n" +
                "Missing: " + modules[i] + "\n\n" +
                "Tried:\n" +
                "  - " + moduleBin.fsName + "\n" +
                "  - " + moduleJsx.fsName + "\n\n" +
                "No JSXBIN files found. Please contact your administrator.");
            return;
        }

        try {
            $.evalFile(moduleFile);
        } catch (e) {
            alert("Error loading module: " + modules[i] + "\n\n" + e.toString());
            return;
        }
    }

    $.global.PRLB_PRODUCTION = isProduction;

    var securityResult = security.check();
    if (!securityResult.ok) {
        security.showActivationError(securityResult.machineID);
        if (typeof prlbLogger !== "undefined") {
            prlbLogger.log("SECURITY", "WARN", "Access denied (Place Garment) - Machine ID: " + securityResult.machineID);
        }
        return;
    }
    if (typeof prlbLogger !== "undefined") {
        prlbLogger.log("SECURITY", "INFO", "License check passed (Place Garment) - Machine ID: " + securityResult.machineID);
    }

    var mainBin = new File(scriptDirPath + "/PlaceGarment.jsxbin");
    var mainJsx = new File(scriptDirPath + "/PlaceGarment.jsx");
    
    var mainFile = null;
    if (mainBin.exists) {
        mainFile = mainBin;
    } else if (mainJsx.exists) {
        mainFile = mainJsx;
        $.global.PRLB_PRODUCTION = false; // Using .jsx = dev mode
    } else {
        alert("Error: PlaceGarment script not found.\n\n" +
            "Tried:\n" +
            "  - " + mainBin.fsName + "\n" +
            "  - " + mainJsx.fsName + "\n\n" +
            "No JSXBIN files found. Please contact your administrator.");
        return;
    }

    try {
        $.evalFile(mainFile);
    } catch (e) {
        alert("Error loading PlaceGarment script: " + e.toString());
        return;
    }
})();
