"use strict";
/**
 * Browser True Streaming Compression
 *
 * Uses native CompressionStream("deflate-raw") for real chunk-by-chunk streaming.
 * Falls back to buffered compression if not supported.
 *
 * API compatible with Node.js version - supports .on("data"), .on("end"), .write(callback), .end()
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasDeflateRaw = hasDeflateRaw;
exports.createDeflateStream = createDeflateStream;
exports.createInflateStream = createInflateStream;
const _stream_1 = require("../../stream/index.js");
const deflate_fallback_1 = require("./deflate-fallback.js");
const compress_base_1 = require("./compress.base.js");
const bytes_1 = require("../utils/bytes.js");
const defaults_1 = require("../defaults.js");
const streaming_compress_base_1 = require("./streaming-compress.base.js");
/**
 * Check if deflate-raw streaming compression is supported by this library.
 *
 * In browsers, the library always supports deflate-raw via the JS fallback.
 * Native deflate-raw Web Streams support (CompressionStream/DecompressionStream)
 * is used when available, but is not required.
 */
function hasDeflateRaw() {
    return true;
}
function hasNativeDeflateRawWebStreams() {
    return (0, compress_base_1.hasDeflateRawWebStreams)();
}
class WebStreamCodec extends _stream_1.EventEmitter {
    constructor(writer, reader) {
        super();
        this.ended = false;
        this.writer = writer;
        this.reader = reader;
        this.readPromise = this._startReading();
    }
    async _startReading() {
        try {
            while (true) {
                const { value, done } = await this.reader.read();
                if (done) {
                    break;
                }
                if (value) {
                    this.emit("data", value);
                }
            }
            this.emit("end");
        }
        catch (err) {
            this.emit("error", (0, streaming_compress_base_1.asError)(err));
        }
    }
    write(chunk, callback) {
        if (this.ended) {
            const err = new Error("write after end");
            if (callback) {
                callback(err);
            }
            else {
                this.emit("error", err);
            }
            return false;
        }
        this.writer
            .write(chunk)
            .then(() => {
            if (callback) {
                callback();
            }
        })
            .catch(err => {
            const error = (0, streaming_compress_base_1.asError)(err);
            if (callback) {
                callback(error);
            }
            else {
                this.emit("error", error);
            }
        });
        return true;
    }
    end(callback) {
        if (this.ended) {
            if (callback) {
                callback();
            }
            return;
        }
        this.ended = true;
        this.writer
            .close()
            .then(() => this.readPromise)
            .then(() => {
            if (callback) {
                callback();
            }
        })
            .catch(err => {
            const error = (0, streaming_compress_base_1.asError)(err);
            if (callback) {
                callback(error);
            }
            else {
                this.emit("error", error);
            }
        });
    }
    destroy(err) {
        this.ended = true;
        if (err) {
            this.emit("error", err);
        }
        try {
            this.reader.cancel(err);
        }
        catch {
            // Ignore
        }
        try {
            this.writer.abort(err);
        }
        catch {
            // Ignore
        }
    }
}
/**
 * Browser True Streaming Deflate using CompressionStream API
 * Simple EventEmitter-based - emits "data" as compressed chunks arrive
 */
class TrueStreamingDeflate extends _stream_1.EventEmitter {
    constructor(_level) {
        super();
        const compressionStream = new CompressionStream("deflate-raw");
        const writer = compressionStream.writable.getWriter();
        const reader = compressionStream.readable.getReader();
        this.codec = new WebStreamCodec(writer, reader);
        this.codec.on("data", chunk => this.emit("data", chunk));
        this.codec.on("end", () => this.emit("end"));
        this.codec.on("error", err => this.emit("error", err));
    }
    write(chunk, callback) {
        return this.codec.write(chunk, callback);
    }
    end(callback) {
        this.codec.end(callback);
    }
    destroy(err) {
        this.codec.destroy(err);
    }
}
/**
 * Fallback Deflate - buffers all data, compresses at end
 */
class FallbackDeflate extends _stream_1.EventEmitter {
    constructor(_level) {
        super();
        this.codec = new BufferedCodec(deflate_fallback_1.deflateRawCompressed);
        this.codec.on("data", chunk => this.emit("data", chunk));
        this.codec.on("end", () => this.emit("end"));
        this.codec.on("error", err => this.emit("error", err));
    }
    write(chunk, callback) {
        return this.codec.write(chunk, callback);
    }
    end(callback) {
        this.codec.end(callback);
    }
    destroy(err) {
        this.codec.destroy(err);
    }
}
class BufferedCodec extends _stream_1.EventEmitter {
    constructor(process) {
        super();
        this.process = process;
        this.chunks = [];
        this.ended = false;
    }
    write(chunk, callback) {
        if (this.ended) {
            const err = new Error("write after end");
            if (callback) {
                callback(err);
            }
            else {
                this.emit("error", err);
            }
            return false;
        }
        this.chunks.push(chunk);
        if (callback) {
            queueMicrotask(() => callback());
        }
        return true;
    }
    end(callback) {
        if (this.ended) {
            if (callback) {
                callback();
            }
            return;
        }
        this.ended = true;
        try {
            const data = (0, bytes_1.concatUint8Arrays)(this.chunks);
            const output = this.process(data);
            this.emit("data", output);
            this.emit("end");
            if (callback) {
                callback();
            }
        }
        catch (err) {
            const error = (0, streaming_compress_base_1.asError)(err);
            this.emit("error", error);
            if (callback) {
                callback(error);
            }
        }
    }
    destroy(err) {
        this.ended = true;
        if (err) {
            this.emit("error", err);
        }
    }
}
/**
 * Create a streaming DEFLATE compressor
 */
function createDeflateStream(options = {}) {
    const level = options.level ?? defaults_1.DEFAULT_COMPRESS_LEVEL;
    if (hasNativeDeflateRawWebStreams()) {
        return new TrueStreamingDeflate(level);
    }
    else {
        return new FallbackDeflate(level);
    }
}
/**
 * Browser True Streaming Inflate using DecompressionStream API
 */
class TrueStreamingInflate extends _stream_1.EventEmitter {
    constructor() {
        super();
        const decompressionStream = new DecompressionStream("deflate-raw");
        const writer = decompressionStream.writable.getWriter();
        const reader = decompressionStream.readable.getReader();
        this.codec = new WebStreamCodec(writer, reader);
        this.codec.on("data", chunk => this.emit("data", chunk));
        this.codec.on("end", () => this.emit("end"));
        this.codec.on("error", err => this.emit("error", err));
    }
    write(chunk, callback) {
        return this.codec.write(chunk, callback);
    }
    end(callback) {
        this.codec.end(callback);
    }
    destroy(err) {
        this.codec.destroy(err);
    }
}
/**
 * Fallback Inflate - buffers all data, decompresses at end
 */
class FallbackInflate extends _stream_1.EventEmitter {
    constructor() {
        super();
        this.codec = new BufferedCodec(deflate_fallback_1.inflateRaw);
        this.codec.on("data", chunk => this.emit("data", chunk));
        this.codec.on("end", () => this.emit("end"));
        this.codec.on("error", err => this.emit("error", err));
    }
    write(chunk, callback) {
        return this.codec.write(chunk, callback);
    }
    end(callback) {
        this.codec.end(callback);
    }
    destroy(err) {
        this.codec.destroy(err);
    }
}
/**
 * Create a streaming INFLATE decompressor
 */
function createInflateStream() {
    if (hasNativeDeflateRawWebStreams()) {
        return new TrueStreamingInflate();
    }
    else {
        return new FallbackInflate();
    }
}
