#target illustrator
#targetengine "main"

(function () {
    function getScriptDirectory() {
        var dir = null;

        if ($.global.btScript_MyLocation && $.global.btScript_MyLocation !== "") {
            try {
                var btFile = new File($.global.btScript_MyLocation);
                if (btFile.exists) {
                    dir = btFile.parent.fsName;
                }
            } catch (e1) { }
        }

        if (!dir && $.global.startupLocation && $.global.startupLocation !== "") {
            try {
                var startupFolder = new Folder($.global.startupLocation);
                if (startupFolder.exists) {
                    dir = startupFolder.fsName;
                }
            } catch (e2) { }
        }

        if (!dir && $.fileName && $.fileName !== "") {
            try {
                var scriptFile = new File($.fileName);
                if (scriptFile.exists) {
                    dir = scriptFile.parent.fsName;
                }
            } catch (e3) { }
        }

        if (!dir) return null;
        return dir.replace(/\\/g, "/");
    }

    var scriptDirPath = getScriptDirectory();
    if (!scriptDirPath) {
        alert("Error: Cannot determine script directory for ProofLabPanelLauncher.jsx.\n\n" +
              "Please ensure the launcher is loaded from a valid path.");
        return;
    }

    var panelBin = new File(scriptDirPath + "/ProofLabPanel.jsxbin");
    var panelJsx = new File(scriptDirPath + "/ProofLabPanel.jsx");
    
    var panelFile = null;
    var isProduction = true;
    
    if (panelBin.exists) {
        panelFile = panelBin;
    } else if (panelJsx.exists) {
        panelFile = panelJsx;
        isProduction = false; // Using .jsx = dev mode
    } else {
        alert("Error: ProofLabPanel script not found.\n\n" +
            "Tried:\n" +
            "  - " + panelBin.fsName + "\n" +
            "  - " + panelJsx.fsName + "\n\n" +
            "No JSXBIN files found. Please contact your administrator.");
        return;
    }

    $.global.PRLB_PRODUCTION = isProduction;
    $.global.PRLB_PANEL_DIR = scriptDirPath;
    $.global.PRLB_SCRIPT_DIR = scriptDirPath;

    try {
        var needsJsonPolyfill = false;
        try {
            needsJsonPolyfill = (typeof JSON === "undefined" || !JSON ||
                typeof JSON.parse !== "function" || typeof JSON.stringify !== "function");
        } catch (_eJsonCheck) {
            needsJsonPolyfill = true;
        }
        if (needsJsonPolyfill) {
            var jsonBin = new File(scriptDirPath + "/lib/01_json_polyfill.jsxbin");
            var jsonJsx = new File(scriptDirPath + "/lib/01_json_polyfill.jsx");
            if (jsonBin.exists) {
                $.evalFile(jsonBin);
            } else if (jsonJsx.exists) {
                $.evalFile(jsonJsx);
            }
        }

        if (!($.global && $.global.settingsManager)) {
            var settingsBin = new File(scriptDirPath + "/lib/02_settings.jsxbin");
            var settingsJsx = new File(scriptDirPath + "/lib/02_settings.jsx");
            if (settingsBin.exists) {
                $.evalFile(settingsBin);
            } else if (settingsJsx.exists) {
                $.evalFile(settingsJsx);
            }
            if (typeof settingsManager !== "undefined" && settingsManager) {
                $.global.settingsManager = settingsManager;
            }
        }
    } catch (_eSettings) { }

    try {
        $.evalFile(panelFile);
    } catch (e) {
        alert("Error loading ProofLabPanel: " + e.toString());
    }
})();
