// assemble.jsx — Photoshop 자동 조립 (manifest.json 기반)
(function() {
    var scriptFile = new File($.fileName);
    var folder = scriptFile.parent;
    var manifestFile = new File(folder + "/manifest.json");
    if (!manifestFile.exists) { alert("manifest.json 없음"); return; }
    manifestFile.open("r");
    var raw = manifestFile.read();
    manifestFile.close();
    var manifest = eval("(" + raw + ")");
    var doc = app.documents.add(manifest.width, manifest.height, 72, "matgo_mockup",
                                NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
    for (var i = 0; i < manifest.layers.length; i++) {
        var L = manifest.layers[i];
        var pngFile = new File(folder + "/" + L.file);
        if (!pngFile.exists) continue;
        try {
            var idPlc = stringIDToTypeID("placeEvent");
            var desc = new ActionDescriptor();
            desc.putPath(charIDToTypeID("null"), pngFile);
            desc.putEnumerated(charIDToTypeID("FTcs"), charIDToTypeID("QCSt"), charIDToTypeID("Qcsa"));
            executeAction(idPlc, desc, DialogModes.NO);
            var newLayer = doc.activeLayer;
            newLayer.rasterize(RasterizeType.ENTIRELAYER);
            newLayer.name = L.name;
            newLayer.opacity = L.opacity;
        } catch (e) {}
    }
    alert("레이어 " + manifest.layers.length + "개 import 완료.");
})();
