'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var index = require('../packages/ui/dist/index.js');
var i18n = require('../core/i18n.js');
var AnimatedPerson = require('./AnimatedPerson.js');
var characterTypes = require('./characterTypes.js');

const STORAGE_KEY = "nice2dev.animation-editor.character";
/* ---------- helpers ---------- */
function clampPalette(colors) {
    const c = (colors || []).slice(0, 3);
    if (c.length === 0)
        c.push("var(--anim-default-black, #000000)");
    while (c.length < 3)
        c.push(c[c.length - 1]);
    return c;
}
function ColorInputs({ value, onChange, }) {
    var _a;
    const base = clampPalette(value);
    const count = Math.min(3, Math.max(1, (_a = value === null || value === void 0 ? void 0 : value.length) !== null && _a !== void 0 ? _a : 1));
    const view = base.slice(0, count);
    return (jsxRuntime.jsxs("div", { className: "ntd-ae-color-inputs", children: [jsxRuntime.jsx("div", { className: "ntd-ae-colors", children: view.map((col, i) => (jsxRuntime.jsxs("label", { className: "ntd-ae-color-label", children: ["C", i + 1, jsxRuntime.jsx(index.NiceColorPicker, { value: col, onChange: (color) => {
                                const next = [...view];
                                next[i] = color;
                                onChange(next);
                            } })] }, i))) }), jsxRuntime.jsxs("div", { className: "ntd-ae-color-btns", children: [jsxRuntime.jsx(index.NiceButton, { variant: "ghost", size: "sm", disabled: count <= 1, onClick: () => onChange(view.slice(0, Math.max(1, count - 1))), children: "-" }), jsxRuntime.jsx(index.NiceButton, { variant: "ghost", size: "sm", disabled: count >= 3, onClick: () => onChange([...view, view[view.length - 1] || "var(--anim-default-black, #000000)"]), children: "+" })] })] }));
}
function Row({ title, children }) {
    return (jsxRuntime.jsxs("div", { className: "ntd-ae-row", children: [jsxRuntime.jsx("div", { className: "ntd-ae-row-title", children: title }), jsxRuntime.jsx("div", { className: "ntd-ae-row-content", children: children })] }));
}
function FeatureEditor({ title, feature, variants, onChange, }) {
    const options = variants.map((v) => ({ value: v, label: v }));
    return (jsxRuntime.jsxs(Row, { title: title, children: [jsxRuntime.jsx(index.NiceSelect, { className: "ntd-ae-select", value: feature.variant, onChange: (val) => onChange({ ...feature, variant: val }), options: options }), jsxRuntime.jsx(ColorInputs, { value: feature.colors, onChange: (colors) => onChange({ ...feature, colors }) })] }));
}
/* ---------- component ---------- */
function NiceAnimationEditor({ initialConfig, onChange, storageKey = STORAGE_KEY, onNotify, showJsonPanel = true, showPreview = true, className, style, }) {
    var _a, _b;
    const { t } = i18n.useNiceTranslation();
    const [json, setJson] = React.useState("");
    const [cfg, setCfg] = React.useState(() => {
        if (initialConfig)
            return initialConfig;
        if (storageKey) {
            try {
                const saved = localStorage.getItem(storageKey);
                return saved ? JSON.parse(saved) : characterTypes.DEFAULT_CHARACTER;
            }
            catch (_a) {
                return characterTypes.DEFAULT_CHARACTER;
            }
        }
        return characterTypes.DEFAULT_CHARACTER;
    });
    React.useEffect(() => {
        setJson(JSON.stringify(cfg, null, 2));
        if (storageKey) {
            try {
                localStorage.setItem(storageKey, JSON.stringify(cfg));
            }
            catch ( /* ignore */_a) { /* ignore */ }
        }
        onChange === null || onChange === void 0 ? void 0 : onChange(cfg);
    }, [cfg, storageKey, onChange]);
    const applyJson = () => {
        try {
            const parsed = JSON.parse(json);
            setCfg(parsed);
            onNotify === null || onNotify === void 0 ? void 0 : onNotify(t("characterEditor.jsonApplied", "JSON applied"), "success");
        }
        catch (_a) {
            onNotify === null || onNotify === void 0 ? void 0 : onNotify(t("characterEditor.invalidJson", "Invalid JSON"), "error");
        }
    };
    const downloadJson = () => {
        const blob = new Blob([JSON.stringify(cfg, null, 2)], { type: "application/json" });
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = `${cfg.name || "character"}.json`;
        a.click();
        URL.revokeObjectURL(url);
    };
    const randomizeColors = () => {
        const rand = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, "0")}`;
        const paint = (f) => ({ ...f, colors: [rand(), rand(), rand()] });
        setCfg({
            ...cfg,
            face: paint(cfg.face),
            hair: paint(cfg.hair),
            eyes: paint(cfg.eyes),
            nose: paint(cfg.nose),
            mouth: paint(cfg.mouth),
            outfit: paint(cfg.outfit),
            headwear: paint(cfg.headwear),
            prop: paint(cfg.prop),
        });
    };
    const updateFeature = (key, val) => {
        setCfg((prev) => ({ ...prev, [key]: val }));
    };
    return (jsxRuntime.jsxs("div", { className: `ntd-animation-editor ${className !== null && className !== void 0 ? className : ""}`, style: style, children: [jsxRuntime.jsxs("div", { className: "ntd-ae-controls", children: [jsxRuntime.jsx(Row, { title: t("characterEditor.name", "Name"), children: jsxRuntime.jsx(index.NiceTextInput, { className: "ntd-ae-input", value: cfg.name || "", onChange: (val) => setCfg({ ...cfg, name: val }), placeholder: t("characterEditor.namePlaceholder", "Character name") }) }), jsxRuntime.jsxs(Row, { title: t("characters.size", "Size"), children: [jsxRuntime.jsx(index.NiceSlider, { min: 140, max: 300, value: (_a = cfg.size) !== null && _a !== void 0 ? _a : 180, onChange: (val) => setCfg({ ...cfg, size: val }) }), jsxRuntime.jsxs("span", { className: "ntd-ae-size-value", children: [(_b = cfg.size) !== null && _b !== void 0 ? _b : 180, "px"] })] }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.faceShape", "Face"), feature: cfg.face, variants: characterTypes.VARIANTS.face, onChange: (v) => updateFeature("face", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.hairstyle", "Hair"), feature: cfg.hair, variants: characterTypes.VARIANTS.hair, onChange: (v) => updateFeature("hair", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.eyes", "Eyes"), feature: cfg.eyes, variants: characterTypes.VARIANTS.eyes, onChange: (v) => updateFeature("eyes", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.nose", "Nose"), feature: cfg.nose, variants: characterTypes.VARIANTS.nose, onChange: (v) => updateFeature("nose", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.mouth", "Mouth"), feature: cfg.mouth, variants: characterTypes.VARIANTS.mouth, onChange: (v) => updateFeature("mouth", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.clothing", "Outfit"), feature: cfg.outfit, variants: characterTypes.VARIANTS.outfit, onChange: (v) => updateFeature("outfit", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.headwear", "Headwear"), feature: cfg.headwear, variants: characterTypes.VARIANTS.headwear, onChange: (v) => updateFeature("headwear", v) }), jsxRuntime.jsx(FeatureEditor, { title: t("characters.prop", "Prop"), feature: cfg.prop, variants: characterTypes.VARIANTS.prop, onChange: (v) => updateFeature("prop", v) }), jsxRuntime.jsxs("div", { className: "ntd-ae-action-row", children: [jsxRuntime.jsxs(index.NiceButton, { className: "ntd-ae-btn", variant: "ghost", onClick: randomizeColors, children: ["\uD83C\uDFB2 ", t("characterEditor.randomizeColors", "Randomize colors")] }), jsxRuntime.jsxs(index.NiceButton, { className: "ntd-ae-btn", variant: "ghost", onClick: downloadJson, children: ["\u2B07 ", t("characters.downloadJson", "Download JSON")] })] }), showJsonPanel && (jsxRuntime.jsxs("div", { className: "ntd-ae-json-panel", children: [jsxRuntime.jsx("div", { className: "ntd-ae-json-label", children: t("characterEditor.jsonSection", "JSON") }), jsxRuntime.jsx(index.NiceTextArea, { className: "ntd-ae-textarea", value: json, onChange: (val) => setJson(val) }), jsxRuntime.jsxs("div", { className: "ntd-ae-json-actions", children: [jsxRuntime.jsx(index.NiceButton, { className: "ntd-ae-btn", variant: "primary", onClick: applyJson, children: t("characterEditor.applyJson", "Apply JSON") }), jsxRuntime.jsx(index.NiceButton, { className: "ntd-ae-btn", variant: "ghost", onClick: () => navigator.clipboard.writeText(JSON.stringify(cfg, null, 2)), children: t("characterEditor.copyToClipboard", "Copy") })] })] }))] }), showPreview && (jsxRuntime.jsxs("div", { className: "ntd-ae-preview", children: [jsxRuntime.jsx("div", { className: "ntd-ae-preview-label", children: t("characterEditor.preview", "Preview") }), jsxRuntime.jsx(AnimatedPerson, { character: cfg, score: 10, startPose: "idle" })] }))] }));
}
/** @deprecated Use NiceAnimationEditor */
const AnimationEditor = NiceAnimationEditor;

exports.AnimationEditor = AnimationEditor;
exports.NiceAnimationEditor = NiceAnimationEditor;
//# sourceMappingURL=AnimationEditor.js.map
