'use strict';

var rigTypes = require('./rig/rigTypes.js');
var rigMath = require('./rig/rigMath.js');

// ── Bone Mapping ────────────────────────────────────────────────
const BONE_PARENT_MAP = {
    pelvis: "root",
    spineLower: "pelvis",
    spineUpper: "spineLower",
    neck: "spineUpper",
    head: "neck",
    lShoulder: "spineUpper",
    lUpperArm: "lShoulder",
    lForearm: "lUpperArm",
    lHand: "lForearm",
    rShoulder: "spineUpper",
    rUpperArm: "rShoulder",
    rForearm: "rUpperArm",
    rHand: "rForearm",
    lThigh: "pelvis",
    lShin: "lThigh",
    lFoot: "lShin",
    rThigh: "pelvis",
    rShin: "rThigh",
    rFoot: "rShin",
};
function boneLength(rig, key, size) {
    var _a;
    const spec = rig[key];
    return ((_a = spec === null || spec === void 0 ? void 0 : spec.length) !== null && _a !== void 0 ? _a : 0) * size;
}
// ── Export ───────────────────────────────────────────────────────
/** Export rig and animations to Spine JSON format. */
function exportToSpine(animations, options = {}) {
    const { name = "nice2dev_character", size = 200, rig = rigTypes.DEFAULT_RIG, imagePath = "./images/", } = options;
    // Solve default pose for initial bone positions
    const fk = rigMath.solveFK(rig, rigTypes.DEFAULT_POSE, size);
    // Build bones
    const bones = rigTypes.BONE_ORDER.map((key) => {
        var _a;
        const solved = fk[key];
        const parent = BONE_PARENT_MAP[key];
        const parentSolved = parent ? fk[parent] : undefined;
        const x = solved ? (parentSolved ? solved.x - parentSolved.x : solved.x) : 0;
        const y = solved ? (parentSolved ? solved.y - parentSolved.y : solved.y) : 0;
        const rotation = (_a = rigTypes.DEFAULT_POSE[key]) !== null && _a !== void 0 ? _a : 0;
        return {
            name: key,
            parent: parent,
            length: boneLength(rig, key, size),
            x: Math.round(x * 100) / 100,
            y: Math.round(-y * 100) / 100, // Spine uses Y-up
            rotation: Math.round(rotation * 100) / 100,
        };
    });
    // Build slots (one per bone for attachment)
    const slots = rigTypes.BONE_ORDER.map((key) => ({
        name: `${key}_slot`,
        bone: key,
        attachment: `${key}_img`,
    }));
    // Build default skin
    const skinAttachments = {};
    for (const key of rigTypes.BONE_ORDER) {
        skinAttachments[`${key}_slot`] = {
            [`${key}_img`]: {
                type: "region",
                x: 0,
                y: 0,
                width: Math.round(boneLength(rig, key, size)),
                height: 8,
            },
        };
    }
    // Build animations
    const animationsOut = {};
    for (const anim of animations) {
        const sortedKf = [...anim.keyframes].sort((a, b) => a.time - b.time);
        const boneRotations = {};
        for (const key of rigTypes.BONE_ORDER) {
            const rotateKf = sortedKf.map((kf) => {
                var _a, _b, _c;
                return ({
                    time: Math.round(kf.time) / 1000, // ms → seconds
                    angle: ((_b = (_a = kf.pose[key]) !== null && _a !== void 0 ? _a : rigTypes.DEFAULT_POSE[key]) !== null && _b !== void 0 ? _b : 0) - ((_c = rigTypes.DEFAULT_POSE[key]) !== null && _c !== void 0 ? _c : 0),
                    curve: "stepped",
                });
            });
            boneRotations[key] = { rotate: rotateKf };
        }
        animationsOut[anim.name] = { bones: boneRotations };
    }
    return {
        skeleton: {
            hash: generateHash(name),
            spine: "4.1",
            x: 0,
            y: 0,
            width: size,
            height: size,
            images: imagePath,
        },
        bones,
        slots,
        skins: { default: skinAttachments },
        animations: animationsOut,
    };
}
/** Serialize Spine data to JSON string. */
function spineToJSON(data) {
    return JSON.stringify(data, null, 2);
}
/** Create a downloadable Blob for the Spine JSON. */
function spineToBlob(data) {
    return new Blob([spineToJSON(data)], { type: "application/json" });
}
// ── Helpers ─────────────────────────────────────────────────────
function generateHash(input) {
    let hash = 0;
    for (let i = 0; i < input.length; i++) {
        const char = input.charCodeAt(i);
        hash = ((hash << 5) - hash) + char;
        hash |= 0;
    }
    return Math.abs(hash).toString(36);
}

exports.exportToSpine = exportToSpine;
exports.spineToBlob = spineToBlob;
exports.spineToJSON = spineToJSON;
//# sourceMappingURL=spineExport.js.map
