import { hasChildren } from './types.js';
// ─── ID Generation ──────────────────────────────────────────────────
let counter = 0;
export function createId() {
    const timestamp = Date.now().toString(36);
    const count = (counter++).toString(36);
    const random = Math.random().toString(36).slice(2, 8);
    return `node_${timestamp}_${count}_${random}`;
}
// ─── Tree Traversal ─────────────────────────────────────────────────
function getChildren(node) {
    if (hasChildren(node)) {
        return node.children;
    }
    return [];
}
export function findNode(root, id) {
    if (root.id === id)
        return root;
    for (const child of getChildren(root)) {
        const found = findNode(child, id);
        if (found)
            return found;
    }
    return undefined;
}
export function findParent(root, id) {
    if (!hasChildren(root))
        return undefined;
    for (const child of getChildren(root)) {
        if (child.id === id)
            return root;
        const found = findParent(child, id);
        if (found)
            return found;
    }
    return undefined;
}
// ─── Tree Mutation (Immutable) ──────────────────────────────────────
function deepClone(obj) {
    return JSON.parse(JSON.stringify(obj));
}
export function addNode(root, parentId, node, index) {
    const newRoot = deepClone(root);
    const parent = findNode(newRoot, parentId);
    if (!parent || !hasChildren(parent))
        return newRoot;
    const clonedNode = deepClone(node);
    const children = parent.children;
    if (index !== undefined) {
        children.splice(index, 0, clonedNode);
    }
    else {
        children.push(clonedNode);
    }
    return newRoot;
}
export function removeNode(root, nodeId) {
    const newRoot = deepClone(root);
    const parent = findParent(newRoot, nodeId);
    if (!parent)
        return newRoot;
    const children = parent.children;
    const idx = children.findIndex((c) => c.id === nodeId);
    if (idx !== -1) {
        children.splice(idx, 1);
    }
    return newRoot;
}
export function moveNode(root, nodeId, newParentId, index) {
    const newRoot = deepClone(root);
    const oldParent = findParent(newRoot, nodeId);
    if (!oldParent)
        return newRoot;
    const oldChildren = oldParent.children;
    const idx = oldChildren.findIndex((c) => c.id === nodeId);
    if (idx === -1)
        return newRoot;
    const removed = oldChildren.splice(idx, 1);
    const node = removed[0];
    const newParent = findNode(newRoot, newParentId);
    if (!newParent || !hasChildren(newParent))
        return newRoot;
    const newChildren = newParent.children;
    if (index !== undefined) {
        newChildren.splice(index, 0, node);
    }
    else {
        newChildren.push(node);
    }
    return newRoot;
}
export function updateNodeSettings(root, nodeId, settings) {
    const newRoot = deepClone(root);
    const node = findNode(newRoot, nodeId);
    if (!node || !('settings' in node))
        return newRoot;
    node.settings = { ...node.settings, ...settings };
    return newRoot;
}
export function updateBlockData(root, blockId, data) {
    const newRoot = deepClone(root);
    const node = findNode(newRoot, blockId);
    if (!node || node.type !== 'block')
        return newRoot;
    node.data = { ...node.data, ...data };
    return newRoot;
}
// ─── Factory Functions ──────────────────────────────────────────────
export function createEmptyPage() {
    return { id: createId(), type: 'page', children: [] };
}
export function createSection(settings) {
    return {
        id: createId(),
        type: 'section',
        settings: { ...settings },
        children: [],
    };
}
export function createContainer(settings) {
    return {
        id: createId(),
        type: 'container',
        settings: { maxWidth: '1200px', ...settings },
        children: [],
    };
}
export function createRow(columns, settings) {
    return {
        id: createId(),
        type: 'row',
        settings: { gap: '24px', ...settings },
        children: columns ?? [],
    };
}
export function createColumn(width, children) {
    return {
        id: createId(),
        type: 'column',
        settings: { width },
        children: children ?? [],
    };
}
export function createBlock(blockType, variant, data) {
    return {
        id: createId(),
        type: 'block',
        settings: { blockType, ...(variant ? { variant } : {}) },
        data: data ?? {},
    };
}
export function createSavedSectionRef(refId) {
    return {
        id: createId(),
        type: 'savedSectionRef',
        settings: { refId },
    };
}
//# sourceMappingURL=tree.js.map