import { jsxs, jsx } from 'react/jsx-runtime';
import React from 'react';
import { NiceButton as We } from '../packages/ui/dist/index.js';
import NiceAnimatedPerson from './AnimatedPerson.js';
import { DEFAULT_CHARACTER } from './characterTypes.js';
import { runRounds, seq } from './choreoDSL.js';

const rnd = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, "0")}`;
function recolor(f, n) {
    const colors = new Array(Math.max(1, Math.min(3, n))).fill(0).map(rnd);
    return { ...f, colors };
}
function makeAudience(n) {
    const arr = [];
    for (let i = 0; i < n; i++) {
        arr.push({
            ...DEFAULT_CHARACTER,
            name: `Fan ${i + 1}`,
            size: 150 + Math.round(Math.random() * 40) - 20,
            outfit: recolor(DEFAULT_CHARACTER.outfit, 3),
            hair: recolor(DEFAULT_CHARACTER.hair, 2),
            headwear: Math.random() < 0.3
                ? recolor(DEFAULT_CHARACTER.headwear, 2)
                : { variant: "none", colors: ["var(--anim-stroke, #111)"] },
            prop: Math.random() < 0.25
                ? { variant: "star", colors: [rnd(), rnd(), "var(--anim-stroke, #111)"] }
                : { variant: "none", colors: ["var(--anim-stroke, #111)"] },
        });
    }
    return arr;
}
/** Grid of animated audience members with wave/cheer actions. */
const NiceAudience = ({ count = 36, columns = 12 }) => {
    const people = React.useMemo(() => makeAudience(count), [count]);
    const refs = React.useRef([]);
    refs.current = [];
    const actors = () => refs.current.filter(Boolean);
    const cheer = async () => {
        const a = actors();
        if (!a.length)
            return;
        await runRounds(a, [seq().jump(250).celebrate(500), seq().waveHand(600), seq().danceB(450)], 60);
    };
    const wave = async () => {
        const a = actors();
        if (!a.length)
            return;
        await Promise.all(new Array(Math.ceil(a.length / columns)).fill(0).map((_, row) => runRounds(a.slice(row * columns, row * columns + columns), [seq().waveHand(500)], 120 * row)));
    };
    return (jsxs("div", { style: { display: "flex", flexDirection: "column", gap: 8 }, children: [jsx("div", { style: {
                    display: "grid",
                    gap: 8,
                    gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))`,
                }, children: people.map((c, i) => (jsx(NiceAnimatedPerson, { character: c, score: 10, startPose: "idle", onReady: (fx) => (refs.current[i] = fx) }, i))) }), jsxs("div", { style: { display: "flex", gap: 8 }, children: [jsx(We, { variant: "ghost", size: "sm", onClick: wave, children: "Wave" }), jsx(We, { variant: "ghost", size: "sm", onClick: cheer, children: "Cheer" })] })] }));
};

export { NiceAudience as default };
//# sourceMappingURL=Audience.js.map
