"use strict";
/**
 * EncodedAudioChunk - Represents a chunk of encoded audio data
 * Implements the W3C WebCodecs EncodedAudioChunk interface
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedAudioChunk = void 0;
class EncodedAudioChunk {
    constructor(init) {
        if (!init.type) {
            throw new TypeError('type is required');
        }
        if (init.type !== 'key' && init.type !== 'delta') {
            throw new TypeError('type must be "key" or "delta"');
        }
        if (init.timestamp === undefined) {
            throw new TypeError('timestamp is required');
        }
        if (!init.data) {
            throw new TypeError('data is required');
        }
        this.type = init.type;
        this.timestamp = init.timestamp;
        this.duration = init.duration ?? null;
        // Copy the data
        let source;
        if (init.data instanceof ArrayBuffer) {
            source = new Uint8Array(init.data);
        }
        else {
            source = new Uint8Array(init.data.buffer, init.data.byteOffset, init.data.byteLength);
        }
        this._data = new Uint8Array(source);
        this.byteLength = this._data.byteLength;
    }
    /**
     * Copy the encoded data to a destination buffer
     */
    copyTo(destination) {
        let dest;
        if (destination instanceof ArrayBuffer) {
            dest = new Uint8Array(destination);
        }
        else {
            dest = new Uint8Array(destination.buffer, destination.byteOffset, destination.byteLength);
        }
        if (dest.byteLength < this._data.byteLength) {
            throw new TypeError('Destination buffer is too small');
        }
        dest.set(this._data);
    }
}
exports.EncodedAudioChunk = EncodedAudioChunk;
//# sourceMappingURL=EncodedAudioChunk.js.map