import { jsx } from 'react/jsx-runtime';
import React from 'react';
import { DEFAULT_POSE, DEFAULT_RIG } from './rigTypes.js';
import NiceRiggedBody from './RiggedBody.js';
import { tweenPose, ease } from './poseOps.js';
import { rigPoseFor } from './choreoRigBridge.js';
import { solveFK } from './rigMath.js';
import { solveTwoBoneIK } from './ik.js';

const NiceRigPlayer = React.forwardRef(({ character, rig, pose, size, debug }, ref) => {
    var _a;
    const [current, setCurrent] = React.useState({ ...DEFAULT_POSE, ...(pose !== null && pose !== void 0 ? pose : {}) });
    const r = rig !== null && rig !== void 0 ? rig : DEFAULT_RIG;
    const S = Math.max(140, Math.floor((_a = size !== null && size !== void 0 ? size : character.size) !== null && _a !== void 0 ? _a : 180));
    const setNow = (p) => setCurrent({ ...DEFAULT_POSE, ...p });
    const aimRightHandAt = (x, y) => {
        const fk = solveFK(r, { ...DEFAULT_POSE, ...current }, S);
        const L1 = Math.hypot(fk.rUpperArm.x2 - fk.rUpperArm.x, fk.rUpperArm.y2 - fk.rUpperArm.y);
        const L2 = Math.hypot(fk.rForearm.x2 - fk.rForearm.x, fk.rForearm.y2 - fk.rForearm.y);
        const { upper, fore } = solveTwoBoneIK(fk.rShoulder.x, fk.rShoulder.y, L1, L2, x, y);
        setNow({ ...current, rUpperArm: upper, rForearm: fore });
    };
    const raiseCardIK = async (_score, durationMs = 450) => {
        const fk = solveFK(r, { ...DEFAULT_POSE, ...current }, S);
        const tx = fk.head.x2 + S * 0.12;
        const ty = fk.head.y2 - S * 0.05;
        const L1 = Math.hypot(fk.rUpperArm.x2 - fk.rUpperArm.x, fk.rUpperArm.y2 - fk.rUpperArm.y);
        const L2 = Math.hypot(fk.rForearm.x2 - fk.rForearm.x, fk.rForearm.y2 - fk.rForearm.y);
        const { upper, fore } = solveTwoBoneIK(fk.rShoulder.x, fk.rShoulder.y, L1, L2, tx, ty);
        const target = { ...current, rUpperArm: upper, rForearm: fore, head: 60 };
        await tweenPose(current, target, durationMs, setNow, ease.inOut);
    };
    React.useImperativeHandle(ref, () => ({
        setPose: setNow,
        tweenTo: (p, durationMs = 450) => tweenPose(current, { ...DEFAULT_POSE, ...p }, durationMs, setNow, ease.inOut),
        playChoreo: async (steps, defaultMs = 450) => {
            var _a, _b, _c, _d, _e;
            let cur = current;
            for (const s of steps) {
                if (s.pose === "raiseCard") {
                    await raiseCardIK(undefined, (_b = (_a = s.options) === null || _a === void 0 ? void 0 : _a.duration) !== null && _b !== void 0 ? _b : defaultMs);
                    cur = { ...current };
                }
                else {
                    const target = rigPoseFor(s.pose);
                    await tweenPose(cur, target, (_d = (_c = s.options) === null || _c === void 0 ? void 0 : _c.duration) !== null && _d !== void 0 ? _d : defaultMs, setNow, ease.inOut);
                    cur = target;
                }
                const hold = (_e = s.holdMs) !== null && _e !== void 0 ? _e : 0;
                if (hold > 0)
                    await new Promise((r) => setTimeout(r, hold));
            }
        },
        aimRightHandAt,
        raiseCardIK,
    }));
    return jsx(NiceRiggedBody, { character: character, rig: r, pose: current, size: S, debug: debug });
});

export { NiceRigPlayer as default };
//# sourceMappingURL=RigPlayer.js.map
