'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var GameAsset2dEditor_module = require('./GameAsset2dEditor.module.css.js');
var useGameAssetEditor = require('./useGameAssetEditor.js');
var index = require('../packages/ui/dist/index.js');

/* ────────────────────────────────────────────────────────────────
   MENU BAR
──────────────────────────────────────────────────────────────── */
const MenuBar = ({ api }) => {
    const fileInputRef = React.useRef(null);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.menuBar, children: [jsxRuntime.jsx("h3", { children: "\uD83C\uDFAE Game Asset Editor" }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.menuGroup, children: [jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: () => api.newProject('New Project'), children: "\uD83D\uDCC4 New" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: () => { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, children: "\uD83D\uDCC2 Open" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: api.saveProject, children: "\uD83D\uDCBE Save" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.spacer }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.menuGroup, children: [jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: api.undo, disabled: !api.canUndo, children: "\u21A9 Undo" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: api.redo, disabled: !api.canRedo, children: "\u21AA Redo" })] }), jsxRuntime.jsx("input", { ref: fileInputRef, type: "file", accept: ".json", style: { display: 'none' }, onChange: e => {
                    var _a;
                    const file = (_a = e.target.files) === null || _a === void 0 ? void 0 : _a[0];
                    if (file)
                        api.loadProject(file);
                    e.target.value = '';
                } })] }));
};
/* ────────────────────────────────────────────────────────────────
   MODE TABS
──────────────────────────────────────────────────────────────── */
const ModeTabs = ({ api }) => {
    const modes = [
        { id: 'tilemap', label: 'Tile Map', icon: '🗺️' },
        { id: 'spritesheet', label: 'Sprite Sheet', icon: '🖼️' },
        { id: 'animation', label: 'Animation', icon: '🎬' },
        { id: 'effects', label: 'Effects', icon: '✨' },
    ];
    return (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.modeTabs, children: modes.map(mode => (jsxRuntime.jsxs(index.NiceButton, { className: `${GameAsset2dEditor_module.modeTab} ${api.editorState.mode === mode.id ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.mode === mode.id ? 'primary' : 'ghost', onClick: () => api.setMode(mode.id), children: [mode.icon, " ", mode.label] }, mode.id))) }));
};
/* ────────────────────────────────────────────────────────────────
   TILE MAP TOOLBAR
──────────────────────────────────────────────────────────────── */
const TileMapToolbar = ({ api }) => {
    const tools = [
        { id: 'select', icon: '⬚', label: 'Select' },
        { id: 'brush', icon: '✏', label: 'Brush' },
        { id: 'eraser', icon: '⌫', label: 'Eraser' },
        { id: 'fill', icon: '🪣', label: 'Fill' },
        { id: 'rectangle', icon: '▢', label: 'Rectangle' },
        { id: 'line', icon: '╱', label: 'Line' },
        { id: 'eyedropper', icon: '💧', label: 'Eyedropper' },
    ];
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.toolbar, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.toolGroup, children: tools.map(tool => (jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.toolBtn} ${api.editorState.tileMapTool === tool.id ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.tileMapTool === tool.id ? 'primary' : 'ghost', size: "sm", onClick: () => api.setTileMapTool(tool.id), title: tool.label, children: tool.icon }, tool.id))) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.spacer }), jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.toolBtn} ${api.editorState.showGrid ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.showGrid ? 'primary' : 'ghost', size: "sm", onClick: api.toggleGrid, title: "Toggle Grid", children: "\u229E" }), jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.toolBtn} ${api.editorState.showCollision ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.showCollision ? 'primary' : 'ghost', size: "sm", onClick: api.toggleCollision, title: "Show Collision", children: "\u25C7" }), jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.toolBtn} ${api.editorState.snapToGrid ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.snapToGrid ? 'primary' : 'ghost', size: "sm", onClick: api.toggleSnap, title: "Snap to Grid", children: "\u22A1" })] }));
};
/* ────────────────────────────────────────────────────────────────
   SPRITE SHEET TOOLBAR
──────────────────────────────────────────────────────────────── */
const SpriteSheetToolbar = ({ api }) => {
    const [sliceWidth, setSliceWidth] = React.useState(16);
    const [sliceHeight, setSliceHeight] = React.useState(16);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.toolbar, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.toolGroup, children: [jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.toolBtn} ${api.editorState.spriteSheetTool === 'select' ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.spriteSheetTool === 'select' ? 'primary' : 'ghost', size: "sm", onClick: () => api.setSpriteSheetTool('select'), title: "Select", children: "\u2B1A" }), jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.toolBtn} ${api.editorState.spriteSheetTool === 'slice' ? GameAsset2dEditor_module.active : ''}`, variant: api.editorState.spriteSheetTool === 'slice' ? 'primary' : 'ghost', size: "sm", onClick: () => api.setSpriteSheetTool('slice'), title: "Manual Slice", children: "\u2702" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.spacer }), jsxRuntime.jsxs("label", { style: { fontSize: 11, color: '#8892a8' }, children: ["Frame size:", jsxRuntime.jsx(index.NiceNumberInput, { value: sliceWidth, onChange: val => setSliceWidth(val !== null && val !== void 0 ? val : 16), style: { width: 40, margin: '0 4px' }, className: GameAsset2dEditor_module.panelInput }), "\u00D7", jsxRuntime.jsx(index.NiceNumberInput, { value: sliceHeight, onChange: val => setSliceHeight(val !== null && val !== void 0 ? val : 16), style: { width: 40, marginLeft: 4 }, className: GameAsset2dEditor_module.panelInput })] }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.btnSecondary, variant: "ghost", onClick: () => api.autoSlice(sliceWidth, sliceHeight), style: { marginLeft: 8 }, children: "Auto Slice" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.btnSecondary, variant: "ghost", onClick: api.detectSprites, style: { marginLeft: 4 }, children: "Detect Sprites" })] }));
};
/* ────────────────────────────────────────────────────────────────
   TILE PALETTE (LEFT PANEL)
──────────────────────────────────────────────────────────────── */
const TilePalette = ({ api }) => {
    const fileInputRef = React.useRef(null);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.leftPanel, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelHeader, children: [jsxRuntime.jsx("span", { children: "Tiles" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: () => { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, children: "+ Add" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: api.project.tileSets.length === 0 ? (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.emptyState, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "No tiles yet" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.btnSecondary, variant: "ghost", onClick: () => { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, children: "Import Tiles" })] })) : (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.tilePalette, children: api.project.tileSets.map(tile => (jsxRuntime.jsx("div", { className: `${GameAsset2dEditor_module.tileItem} ${api.editorState.selectedTileId === tile.id ? GameAsset2dEditor_module.selected : ''}`, onClick: () => api.selectTile(tile.id), title: tile.name, children: jsxRuntime.jsx("img", { src: tile.thumbnail, alt: tile.name }) }, tile.id))) })) }), jsxRuntime.jsx("input", { ref: fileInputRef, type: "file", accept: "image/*", style: { display: 'none' }, onChange: e => {
                    var _a;
                    const file = (_a = e.target.files) === null || _a === void 0 ? void 0 : _a[0];
                    if (file)
                        api.importTileSet(file);
                    e.target.value = '';
                } })] }));
};
/* ────────────────────────────────────────────────────────────────
   LAYERS PANEL (RIGHT PANEL)
──────────────────────────────────────────────────────────────── */
const LayersPanel = ({ api }) => {
    const activeMap = api.project.tileMaps.find(m => m.id === api.editorState.activeTileMapId);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.rightPanel, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelHeader, children: [jsxRuntime.jsx("span", { children: "Layers" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: api.addLayer, disabled: !activeMap, children: "+ Add" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: activeMap ? (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.layersList, children: [...activeMap.layers].reverse().map(layer => (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.layerItem} ${api.editorState.activeLayerId === layer.id ? GameAsset2dEditor_module.selected : ''}`, onClick: () => api.selectLayer(layer.id), children: [jsxRuntime.jsx(index.NiceButton, { className: `${GameAsset2dEditor_module.layerVisibility} ${!layer.visible ? GameAsset2dEditor_module.hidden : ''}`, variant: "ghost", size: "sm", onClick: e => {
                                    e.stopPropagation();
                                    api.toggleLayerVisibility(layer.id);
                                }, children: layer.visible ? '👁' : '○' }), jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: layer.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [Math.round(layer.opacity * 100), "%"] })] }, layer.id))) })) : (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyState, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "No tile map selected" }) })) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelHeader, children: [jsxRuntime.jsx("span", { children: "Maps" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: () => api.createTileMap('New Map', 32, 32), children: "+ New" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: api.project.tileMaps.map(map => (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.layerItem} ${api.editorState.activeTileMapId === map.id ? GameAsset2dEditor_module.selected : ''}`, onClick: () => api.selectTileMap(map.id), children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: map.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [map.width, "\u00D7", map.height] })] }, map.id))) })] }));
};
/* ────────────────────────────────────────────────────────────────
   TILE MAP CANVAS
──────────────────────────────────────────────────────────────── */
const TileMapCanvas = ({ api }) => {
    const canvasRef = React.useRef(null);
    const containerRef = React.useRef(null);
    const [isDragging, setIsDragging] = React.useState(false);
    const activeMap = api.project.tileMaps.find(m => m.id === api.editorState.activeTileMapId);
    // Render the tile map
    React.useEffect(() => {
        var _a;
        const canvas = canvasRef.current;
        if (!canvas || !activeMap)
            return;
        const ctx = canvas.getContext('2d');
        if (!ctx)
            return;
        canvas.width = activeMap.width * activeMap.tileWidth;
        canvas.height = activeMap.height * activeMap.tileHeight;
        // Background
        ctx.fillStyle = activeMap.backgroundColor;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        // Render layers
        for (const layer of activeMap.layers.filter(l => l.visible)) {
            ctx.globalAlpha = layer.opacity;
            for (let y = 0; y < activeMap.height; y++) {
                for (let x = 0; x < activeMap.width; x++) {
                    const tileId = (_a = layer.tiles[y]) === null || _a === void 0 ? void 0 : _a[x];
                    if (tileId) {
                        const tile = api.project.tileSets.find(t => t.id === tileId);
                        if (tile === null || tile === void 0 ? void 0 : tile.thumbnail) {
                            const img = new Image();
                            img.src = tile.thumbnail;
                            ctx.drawImage(img, x * activeMap.tileWidth, y * activeMap.tileHeight, activeMap.tileWidth, activeMap.tileHeight);
                        }
                    }
                }
            }
        }
        // Grid overlay
        if (api.editorState.showGrid) {
            ctx.globalAlpha = 0.3;
            ctx.strokeStyle = '#ffffff';
            ctx.lineWidth = 1;
            for (let x = 0; x <= activeMap.width; x++) {
                ctx.beginPath();
                ctx.moveTo(x * activeMap.tileWidth, 0);
                ctx.lineTo(x * activeMap.tileWidth, canvas.height);
                ctx.stroke();
            }
            for (let y = 0; y <= activeMap.height; y++) {
                ctx.beginPath();
                ctx.moveTo(0, y * activeMap.tileHeight);
                ctx.lineTo(canvas.width, y * activeMap.tileHeight);
                ctx.stroke();
            }
        }
        ctx.globalAlpha = 1;
    }, [activeMap, api.project.tileSets, api.editorState.showGrid]);
    const handleCanvasClick = React.useCallback((e) => {
        if (!activeMap || !api.editorState.activeLayerId || !api.editorState.selectedTileId)
            return;
        const canvas = canvasRef.current;
        if (!canvas)
            return;
        const rect = canvas.getBoundingClientRect();
        const scaleX = canvas.width / rect.width;
        const scaleY = canvas.height / rect.height;
        const x = Math.floor((e.clientX - rect.left) * scaleX / activeMap.tileWidth);
        const y = Math.floor((e.clientY - rect.top) * scaleY / activeMap.tileHeight);
        if (api.editorState.tileMapTool === 'brush') {
            api.setTile(api.editorState.activeLayerId, x, y, api.editorState.selectedTileId);
        }
        else if (api.editorState.tileMapTool === 'eraser') {
            api.setTile(api.editorState.activeLayerId, x, y, null);
        }
        else if (api.editorState.tileMapTool === 'fill') {
            api.fillTiles(api.editorState.activeLayerId, x, y, api.editorState.selectedTileId);
        }
    }, [activeMap, api]);
    return (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.viewport, ref: containerRef, children: activeMap ? (jsxRuntime.jsx("canvas", { ref: canvasRef, className: GameAsset2dEditor_module.viewportCanvas, style: {
                transform: `translate(-50%, -50%) scale(${api.editorState.zoom})`,
            }, onClick: handleCanvasClick, onMouseDown: () => setIsDragging(true), onMouseUp: () => setIsDragging(false), onMouseMove: e => {
                if (isDragging && (api.editorState.tileMapTool === 'brush' || api.editorState.tileMapTool === 'eraser')) {
                    handleCanvasClick(e);
                }
            } })) : (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.emptyState, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "Create or select a tile map to start editing" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.btnPrimary, variant: "primary", onClick: () => api.createTileMap('New Map', 32, 32), children: "Create Tile Map" })] })) }));
};
/* ────────────────────────────────────────────────────────────────
   SPRITE SHEET CANVAS
──────────────────────────────────────────────────────────────── */
const SpriteSheetCanvas = ({ api }) => {
    const canvasRef = React.useRef(null);
    const fileInputRef = React.useRef(null);
    const activeSheet = api.project.spriteSheets.find(s => s.id === api.editorState.activeSpriteSheetId);
    // Render sprite sheet with frame overlays
    React.useEffect(() => {
        const canvas = canvasRef.current;
        if (!canvas || !(activeSheet === null || activeSheet === void 0 ? void 0 : activeSheet.image))
            return;
        const ctx = canvas.getContext('2d');
        if (!ctx)
            return;
        canvas.width = activeSheet.width;
        canvas.height = activeSheet.height;
        // Draw image
        ctx.drawImage(activeSheet.image, 0, 0);
        // Draw frame overlays
        ctx.strokeStyle = '#ffd369';
        ctx.lineWidth = 2;
        for (const frame of activeSheet.frames) {
            const isSelected = api.editorState.selectedFrameIds.includes(frame.id);
            ctx.strokeStyle = isSelected ? '#ffd369' : '#4a5a7e';
            ctx.strokeRect(frame.x, frame.y, frame.width, frame.height);
            // Draw pivot point
            const pivotX = frame.x + frame.width * frame.pivotX;
            const pivotY = frame.y + frame.height * frame.pivotY;
            ctx.fillStyle = isSelected ? '#ffd369' : '#4a5a7e';
            ctx.beginPath();
            ctx.arc(pivotX, pivotY, 3, 0, Math.PI * 2);
            ctx.fill();
        }
    }, [activeSheet, api.editorState.selectedFrameIds]);
    const handleFrameClick = React.useCallback((e) => {
        if (!activeSheet)
            return;
        const canvas = canvasRef.current;
        if (!canvas)
            return;
        const rect = canvas.getBoundingClientRect();
        const scaleX = canvas.width / rect.width;
        const scaleY = canvas.height / rect.height;
        const x = (e.clientX - rect.left) * scaleX;
        const y = (e.clientY - rect.top) * scaleY;
        // Find clicked frame
        const clickedFrame = activeSheet.frames.find(f => x >= f.x && x <= f.x + f.width && y >= f.y && y <= f.y + f.height);
        if (clickedFrame) {
            if (e.ctrlKey || e.metaKey) {
                // Toggle selection
                const newSelection = api.editorState.selectedFrameIds.includes(clickedFrame.id)
                    ? api.editorState.selectedFrameIds.filter(id => id !== clickedFrame.id)
                    : [...api.editorState.selectedFrameIds, clickedFrame.id];
                api.selectFrames(newSelection);
            }
            else {
                api.selectFrames([clickedFrame.id]);
            }
        }
        else {
            api.selectFrames([]);
        }
    }, [activeSheet, api]);
    return (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.viewport, children: activeSheet ? (jsxRuntime.jsx("canvas", { ref: canvasRef, className: GameAsset2dEditor_module.viewportCanvas, style: {
                transform: `translate(-50%, -50%) scale(${api.editorState.zoom})`,
            }, onClick: handleFrameClick })) : (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.emptyState, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "Import a sprite sheet to start" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.btnPrimary, variant: "primary", onClick: () => { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, children: "Import Sprite Sheet" }), jsxRuntime.jsx("input", { ref: fileInputRef, type: "file", accept: "image/*", style: { display: 'none' }, onChange: e => {
                        var _a;
                        const file = (_a = e.target.files) === null || _a === void 0 ? void 0 : _a[0];
                        if (file)
                            api.importSpriteSheet(file);
                        e.target.value = '';
                    } })] })) }));
};
/* ────────────────────────────────────────────────────────────────
   SPRITE SHEETS LIST
──────────────────────────────────────────────────────────────── */
const SpriteSheetsList = ({ api }) => {
    const fileInputRef = React.useRef(null);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.leftPanel, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelHeader, children: [jsxRuntime.jsx("span", { children: "Sprite Sheets" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: () => { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, children: "+ Import" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: api.project.spriteSheets.length === 0 ? (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyState, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "No sprite sheets" }) })) : (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.layersList, children: api.project.spriteSheets.map(sheet => (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.layerItem} ${api.editorState.activeSpriteSheetId === sheet.id ? GameAsset2dEditor_module.selected : ''}`, onClick: () => api.selectSpriteSheet(sheet.id), children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: sheet.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [sheet.frames.length, " frames"] })] }, sheet.id))) })) }), jsxRuntime.jsx("input", { ref: fileInputRef, type: "file", accept: "image/*", style: { display: 'none' }, onChange: e => {
                    var _a;
                    const file = (_a = e.target.files) === null || _a === void 0 ? void 0 : _a[0];
                    if (file)
                        api.importSpriteSheet(file);
                    e.target.value = '';
                } })] }));
};
/* ────────────────────────────────────────────────────────────────
   FRAMES & ANIMATIONS PANEL
──────────────────────────────────────────────────────────────── */
const FramesPanel = ({ api }) => {
    var _a;
    const [animName, setAnimName] = React.useState('');
    const activeSheet = api.project.spriteSheets.find(s => s.id === api.editorState.activeSpriteSheetId);
    const createAnimationFromSelection = React.useCallback(() => {
        if (animName && api.editorState.selectedFrameIds.length > 0) {
            api.createAnimation(animName, api.editorState.selectedFrameIds);
            setAnimName('');
        }
    }, [animName, api]);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.rightPanel, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsxs("span", { children: ["Frames (", (_a = activeSheet === null || activeSheet === void 0 ? void 0 : activeSheet.frames.length) !== null && _a !== void 0 ? _a : 0, ")"] }) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, style: { maxHeight: 200 }, children: activeSheet === null || activeSheet === void 0 ? void 0 : activeSheet.frames.map(frame => (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.layerItem} ${api.editorState.selectedFrameIds.includes(frame.id) ? GameAsset2dEditor_module.selected : ''}`, onClick: () => api.selectFrames([frame.id]), children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: frame.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [frame.width, "\u00D7", frame.height] })] }, frame.id))) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsx("span", { children: "Animations" }) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelContent, children: [api.editorState.selectedFrameIds.length > 0 && (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelSection, children: [jsxRuntime.jsx(index.NiceTextInput, { className: GameAsset2dEditor_module.panelInput, placeholder: "Animation name...", value: animName, onChange: val => setAnimName(val) }), jsxRuntime.jsxs(index.NiceButton, { className: GameAsset2dEditor_module.btnPrimary, variant: "primary", onClick: createAnimationFromSelection, style: { marginTop: 8, width: '100%' }, disabled: !animName, children: ["Create from ", api.editorState.selectedFrameIds.length, " frames"] })] })), activeSheet === null || activeSheet === void 0 ? void 0 : activeSheet.animations.map(anim => (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.layerItem, children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: anim.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [anim.frameIds.length, "f"] }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.effectRemove, variant: "ghost", size: "sm", onClick: () => api.removeAnimation(anim.id), children: "\u00D7" })] }, anim.id)))] })] }));
};
/* ────────────────────────────────────────────────────────────────
   ANIMATION STATE MACHINE
──────────────────────────────────────────────────────────────── */
const AnimationStateMachineView = ({ api }) => {
    var _a;
    const [newStateName, setNewStateName] = React.useState('');
    const activeSM = api.project.stateMachines.find(sm => sm.id === api.editorState.activeStateMachineId);
    // Get all animations from all sprite sheets
    const allAnimations = React.useMemo(() => {
        const result = [];
        for (const sheet of api.project.spriteSheets) {
            for (const anim of sheet.animations) {
                result.push({ ...anim, sheetName: sheet.name });
            }
        }
        return result;
    }, [api.project.spriteSheets]);
    return (jsxRuntime.jsxs("div", { style: { display: 'flex', flex: 1, overflow: 'hidden' }, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.leftPanel, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelHeader, children: [jsxRuntime.jsx("span", { children: "State Machines" }), jsxRuntime.jsx("button", { className: GameAsset2dEditor_module.menuBtn, onClick: () => api.createStateMachine('New State Machine'), children: "+ New" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, style: { maxHeight: 150 }, children: api.project.stateMachines.map(sm => (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.layerItem} ${api.editorState.activeStateMachineId === sm.id ? GameAsset2dEditor_module.selected : ''}`, onClick: () => api.selectStateMachine(sm.id), children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: sm.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [sm.states.length, " states"] })] }, sm.id))) }), activeSM && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsx("span", { children: "States" }) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelContent, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelSection, children: [jsxRuntime.jsx(index.NiceTextInput, { className: GameAsset2dEditor_module.panelInput, placeholder: "State name...", value: newStateName, onChange: val => setNewStateName(val) }), allAnimations.length > 0 && newStateName && (jsxRuntime.jsx(index.NiceSelect, { className: GameAsset2dEditor_module.panelInput, style: { marginTop: 4 }, onChange: val => {
                                                    if (val) {
                                                        api.addState(newStateName, val);
                                                        setNewStateName('');
                                                    }
                                                }, options: [
                                                    { value: '', label: 'Select animation...' },
                                                    ...allAnimations.map(anim => ({
                                                        value: anim.id,
                                                        label: `${anim.sheetName}: ${anim.name}`
                                                    }))
                                                ] }))] }), activeSM.states.map(state => (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.layerItem} ${api.editorState.selectedStateId === state.id ? GameAsset2dEditor_module.selected : ''} ${activeSM.entryStateId === state.id ? GameAsset2dEditor_module.entry : ''}`, onClick: () => api.selectState(state.id), children: [jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerName, children: [activeSM.entryStateId === state.id && '▶ ', state.name] }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.effectRemove, variant: "ghost", size: "sm", onClick: e => {
                                                    e.stopPropagation();
                                                    api.removeState(state.id);
                                                }, children: "\u00D7" })] }, state.id)))] })] }))] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.stateGraph, children: activeSM ? (jsxRuntime.jsxs("svg", { width: "100%", height: "100%", children: [jsxRuntime.jsx("defs", { children: jsxRuntime.jsx("marker", { id: "arrowhead", markerWidth: "10", markerHeight: "7", refX: "9", refY: "3.5", orient: "auto", children: jsxRuntime.jsx("polygon", { points: "0 0, 10 3.5, 0 7", fill: "#4a5a7e" }) }) }), activeSM.transitions.map(trans => {
                            const from = activeSM.states.find(s => s.id === trans.fromStateId);
                            const to = activeSM.states.find(s => s.id === trans.toStateId);
                            if (!from || !to)
                                return null;
                            return (jsxRuntime.jsx("line", { x1: from.position.x + 60, y1: from.position.y + 20, x2: to.position.x + 60, y2: to.position.y + 20, className: GameAsset2dEditor_module.transition, onClick: () => api.removeTransition(trans.id) }, trans.id));
                        }), activeSM.states.map(state => {
                            var _a, _b;
                            return (jsxRuntime.jsx("foreignObject", { x: state.position.x, y: state.position.y, width: 120, height: 50, children: jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.stateNode} ${api.editorState.selectedStateId === state.id ? GameAsset2dEditor_module.selected : ''} ${activeSM.entryStateId === state.id ? GameAsset2dEditor_module.entry : ''}`, onClick: () => api.selectState(state.id), children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.stateNodeName, children: state.name }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.stateNodeAnim, children: (_b = (_a = allAnimations.find(a => a.id === state.animationId)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : 'No animation' })] }) }, state.id));
                        })] })) : (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyState, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "Create a state machine to design animation flows" }) })) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.rightPanel, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelHeader, children: [jsxRuntime.jsx("span", { children: "Parameters" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.menuBtn, variant: "ghost", size: "sm", onClick: () => api.addParameter('new_param', 'trigger'), disabled: !activeSM, children: "+ Add" })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: activeSM === null || activeSM === void 0 ? void 0 : activeSM.parameters.map(param => (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.layerItem, children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerOpacity, children: param.type }), jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: param.name }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.effectRemove, variant: "ghost", size: "sm", onClick: () => api.removeParameter(param.id), children: "\u00D7" })] }, param.id))) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsx("span", { children: "Transitions" }) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelContent, children: [activeSM === null || activeSM === void 0 ? void 0 : activeSM.transitions.map(trans => {
                                var _a, _b;
                                const from = activeSM.states.find(s => s.id === trans.fromStateId);
                                const to = activeSM.states.find(s => s.id === trans.toStateId);
                                return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.layerItem, children: [jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerName, children: [(_a = from === null || from === void 0 ? void 0 : from.name) !== null && _a !== void 0 ? _a : '?', " \u2192 ", (_b = to === null || to === void 0 ? void 0 : to.name) !== null && _b !== void 0 ? _b : '?'] }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.effectRemove, variant: "ghost", size: "sm", onClick: () => api.removeTransition(trans.id), children: "\u00D7" })] }, trans.id));
                            }), api.editorState.selectedStateId && (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelSection, style: { marginTop: 12 }, children: [jsxRuntime.jsx("label", { className: GameAsset2dEditor_module.panelLabel, children: "Add transition to:" }), jsxRuntime.jsx(index.NiceSelect, { className: GameAsset2dEditor_module.panelInput, onChange: val => {
                                            if (val && api.editorState.selectedStateId) {
                                                api.addTransition(api.editorState.selectedStateId, val);
                                            }
                                        }, options: [
                                            { value: '', label: 'Select target state...' },
                                            ...((_a = activeSM === null || activeSM === void 0 ? void 0 : activeSM.states.filter(s => s.id !== api.editorState.selectedStateId).map(s => ({ value: s.id, label: s.name }))) !== null && _a !== void 0 ? _a : [])
                                        ] })] }))] })] })] }));
};
/* ────────────────────────────────────────────────────────────────
   EFFECTS PANEL
──────────────────────────────────────────────────────────────── */
const EffectsView = ({ api }) => {
    const [currentEffects, setCurrentEffects] = React.useState([]);
    const [presetName, setPresetName] = React.useState('');
    const effectTypes = [
        { type: 'brightness', label: 'Brightness' },
        { type: 'contrast', label: 'Contrast' },
        { type: 'saturation', label: 'Saturation' },
        { type: 'hue-shift', label: 'Hue Shift' },
        { type: 'invert', label: 'Invert' },
        { type: 'pixelate', label: 'Pixelate' },
        { type: 'outline', label: 'Outline' },
        { type: 'tint', label: 'Tint' },
        { type: 'blur', label: 'Blur' },
        { type: 'glow', label: 'Glow' },
        { type: 'shadow', label: 'Shadow' },
        { type: 'palette-swap', label: 'Palette Swap' },
    ];
    return (jsxRuntime.jsxs("div", { style: { display: 'flex', flex: 1, overflow: 'hidden' }, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.leftPanel, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsx("span", { children: "Add Effect" }) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: effectTypes.map(({ type, label }) => (jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.layerItem, variant: "ghost", onClick: () => api.addEffect(type), style: { cursor: 'pointer', border: 'none', textAlign: 'left' }, children: jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: label }) }, type))) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsx("span", { children: "Presets" }) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelContent, children: [jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.panelSection, children: [jsxRuntime.jsx(index.NiceTextInput, { className: GameAsset2dEditor_module.panelInput, placeholder: "Preset name...", value: presetName, onChange: val => setPresetName(val) }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.btnSecondary, variant: "ghost", style: { marginTop: 4, width: '100%' }, onClick: () => {
                                            if (presetName) {
                                                api.saveEffectPreset(presetName);
                                                setPresetName('');
                                            }
                                        }, disabled: !presetName, children: "Save Current Effects" })] }), api.project.effectPresets.map(preset => (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.layerItem, onClick: () => api.loadEffectPreset(preset.id), style: { cursor: 'pointer' }, children: [jsxRuntime.jsx("span", { className: GameAsset2dEditor_module.layerName, children: preset.name }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.layerOpacity, children: [preset.effects.length, " effects"] })] }, preset.id)))] })] }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.viewport, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyState, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "Select a sprite or tile to preview effects" }) }) }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.rightPanel, children: [jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelHeader, children: jsxRuntime.jsx("span", { children: "Active Effects" }) }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.panelContent, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.effectsList, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyState, children: jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.emptyStateText, children: "Add effects from the left panel" }) }) }) })] })] }));
};
/* ────────────────────────────────────────────────────────────────
   STATUS BAR
──────────────────────────────────────────────────────────────── */
const StatusBar = ({ api }) => {
    const activeMap = api.project.tileMaps.find(m => m.id === api.editorState.activeTileMapId);
    const activeSheet = api.project.spriteSheets.find(s => s.id === api.editorState.activeSpriteSheetId);
    return (jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.statusBar, children: [jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.statusItem, children: ["Mode: ", api.editorState.mode] }), api.editorState.mode === 'tilemap' && activeMap && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.statusItem, children: ["Map: ", activeMap.name, " (", activeMap.width, "\u00D7", activeMap.height, ")"] }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.statusItem, children: ["Tile: ", activeMap.tileWidth, "\u00D7", activeMap.tileHeight, "px"] })] })), api.editorState.mode === 'spritesheet' && activeSheet && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.statusItem, children: ["Sheet: ", activeSheet.name] }), jsxRuntime.jsxs("span", { className: GameAsset2dEditor_module.statusItem, children: ["Selected: ", api.editorState.selectedFrameIds.length, " frames"] })] })), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.zoomControl, children: [jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.zoomBtn, variant: "ghost", size: "sm", onClick: () => api.setZoom(api.editorState.zoom - 0.25), children: "\u2212" }), jsxRuntime.jsxs("span", { children: [Math.round(api.editorState.zoom * 100), "%"] }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.zoomBtn, variant: "ghost", size: "sm", onClick: () => api.setZoom(api.editorState.zoom + 0.25), children: "+" }), jsxRuntime.jsx(index.NiceButton, { className: GameAsset2dEditor_module.zoomBtn, variant: "ghost", size: "sm", onClick: () => api.setZoom(1), children: "\u2299" })] })] }));
};
/* ────────────────────────────────────────────────────────────────
   MAIN COMPONENT
──────────────────────────────────────────────────────────────── */
const NiceGameAsset2dEditor = ({ className, onSave, onExport, }) => {
    const api = useGameAssetEditor.useGameAssetEditor({ onSave, onExport });
    const renderModeContent = () => {
        switch (api.editorState.mode) {
            case 'tilemap':
                return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(TileMapToolbar, { api: api }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.mainArea, children: [jsxRuntime.jsx(TilePalette, { api: api }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.centerArea, children: jsxRuntime.jsx(TileMapCanvas, { api: api }) }), jsxRuntime.jsx(LayersPanel, { api: api })] })] }));
            case 'spritesheet':
                return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(SpriteSheetToolbar, { api: api }), jsxRuntime.jsxs("div", { className: GameAsset2dEditor_module.mainArea, children: [jsxRuntime.jsx(SpriteSheetsList, { api: api }), jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.centerArea, children: jsxRuntime.jsx(SpriteSheetCanvas, { api: api }) }), jsxRuntime.jsx(FramesPanel, { api: api })] })] }));
            case 'animation':
                return (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.mainArea, children: jsxRuntime.jsx(AnimationStateMachineView, { api: api }) }));
            case 'effects':
                return (jsxRuntime.jsx("div", { className: GameAsset2dEditor_module.mainArea, children: jsxRuntime.jsx(EffectsView, { api: api }) }));
            default:
                return null;
        }
    };
    return (jsxRuntime.jsxs("div", { className: `${GameAsset2dEditor_module.root} ${className || ''}`, children: [jsxRuntime.jsx(MenuBar, { api: api }), jsxRuntime.jsx(ModeTabs, { api: api }), renderModeContent(), jsxRuntime.jsx(StatusBar, { api: api })] }));
};
/** @deprecated Use NiceGameAsset2dEditor */
const GameAsset2dEditor = NiceGameAsset2dEditor;

exports.GameAsset2dEditor = GameAsset2dEditor;
exports.NiceGameAsset2dEditor = NiceGameAsset2dEditor;
//# sourceMappingURL=GameAsset2dEditor.js.map
