'use strict';

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

/** Convert degrees to radians. */
const deg2rad = (deg) => (deg * Math.PI) / 180;
/** Linear interpolation of angles with wrap-around. */
function lerpAngle(a, b, t) {
    const da = ((b - a + 540) % 360) - 180;
    return a + da * Math.max(0, Math.min(1, t));
}
/** Forward kinematics — compute world-space positions for all bones. */
function solveFK(rig, pose, sizePx) {
    var _a, _b, _c, _d;
    const out = {};
    const N = (v, fb) => (typeof v === "number" ? v : fb);
    for (const key of rigTypes.BONE_ORDER) {
        const spec = rig[key];
        const parent = spec.parent ? out[spec.parent] : undefined;
        let x, y;
        if (!parent) {
            x = N((_a = spec.offset) === null || _a === void 0 ? void 0 : _a[0], 0.5) * sizePx;
            y = N((_b = spec.offset) === null || _b === void 0 ? void 0 : _b[1], 0.58) * sizePx;
        }
        else {
            x = parent.x2 + N((_c = spec.offset) === null || _c === void 0 ? void 0 : _c[0], 0) * sizePx;
            y = parent.y2 + N((_d = spec.offset) === null || _d === void 0 ? void 0 : _d[1], 0) * sizePx;
        }
        const ang = N(pose[key], parent ? parent.angle : 0);
        const L = N(spec.length, 0) * sizePx;
        const dx = Math.cos(deg2rad(ang)) * L;
        const dy = Math.sin(deg2rad(ang)) * L;
        out[key] = { x, y, x2: x + dx, y2: y + dy, angle: ang };
    }
    return out;
}

exports.deg2rad = deg2rad;
exports.lerpAngle = lerpAngle;
exports.solveFK = solveFK;
//# sourceMappingURL=rigMath.js.map
