// ExportLayersWithMetadata_SaveDialog.jsx
#target photoshop

// ─────────────────────────────────────────────────────────────────
// JSON.stringify polyfill for Adobe ExtendScript
// Source: https://github.com/douglascrockford/JSON-js (trimmed)
// ─────────────────────────────────────────────────────────────────

if (typeof JSON === "undefined") {
    JSON = {};
}

(function () {
    function f(n) { return n < 10 ? '0' + n : n; }

    if (typeof Date.prototype.toJSON !== 'function') {
        Date.prototype.toJSON = function () {
            return isFinite(this.valueOf()) ?
                this.getUTCFullYear() + '-' +
                f(this.getUTCMonth() + 1) + '-' +
                f(this.getUTCDate()) + 'T' +
                f(this.getUTCHours()) + ':' +
                f(this.getUTCMinutes()) + ':' +
                f(this.getUTCSeconds()) + 'Z' : null;
        };
        String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function () {
            return this.valueOf();
        };
    }

    var cx = /[\u0000-\u001F\u007F-\u009F]/g,
        escapable = /[\\\"\u0000-\u001F\u007F-\u009F]/g,
        gap, indent,
        meta = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        rep;

    function quote(string) {
        escapable.lastIndex = 0;
        return escapable.test(string) ?
            '"' + string.replace(escapable, function (a) {
                var c = meta[a];
                return typeof c === 'string' ? c :
                    '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
            }) + '"' :
            '"' + string + '"';
    }

    function str(key, holder) {
        var i, k, v, length, mind = gap, partial, value = holder[key];
        if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
            value = value.toJSON(key);
        }
        switch (typeof value) {
        case 'string':
            return quote(value);
        case 'number':
            return isFinite(value) ? String(value) : 'null';
        case 'boolean':
            return String(value);
        case 'object':
            if (!value) return 'null';
            gap += indent;
            partial = [];
            if (Object.prototype.toString.apply(value) === '[object Array]') {
                length = value.length;
                for (i = 0; i < length; i += 1) {
                    partial[i] = str(i, value) || 'null';
                }
                v = partial.length === 0 ? '[]' :
                    gap ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
                          '[' + partial.join(',') + ']';
                gap = mind;
                return v;
            }
            for (k in value) {
                if (Object.prototype.hasOwnProperty.call(value, k)) {
                    v = str(k, value);
                    if (v) partial.push(quote(k) + (gap ? ': ' : ':') + v);
                }
            }
            v = partial.length === 0 ? '{}' :
                gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
                      '{' + partial.join(',') + '}';
            gap = mind;
            return v;
        }
    }

    if (typeof JSON.stringify !== 'function') {
        JSON.stringify = function (value, replacer, space) {
            var i;
            gap = '';
            indent = '';
            if (typeof space === 'number') {
                for (i = 0; i < space; i += 1) indent += ' ';
            } else if (typeof space === 'string') {
                indent = space;
            }
            rep = replacer;
            if (replacer && typeof replacer !== 'function' &&
                    (typeof replacer !== 'object' || typeof replacer.length !== 'number')) {
                throw new Error('JSON.stringify');
            }
            return str('', {'': value});
        };
    }
}());

// ─────────────────────────────────────────────────────────────────
// End JSON polyfill
// ─────────────────────────────────────────────────────────────────

/**
 * Main entry point
 */
function main() {
    if (!app.documents.length) {
        alert("Please open a document first.");
        return;
    }

    // Locate your exported PNGs folder
    var exportFolder = Folder(app.activeDocument.path + "/exported");
    if (!exportFolder.exists) {
        alert("Cannot find exported folder:\n" + exportFolder.fsName);
        return;
    }
    var doc = app.activeDocument,
        base = doc.name.replace(/\.[^\.]+$/, "").replace(/ /g, "-"),
         manifest = {
            CanvasWidth:  Math.round(doc.width.as("px")),
            CanvasHeight: Math.round(doc.height.as("px")),
            layers: []}

    /**
     * Recursively traverse layers and layer sets,
     * mirroring Photoshop’s 4-digit index + 's' for layer groups.
     */
    function traverse(layers, prefix) {
        // Separate counters for groups and art layers
        var artIndex = 0;
        var setIndex = 0;

        for (var i = 0; i < layers.length; i++) {
            var lyr = layers[i];
            var idx, seg;

            if (lyr.typename === "LayerSet") {
                idx = ("000" + setIndex).slice(-4);
                seg = idx + "s";
                setIndex++;
            } else {
                idx = ("000" + artIndex).slice(-4);
                seg = idx;
                artIndex++;
            }

            var newPrefix = prefix.concat([seg]);

            if (lyr.typename === "LayerSet") {
                // Capture group info
                manifest.layers.push({
                    pathSegments: newPrefix.slice(),
                    path: newPrefix,
                    name:    lyr.name,
                    type:   lyr.typename
                });
                traverse(lyr.layers, newPrefix);
            }
            else if (lyr.typename === "ArtLayer") {
                // Bounds in original canvas
                var b = lyr.bounds;
                var left   = b[0].as("px"),
                    top    = b[1].as("px"),
                    right  = b[2].as("px"),
                    bottom = b[3].as("px");

                // Build the exact PNG filename Photoshop created
                var safeName = lyr.name.replace(/[\\\/:*?"<>|]/g, "_").replace(/ /g, "-");
                var fileName = base + "_" + newPrefix.join("_") + "_" + safeName + ".png";

                manifest.layers.push({
                    pathSegments: newPrefix.slice(),
                    path: newPrefix,
                    name:    lyr.name,
                    fileName:     fileName,
                    bounds: {
                        left:   left,
                        top:    top,
                        width:  right  - left,
                        height: bottom - top
                    },
                    type:   lyr.typename
                });
            }
            else {
                manifest.layers.push({
                    pathSegments: newPrefix.slice(),
                    path: newPrefix,
                    layer: lyr,
                    type:   lyr.typename
                });
            }
        }
    }

    // Build manifest data
    traverse(doc.layers, []);

    // Prompt user where to save the JSON manifest
    var saveFile = File.saveDialog("Save JSON manifest as…", "*.json");
    if (!saveFile) {
        alert("Manifest save cancelled.");
        return;
    }

    // Write JSON
    saveFile.encoding = "UTF8";
    saveFile.open("w");
    var raw     = JSON.stringify(manifest, null, 2),
        decoded = raw.replace(/\\u([0-9A-Fa-f]{4})/g,
                    function(m,hex){ return String.fromCharCode(parseInt(hex,16)); });
    saveFile.write(decoded);
    saveFile.close();

    alert("✅ Manifest saved to:\n" + saveFile.fsName);
}

// Run the script
main();
