'use strict';

var math = require('../core/math.js');

/* ────────────────────────────────────────────────────────────────
   Scene Editor — Selection, transform gizmos, viewport rendering
──────────────────────────────────────────────────────────────── */
function createGizmoState(mode = 'translate') {
    return {
        mode,
        activeAxis: 'none',
        dragging: false,
        startWorld: { x: 0, y: 0 },
        startLocal: { x: 0, y: 0 },
        delta: { x: 0, y: 0 },
    };
}
function getSelectionRect(box) {
    const x = Math.min(box.start.x, box.end.x);
    const y = Math.min(box.start.y, box.end.y);
    return {
        x, y,
        width: Math.abs(box.end.x - box.start.x),
        height: Math.abs(box.end.y - box.start.y),
    };
}
function entitiesInRect(world, rect) {
    const result = [];
    for (const e of world.allEntities()) {
        const transform = world.getComponent(e.id, 'Transform');
        if (!transform)
            continue;
        const pos = transform.position;
        if (pos.x >= rect.x && pos.x <= rect.x + rect.width &&
            pos.y >= rect.y && pos.y <= rect.y + rect.height) {
            result.push(e.id);
        }
    }
    return result;
}
/* ── Snap ─────────────────────────────────────────────────────── */
function snapToGrid(value, gridSize) {
    return Math.round(value / gridSize) * gridSize;
}
function snapVec2ToGrid(v, gridSize) {
    return {
        x: snapToGrid(v.x, gridSize),
        y: snapToGrid(v.y, gridSize),
    };
}
/* ── Grid Renderer ────────────────────────────────────────────── */
function renderGrid(ctx, camera, canvasW, canvasH, gridSize, color = 'rgba(255,255,255,0.06)', majorColor = 'rgba(255,255,255,0.15)', majorEvery = 10) {
    const topLeft = camera.screenToWorld({ x: 0, y: 0 });
    const bottomRight = camera.screenToWorld({ x: canvasW, y: canvasH });
    const startX = Math.floor(topLeft.x / gridSize) * gridSize;
    const startY = Math.floor(topLeft.y / gridSize) * gridSize;
    ctx.lineWidth = 1;
    for (let x = startX; x <= bottomRight.x; x += gridSize) {
        const idx = Math.round(x / gridSize);
        ctx.strokeStyle = idx % majorEvery === 0 ? majorColor : color;
        const sx = camera.worldToScreen({ x, y: 0 }).x;
        ctx.beginPath();
        ctx.moveTo(Math.round(sx) + 0.5, 0);
        ctx.lineTo(Math.round(sx) + 0.5, canvasH);
        ctx.stroke();
    }
    for (let y = startY; y <= bottomRight.y; y += gridSize) {
        const idx = Math.round(y / gridSize);
        ctx.strokeStyle = idx % majorEvery === 0 ? majorColor : color;
        const sy = camera.worldToScreen({ x: 0, y }).y;
        ctx.beginPath();
        ctx.moveTo(0, Math.round(sy) + 0.5);
        ctx.lineTo(canvasW, Math.round(sy) + 0.5);
        ctx.stroke();
    }
}
/* ── Gizmo Renderer ───────────────────────────────────────────── */
function renderTranslateGizmo(ctx, screenPos, activeAxis, size = 60) {
    const arrowLen = size;
    const headLen = 10;
    // X arrow (red)
    ctx.strokeStyle = activeAxis === 'x' || activeAxis === 'xy' ? '#ff6060' : '#e03030';
    ctx.lineWidth = activeAxis === 'x' ? 3 : 2;
    ctx.beginPath();
    ctx.moveTo(screenPos.x, screenPos.y);
    ctx.lineTo(screenPos.x + arrowLen, screenPos.y);
    ctx.stroke();
    // Arrowhead
    ctx.fillStyle = ctx.strokeStyle;
    ctx.beginPath();
    ctx.moveTo(screenPos.x + arrowLen, screenPos.y);
    ctx.lineTo(screenPos.x + arrowLen - headLen, screenPos.y - headLen / 2);
    ctx.lineTo(screenPos.x + arrowLen - headLen, screenPos.y + headLen / 2);
    ctx.fill();
    // Y arrow (green)
    ctx.strokeStyle = activeAxis === 'y' || activeAxis === 'xy' ? '#60ff60' : '#30e030';
    ctx.lineWidth = activeAxis === 'y' ? 3 : 2;
    ctx.beginPath();
    ctx.moveTo(screenPos.x, screenPos.y);
    ctx.lineTo(screenPos.x, screenPos.y - arrowLen);
    ctx.stroke();
    ctx.fillStyle = ctx.strokeStyle;
    ctx.beginPath();
    ctx.moveTo(screenPos.x, screenPos.y - arrowLen);
    ctx.lineTo(screenPos.x - headLen / 2, screenPos.y - arrowLen + headLen);
    ctx.lineTo(screenPos.x + headLen / 2, screenPos.y - arrowLen + headLen);
    ctx.fill();
    // Center square (both axes)
    const sq = 12;
    ctx.fillStyle = activeAxis === 'xy' ? 'rgba(255,255,100,0.5)' : 'rgba(255,255,100,0.2)';
    ctx.fillRect(screenPos.x, screenPos.y - sq, sq, sq);
}
function renderRotateGizmo(ctx, screenPos, active, radius = 50) {
    ctx.strokeStyle = active ? '#60a0ff' : '#3080d0';
    ctx.lineWidth = active ? 3 : 2;
    ctx.beginPath();
    ctx.arc(screenPos.x, screenPos.y, radius, 0, Math.PI * 2);
    ctx.stroke();
}
function renderScaleGizmo(ctx, screenPos, activeAxis, size = 60) {
    const boxSize = 6;
    // X line + box
    ctx.strokeStyle = activeAxis === 'x' || activeAxis === 'xy' ? '#ff6060' : '#e03030';
    ctx.lineWidth = activeAxis === 'x' ? 3 : 2;
    ctx.beginPath();
    ctx.moveTo(screenPos.x, screenPos.y);
    ctx.lineTo(screenPos.x + size, screenPos.y);
    ctx.stroke();
    ctx.fillStyle = ctx.strokeStyle;
    ctx.fillRect(screenPos.x + size - boxSize / 2, screenPos.y - boxSize / 2, boxSize, boxSize);
    // Y line + box
    ctx.strokeStyle = activeAxis === 'y' || activeAxis === 'xy' ? '#60ff60' : '#30e030';
    ctx.lineWidth = activeAxis === 'y' ? 3 : 2;
    ctx.beginPath();
    ctx.moveTo(screenPos.x, screenPos.y);
    ctx.lineTo(screenPos.x, screenPos.y - size);
    ctx.stroke();
    ctx.fillStyle = ctx.strokeStyle;
    ctx.fillRect(screenPos.x - boxSize / 2, screenPos.y - size - boxSize / 2, boxSize, boxSize);
}
/* ── Hit-test gizmo axis ──────────────────────────────────────── */
function hitTestGizmoAxis(screenPos, cursorScreen, size = 60, threshold = 8) {
    const dx = cursorScreen.x - screenPos.x;
    const dy = -(cursorScreen.y - screenPos.y); // flip Y
    // Center square
    if (dx >= 0 && dx <= 14 && dy >= 0 && dy <= 14)
        return 'xy';
    // X axis
    if (dx > 0 && dx <= size + threshold && Math.abs(cursorScreen.y - screenPos.y) < threshold)
        return 'x';
    // Y axis
    if (dy > 0 && dy <= size + threshold && Math.abs(cursorScreen.x - screenPos.x) < threshold)
        return 'y';
    return 'none';
}
/* ── Entity pick ──────────────────────────────────────────────── */
function pickEntityAt(world, worldPos, margin = 16) {
    let best = null;
    let bestDist = Infinity;
    for (const e of world.allEntities()) {
        const t = world.getComponent(e.id, 'Transform');
        if (!t)
            continue;
        const dist = math.v2.dist(t.position, worldPos);
        if (dist < margin && dist < bestDist) {
            bestDist = dist;
            best = e;
        }
    }
    return best;
}
function copyEntities(world, ids) {
    const componentTypes = [
        'Transform', 'Sprite', 'Shape', 'Collider2D', 'RigidBody2D',
        'Text', 'Animator', 'Script', 'AudioSource', 'ParticleEmitter', 'CameraTarget',
    ];
    return ids.map(id => {
        const comps = {};
        for (const type of componentTypes) {
            const c = world.getComponent(id, type);
            if (c)
                comps[type] = JSON.parse(JSON.stringify(c));
        }
        const transform = world.getComponent(id, 'Transform');
        return {
            entityId: id,
            components: comps,
            position: transform ? { ...transform.position } : { x: 0, y: 0 },
        };
    }).filter(Boolean);
}
function pasteEntities(world, entries, offset = { x: 20, y: 20 }) {
    const newIds = [];
    for (const entry of entries) {
        const e = world.createEntity(`${entry.entityId}_copy`);
        newIds.push(e.id);
        const comps = entry.components;
        for (const [type, data] of Object.entries(comps)) {
            const clone = JSON.parse(JSON.stringify(data));
            if (type === 'Transform' && clone.position) {
                clone.position.x += offset.x;
                clone.position.y += offset.y;
            }
            world.addComponent(e.id, type, clone);
        }
    }
    return newIds;
}

exports.copyEntities = copyEntities;
exports.createGizmoState = createGizmoState;
exports.entitiesInRect = entitiesInRect;
exports.getSelectionRect = getSelectionRect;
exports.hitTestGizmoAxis = hitTestGizmoAxis;
exports.pasteEntities = pasteEntities;
exports.pickEntityAt = pickEntityAt;
exports.renderGrid = renderGrid;
exports.renderRotateGizmo = renderRotateGizmo;
exports.renderScaleGizmo = renderScaleGizmo;
exports.renderTranslateGizmo = renderTranslateGizmo;
exports.snapToGrid = snapToGrid;
exports.snapVec2ToGrid = snapVec2ToGrid;
//# sourceMappingURL=SceneEditor.js.map
