// Isosceles_RenameAndSerialize.jsx
// Code generated by ChatGPT-5.3
// Date: March 20, 2026
// Description: Batch rename the selected comps in the Project window with user-defined prefix, suffix, numbering, padding, and separator.

(function () {

    app.beginUndoGroup("Batch Rename Selected Comps");

    // =========================
    // UI DIALOG
    // =========================
    var win = new Window("dialog", "Rename & Serialize Comps");

    win.orientation = "column";
    win.alignChildren = "left";

    var g1 = win.add("group");
    g1.add("statictext", undefined, "Prefix:");
    var prefixInput = g1.add("edittext", undefined, "");
    prefixInput.characters = 20;

    var g2 = win.add("group");
    g2.add("statictext", undefined, "Suffix:");
    var suffixInput = g2.add("edittext", undefined, "");
    suffixInput.characters = 20;

    var g3 = win.add("group");
    g3.add("statictext", undefined, "Start Number:");
    var startInput = g3.add("edittext", undefined, "1");
    startInput.characters = 10;

    var g4 = win.add("group");
    g4.add("statictext", undefined, "Padding Digits:");
    var padInput = g4.add("edittext", undefined, "0");
    padInput.characters = 10;

    var g5 = win.add("group");
    g5.add("statictext", undefined, "Separator:");
    var sepInput = g5.add("edittext", undefined, "_");
    sepInput.characters = 10;

    var btnGroup = win.add("group");
    btnGroup.alignment = "right";

    var cancelBtn = btnGroup.add("button", undefined, "Cancel");
    var okBtn = btnGroup.add("button", undefined, "OK");

    var result = null;

    okBtn.onClick = function () {
        result = {
            prefix: prefixInput.text,
            suffix: suffixInput.text,
            start: parseInt(startInput.text, 10),
            pad: parseInt(padInput.text, 10),
            sep: sepInput.text
        };
        win.close();
    };

    cancelBtn.onClick = function () {
        result = null;
        win.close();
    };

    win.show();

    // =========================
    // EXIT IF CANCELLED
    // =========================
    if (!result) {
        app.endUndoGroup();
        return;
    }

    // =========================
    // SETTINGS
    // =========================
    var PREFIX = result.prefix || "";
    var SUFFIX = result.suffix || "";
    var START_NUMBER = isNaN(result.start) ? 1 : result.start;
    var PADDING = isNaN(result.pad) ? 0 : result.pad;
    var SEPARATOR = (result.sep !== undefined) ? result.sep : "_";

    var selection = app.project.selection;

    if (!selection || selection.length === 0) {
        alert("Please select at least one comp in the Project panel.");
        app.endUndoGroup();
        return;
    }

    var count = START_NUMBER;

    for (var i = 0; i < selection.length; i++) {
        var item = selection[i];

        if (item instanceof CompItem) {

            var numStr = count.toString();

            if (PADDING > 0) {
                while (numStr.length < PADDING) {
                    numStr = "0" + numStr;
                }
            }

            var newName = PREFIX;

            if (PREFIX !== "" && SEPARATOR !== "") {
                newName += SEPARATOR;
            }

            newName += numStr;

            if (SUFFIX !== "") {
                if (SEPARATOR !== "") {
                    newName += SEPARATOR;
                }
                newName += SUFFIX;
            }

            item.name = newName;
            count++;
        }
    }

    app.endUndoGroup();

})();