// Copyright 2026 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { ElementTemplateUpdateOps } from '../protocol/opcodes.js';
export function formatElementTemplateUpdateCommands(stream) {
    if (!Array.isArray(stream)) {
        return [];
    }
    const result = [];
    for (let index = 0; index < stream.length;) {
        const opIndex = index;
        const op = stream[index++];
        switch (op) {
            case ElementTemplateUpdateOps.createTemplate:
                result.push({
                    op: 'createTemplate',
                    handleId: stream[index++],
                    templateKey: stream[index++],
                    bundleUrl: stream[index++],
                    attributeSlots: stream[index++],
                    elementSlots: stream[index++],
                });
                break;
            case ElementTemplateUpdateOps.setAttribute:
                result.push({
                    op: 'setAttribute',
                    targetId: stream[index++],
                    attrSlotIndex: stream[index++],
                    value: stream[index++],
                });
                break;
            case ElementTemplateUpdateOps.insertNode:
                result.push({
                    op: 'insertNode',
                    targetId: stream[index++],
                    elementSlotIndex: stream[index++],
                    childId: stream[index++],
                    referenceId: stream[index++],
                });
                break;
            case ElementTemplateUpdateOps.removeNode:
                result.push({
                    op: 'removeNode',
                    targetId: stream[index++],
                    elementSlotIndex: stream[index++],
                    childId: stream[index++],
                    removedSubtreeHandleIds: stream[index++],
                });
                break;
            default:
                result.push({
                    op: 'unknown',
                    opcode: op,
                    index: opIndex,
                    remaining: stream.slice(index),
                });
                index = stream.length;
                break;
        }
    }
    return result;
}
export function printElementTemplateTreeToString(root) {
    if (!root) {
        return '<empty>';
    }
    const lines = [];
    appendInstance(lines, root, 0);
    return lines.join('\n');
}
function appendInstance(lines, instance, depth) {
    const indent = '  '.repeat(depth);
    const type = instance.type ?? '<unknown>';
    const instanceId = instance.instanceId ?? '<unknown>';
    lines.push(`${indent}${type}#${instanceId}`);
    if (Array.isArray(instance.attributeSlots) && instance.attributeSlots.length > 0) {
        lines.push(`${indent}  attributeSlots: ${JSON.stringify(instance.attributeSlots)}`);
    }
    const elementSlots = Array.isArray(instance.elementSlots) ? instance.elementSlots : [];
    for (let slotIndex = 0; slotIndex < elementSlots.length; slotIndex += 1) {
        const children = elementSlots[slotIndex];
        if (!children || children.length === 0) {
            continue;
        }
        lines.push(`${indent}  elementSlots[${slotIndex}]: [${children.map(child => child.instanceId).join(', ')}]`);
    }
    let child = instance.firstChild;
    while (child) {
        appendInstance(lines, child, depth + 1);
        child = child.nextSibling;
    }
}
//# sourceMappingURL=alog.js.map