const maxWorkspaces = 128;
const maxTabsPerWorkspace = 512;
const maxLayoutNodeDepth = 64;
const maxLayoutNodes = 4_096;
export function isTerminalLayoutState(value) {
    return normalizeTerminalLayoutState(value) !== null;
}
export function normalizeTerminalLayoutState(value) {
    if (!value || typeof value !== 'object') {
        return null;
    }
    const state = value;
    if ((typeof state.activeWorkspaceId !== 'string' && state.activeWorkspaceId !== null) ||
        !Array.isArray(state.workspaces) ||
        state.workspaces.length > maxWorkspaces) {
        return null;
    }
    const workspaces = state.workspaces.map(normalizeTerminalWorkspaceLayout);
    if (workspaces.some((workspace) => workspace === null)) {
        return null;
    }
    return {
        activeWorkspaceId: state.activeWorkspaceId,
        workspaces: workspaces
    };
}
export function isTerminalWorkspaceLayout(value) {
    return normalizeTerminalWorkspaceLayout(value) !== null;
}
function normalizeTerminalWorkspaceLayout(value) {
    if (!value || typeof value !== 'object') {
        return null;
    }
    const workspace = value;
    if (typeof workspace.id !== 'string' ||
        typeof workspace.title !== 'string' ||
        (typeof workspace.activeTabId !== 'string' && workspace.activeTabId !== null) ||
        !Array.isArray(workspace.tabs) ||
        workspace.tabs.length > maxTabsPerWorkspace) {
        return null;
    }
    const tabs = workspace.tabs.map(normalizeTerminalTabLayout);
    if (tabs.some((tab) => tab === null)) {
        return null;
    }
    return {
        id: workspace.id,
        title: workspace.title,
        activeTabId: workspace.activeTabId,
        tabs: tabs
    };
}
export function isTerminalTabLayout(value) {
    return normalizeTerminalTabLayout(value) !== null;
}
function normalizeTerminalTabLayout(value) {
    if (!value || typeof value !== 'object') {
        return null;
    }
    const tab = value;
    if (typeof tab.id !== 'string' || typeof tab.title !== 'string' || typeof tab.activeTerminalId !== 'string') {
        return null;
    }
    const root = normalizeLayoutNode(tab.root);
    if (!root) {
        return null;
    }
    return {
        id: tab.id,
        title: tab.title,
        activeTerminalId: tab.activeTerminalId,
        root
    };
}
export function isLayoutNode(value) {
    return normalizeLayoutNode(value) !== null;
}
function normalizeLayoutNode(value) {
    if (!value || typeof value !== 'object') {
        return null;
    }
    let nodeCount = 0;
    return normalizeLayoutNodeAtDepth(value, 1, () => {
        nodeCount += 1;
        return nodeCount <= maxLayoutNodes;
    });
}
function normalizeLayoutNodeAtDepth(value, depth, consumeNode) {
    if (!value || typeof value !== 'object' || depth > maxLayoutNodeDepth || !consumeNode()) {
        return null;
    }
    const node = value;
    if (node.type === 'pane') {
        return typeof node.terminalId === 'string' ? { type: 'pane', terminalId: node.terminalId } : null;
    }
    if (node.type !== 'split' || (node.direction !== 'horizontal' && node.direction !== 'vertical')) {
        return null;
    }
    const first = normalizeLayoutNodeAtDepth(node.first, depth + 1, consumeNode);
    const second = normalizeLayoutNodeAtDepth(node.second, depth + 1, consumeNode);
    if (!first || !second) {
        return null;
    }
    return {
        type: 'split',
        direction: node.direction,
        ratio: normalizeRatio(node.ratio),
        first,
        second
    };
}
function normalizeRatio(value) {
    return typeof value === 'number' && Number.isFinite(value) ? Math.min(0.9, Math.max(0.1, value)) : 0.5;
}
//# sourceMappingURL=layoutState.js.map