import { z } from "zod";
import { TerminalStateSchema } from "./messages.js";
export const TerminalStreamResizeSchema = z
    .object({
    rows: z.number().int().positive(),
    cols: z.number().int().positive(),
})
    .strict();
export const TerminalStreamOpcode = {
    Output: 0x01,
    Input: 0x02,
    Resize: 0x03,
    Snapshot: 0x04,
};
export function asUint8Array(data) {
    if (typeof data === "string") {
        if (typeof TextEncoder !== "undefined") {
            return new TextEncoder().encode(data);
        }
        if (typeof Buffer !== "undefined") {
            return new Uint8Array(Buffer.from(data, "utf8"));
        }
        const out = new Uint8Array(data.length);
        for (let index = 0; index < data.length; index += 1) {
            out[index] = data.charCodeAt(index) & 0xff;
        }
        return out;
    }
    if (data instanceof Uint8Array) {
        return data;
    }
    if (typeof ArrayBuffer !== "undefined" && data instanceof ArrayBuffer) {
        return new Uint8Array(data);
    }
    if (ArrayBuffer.isView(data)) {
        return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
    }
    if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
        return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
    }
    return null;
}
function isTerminalStreamOpcode(value) {
    return (value === TerminalStreamOpcode.Output ||
        value === TerminalStreamOpcode.Input ||
        value === TerminalStreamOpcode.Resize ||
        value === TerminalStreamOpcode.Snapshot);
}
export function encodeTerminalStreamFrame(input) {
    const payload = asUint8Array(input.payload ?? new Uint8Array(0)) ?? new Uint8Array(0);
    const bytes = new Uint8Array(2 + payload.byteLength);
    bytes[0] = input.opcode;
    bytes[1] = input.slot & 0xff;
    bytes.set(payload, 2);
    return bytes;
}
export function decodeTerminalStreamFrame(bytes) {
    if (bytes.byteLength < 2) {
        return null;
    }
    const opcode = bytes[0];
    if (!isTerminalStreamOpcode(opcode)) {
        return null;
    }
    return {
        opcode,
        slot: bytes[1],
        payload: bytes.subarray(2),
    };
}
export function encodeTerminalSnapshotPayload(state) {
    return encodeJsonPayload(state);
}
export function decodeTerminalSnapshotPayload(bytes) {
    const parsed = decodeJsonPayload(bytes);
    const result = TerminalStateSchema.safeParse(parsed);
    return result.success ? result.data : null;
}
export function encodeTerminalResizePayload(input) {
    return encodeJsonPayload(input);
}
export function decodeTerminalResizePayload(bytes) {
    const parsed = decodeJsonPayload(bytes);
    const result = TerminalStreamResizeSchema.safeParse(parsed);
    return result.success ? result.data : null;
}
function encodeJsonPayload(value) {
    return new TextEncoder().encode(JSON.stringify(value));
}
function decodeJsonPayload(bytes) {
    try {
        const text = new TextDecoder().decode(bytes);
        return JSON.parse(text);
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=terminal-stream-protocol.js.map