/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import fs from 'node:fs';
import { Buffer } from 'node:buffer';
import { Deserializer } from './serialization.js';
export function serializeReplayUpdate(update) {
    const { lines, stickyHeaders, ...rest } = update;
    const result = {
        ...rest,
    };
    if (lines) {
        result.lines = {
            totalLength: lines.totalLength,
            updates: lines.updates.map(u => ({
                start: u.start,
                end: u.end,
                data: Buffer.from(u.data).toString('base64'),
                source: u.source ? Buffer.from(u.source).toString('base64') : undefined,
            })),
        };
    }
    if (stickyHeaders) {
        result.stickyHeaders = stickyHeaders.map(h => ({
            ...h,
            lines: Buffer.from(h.lines).toString('base64'),
            stuckLines: h.stuckLines
                ? Buffer.from(h.stuckLines).toString('base64')
                : undefined,
            styledOutput: Buffer.from(h.styledOutput).toString('base64'),
        }));
    }
    return result;
}
export function deserializeReplayUpdate(update) {
    const { lines, stickyHeaders, ...rest } = update;
    const result = {
        ...rest,
    };
    if (lines) {
        result.lines = {
            totalLength: lines.totalLength,
            updates: lines.updates.map(u => ({
                start: u.start,
                end: u.end,
                data: Buffer.from(u.data, 'base64'),
                source: u.source ? Buffer.from(u.source, 'base64') : undefined,
            })),
        };
    }
    if (stickyHeaders) {
        result.stickyHeaders = stickyHeaders.map(h => ({
            ...h,
            lines: Buffer.from(h.lines, 'base64'),
            stuckLines: h.stuckLines
                ? Buffer.from(h.stuckLines, 'base64')
                : undefined,
            styledOutput: Buffer.from(h.styledOutput, 'base64'),
        }));
    }
    return result;
}
export function saveReplay(data, filename) {
    fs.writeFileSync(filename, JSON.stringify(data, null, 2));
}
export function loadReplay(jsonStr) {
    const raw = JSON.parse(jsonStr);
    return {
        ...raw,
        frames: raw.frames.map(f => ({
            ...f,
            updates: f.updates.map(u => deserializeReplayUpdate(u)),
        })),
    };
}
export function createHumanReadableDump(data) {
    const dumpData = {
        type: data.type,
        columns: data.columns,
        rows: data.rows,
        frames: data.frames.map(frame => ({
            tree: frame.tree,
            cursorPosition: frame.cursorPosition,
            updates: frame.updates.map(update => {
                const dumpUpdate = { ...update };
                // Explicitly copy properties that could be undefined if omitted in object spread occasionally
                dumpUpdate['overflowToBackbuffer'] = update.overflowToBackbuffer;
                dumpUpdate['isScrollable'] = update.isScrollable;
                if (update.lines) {
                    dumpUpdate['lines'] = {
                        totalLength: update.lines.totalLength,
                        updates: update.lines.updates.map(u => {
                            const deserializer = new Deserializer(Buffer.from(u.data));
                            const lines = deserializer.deserialize();
                            return {
                                start: u.start,
                                end: u.end,
                                text: lines.map(line => line.getText().trimEnd()),
                            };
                        }),
                    };
                }
                if (update.stickyHeaders) {
                    dumpUpdate['stickyHeaders'] = update.stickyHeaders.map(h => ({
                        ...h,
                        lines: new Deserializer(Buffer.from(h.lines))
                            .deserialize()
                            .map(line => line.getText().trimEnd()),
                        stuckLines: h.stuckLines
                            ? new Deserializer(Buffer.from(h.stuckLines))
                                .deserialize()
                                .map(line => line.getText().trimEnd())
                            : undefined,
                        styledOutput: new Deserializer(Buffer.from(h.styledOutput))
                            .deserialize()
                            .map(line => line.getText().trimEnd()),
                        node: undefined,
                    }));
                }
                return dumpUpdate;
            }),
        })),
    };
    return JSON.stringify(dumpData, null, 2);
}
//# sourceMappingURL=replay.js.map