"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeSimpleBlock = serializeSimpleBlock;
exports.deserializeSimpleBlocks = deserializeSimpleBlocks;
const common_js_1 = require("../../../imports/common.js");
const EBML = __importStar(require("./ebml/ebml.js"));
const ebml_js_1 = require("./ebml/ebml.js");
function serializeSimpleBlock(frame, isKeyframe, trackNumber, relativeTimestamp) {
    const elementId = Buffer.from([0xa3]);
    const contentSize = EBML.vintEncodedNumber(1 + 2 + 1 + frame.length).bytes;
    const keyframe = isKeyframe ? 1 : 0;
    const flags = new common_js_1.BitWriter2(8)
        .set(keyframe)
        .set(0, 3)
        .set(0)
        .set(0, 2)
        .set(0);
    const simpleBlock = Buffer.concat([
        elementId,
        contentSize,
        EBML.vintEncodedNumber(trackNumber).bytes,
        new common_js_1.BufferChain(2).writeInt16BE(relativeTimestamp).buffer,
        new common_js_1.BufferChain(1).writeUInt8(flags.value).buffer,
        frame,
    ]);
    return simpleBlock;
}
function deserializeSimpleBlocks(data) {
    const frames = [];
    let position = 0;
    while (position < data.length) {
        // Element ID (SimpleBlock = 0xA3)
        const elementId = data[position];
        if (elementId !== 0xa3) {
            throw new Error(`unexpected element id 0x${elementId.toString(16)}`);
        }
        position += 1;
        // Content size VINT
        const { value: contentSize, length: contentSizeLength } = (0, ebml_js_1.decodeVintEncodedNumber)(data, position);
        position += contentSizeLength;
        // Track number VINT (we don't actually need the value here but must know its length)
        const { value: trackNumber, length: trackNumberLength } = (0, ebml_js_1.decodeVintEncodedNumber)(data, position);
        position += trackNumberLength;
        // Timecode (signed int16)
        const timecode = data.readInt16BE(position);
        position += 2;
        // Flags (1 byte)
        const flags = new common_js_1.BitStream(data.subarray(position, position + 1));
        const isKeyframe = flags.readBits(1) === 1;
        position += 1;
        const metaSize = trackNumberLength + 2 + 1; // track + timecode + flags
        const remaining = Number(contentSize) - metaSize;
        if (remaining < 0) {
            throw new Error("invalid simple block size");
        }
        const frame = data.subarray(position, position + remaining);
        frames.push({
            data: frame,
            trackNumber: Number(trackNumber),
            isKeyframe,
            timecode,
        });
        position += remaining;
    }
    return frames;
}
//# sourceMappingURL=util.js.map