'use strict';

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

// ── Conversion ──────────────────────────────────────────────────
function msToFrame(ms, fps) {
    return Math.round(ms * fps / 1000);
}
function createPositionKeyframes(keyframes, rig, bone, size, fps) {
    return keyframes.map((kf, i) => {
        var _a, _b;
        const fk = rigMath.solveFK(rig, kf.pose, size);
        const solved = fk[bone];
        return {
            i: i < keyframes.length - 1 ? { x: 0.667, y: 1 } : undefined,
            o: i < keyframes.length - 1 ? { x: 0.333, y: 0 } : undefined,
            t: msToFrame(kf.time, fps),
            s: [(_a = solved === null || solved === void 0 ? void 0 : solved.x) !== null && _a !== void 0 ? _a : 0, (_b = solved === null || solved === void 0 ? void 0 : solved.y) !== null && _b !== void 0 ? _b : 0],
            e: i < keyframes.length - 1
                ? (() => {
                    var _a, _b;
                    const nextFk = rigMath.solveFK(rig, keyframes[i + 1].pose, size);
                    const nextSolved = nextFk[bone];
                    return [(_a = nextSolved === null || nextSolved === void 0 ? void 0 : nextSolved.x) !== null && _a !== void 0 ? _a : 0, (_b = nextSolved === null || nextSolved === void 0 ? void 0 : nextSolved.y) !== null && _b !== void 0 ? _b : 0];
                })()
                : undefined,
        };
    });
}
function createBoneShapeLayer(bone, index, keyframes, rig, size, fps, totalFrames) {
    const posKf = createPositionKeyframes(keyframes, rig, bone, size, fps);
    return {
        ddd: 0,
        ind: index,
        ty: 4, // shape layer
        nm: bone,
        sr: 1,
        ks: {
            o: { a: 0, k: 100 }, // opacity
            r: { a: 0, k: 0 }, // rotation
            p: { a: 1, k: posKf }, // animated position
            a: { a: 0, k: [0, 0, 0] }, // anchor
            s: { a: 0, k: [100, 100, 100] }, // scale
        },
        ao: 0,
        shapes: [
            {
                ty: "el", // ellipse
                d: 1,
                s: { a: 0, k: [4, 4] }, // joint dot size
                p: { a: 0, k: [0, 0] },
                nm: `${bone}_joint`,
            },
            {
                ty: "fl", // fill
                c: { a: 0, k: [0.2, 0.5, 1, 1] },
                o: { a: 0, k: 100 },
                r: 1,
                bm: 0,
                nm: `${bone}_fill`,
            },
        ],
        ip: 0,
        op: totalFrames,
        st: 0,
        bm: 0,
    };
}
// ── Public API ──────────────────────────────────────────────────
/**
 * Export a sequence of RigPose keyframes to Lottie JSON format.
 * Returns a Lottie animation object that can be serialized with JSON.stringify().
 */
function exportToLottie(keyframes, options = {}) {
    const { name = "Nice2Dev Animation", width = 400, height = 400, fps = 30, rig = rigTypes.DEFAULT_RIG, size = 200, author = "Nice2Dev", } = options;
    if (keyframes.length === 0) {
        throw new Error("At least one keyframe is required for Lottie export");
    }
    const sorted = [...keyframes].sort((a, b) => a.time - b.time);
    const totalDurationMs = sorted[sorted.length - 1].time - sorted[0].time;
    const totalFrames = msToFrame(totalDurationMs, fps) + 1;
    const layers = rigTypes.BONE_ORDER.map((bone, idx) => createBoneShapeLayer(bone, idx + 1, sorted, rig, size, fps, totalFrames));
    return {
        v: "5.12.2",
        fr: fps,
        ip: 0,
        op: totalFrames,
        w: width,
        h: height,
        nm: name,
        ddd: 0,
        assets: [],
        layers,
        meta: { a: author, g: "@nice2dev/ui-graphics" },
    };
}
/** Serialize Lottie animation to a JSON string. */
function lottieToJSON(animation) {
    return JSON.stringify(animation);
}
/** Create a downloadable Blob for the Lottie animation. */
function lottieToBlob(animation) {
    return new Blob([lottieToJSON(animation)], { type: "application/json" });
}
/**
 * Convert a choreography sequence (pose name + duration) to Lottie keyframes.
 * Utility for bridging the choreography system to Lottie export.
 */
function choreoToLottieKeyframes(poses) {
    const keyframes = [];
    let time = 0;
    for (const { pose, durationMs } of poses) {
        keyframes.push({ time, pose });
        time += durationMs;
    }
    // Add final frame
    if (poses.length > 0) {
        keyframes.push({ time, pose: poses[poses.length - 1].pose });
    }
    return keyframes;
}

exports.choreoToLottieKeyframes = choreoToLottieKeyframes;
exports.exportToLottie = exportToLottie;
exports.lottieToBlob = lottieToBlob;
exports.lottieToJSON = lottieToJSON;
//# sourceMappingURL=lottieExport.js.map
