"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PipeError = void 0;
exports.safePipe = safePipe;
exports.withResourceCleanup = withResourceCleanup;
const __ΩError = ['name', 'message', 'stack', 'Error', 'P&4!&4"&4#8Mw$y'];
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const promises_1 = require("fs/promises");
class PipeError extends Error {
    constructor(cause, side) {
        super(cause.message);
        this.cause = cause;
        this.side = side;
    }
}
exports.PipeError = PipeError;
PipeError.__type = [() => Error, () => __ΩError, 'cause', "input", "output", 'side', 'constructor', 'PipeError', 'P7!Pn"2#:9P.$.%J2&:9"0\'5w('];
function safePipe(input, output) {
    return (Promise.Ω = [['$']], new Promise(__assignType((resolve, reject) => {
        input.on('close', () => {
            if (!input.readableEnded) {
                if (!output.destroyed)
                    output.destroy();
                reject(new PipeError(new Error('Input stream aborted'), 'input'));
            }
        });
        input.on('error', __assignType(err => {
            if (!output.destroyed)
                output.destroy();
            reject(new PipeError(err, 'input'));
        }, ['err', '', 'P"2!"/"']));
        output.on('close', () => {
            if (!output.writableEnded) {
                if (!input.destroyed)
                    input.destroy();
                reject(new PipeError(new Error('Output stream aborted'), 'output'));
            }
        });
        output.on('error', __assignType(err => {
            if (!input.destroyed)
                input.destroy();
            reject(new PipeError(err, 'output'));
        }, ['err', '', 'P"2!"/"']));
        output.on('finish', () => {
            resolve();
        });
        input.pipe(output);
    }, ['resolve', 'reject', '', 'P"2!"2""/#'])));
}
safePipe.__type = ['input', 'output', 'safePipe', 'P!2!!2""/#'];
class ResourceTracker {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    constructor(reject) {
        this.reject = reject;
        this.files = [];
        this.streams = [];
    }
    addStream(stream) {
        stream.on('error', this.reject);
        this.streams.push(stream);
    }
    addFile(file) {
        this.files.push(file);
    }
}
ResourceTracker.__type = ['files', function () { return []; }, 'streams', function () { return []; }, 'err', '', 'reject', 'constructor', 'stream', 'addStream', 'file', 'addFile', 'ResourceTracker', '&F3!>"P!!JF3#>$PP"2%$/&2\';"0(PP!!J2)"0*P&2+"0,5w-'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function withResourceCleanup(fn, onError) {
    return (Promise.Ω = [[() => fn, () => ResourceTracker, 'tracker', '', 'Pdi!PP7"2#h"!`/$qe!!!j']], new Promise(__assignType((resolve, reject) => {
        const tracker = new ResourceTracker(reject);
        fn(tracker)
            .then(resolve, __assignType(err => {
            onError?.(err);
            reject(err);
        }, ['err', '', 'P"2!"/"']))
            .finally(() => {
            tracker.files.forEach(__assignType(file => {
                (0, promises_1.unlink)(file).catch(() => { });
            }, ['file', '', 'P"2!"/"']));
            tracker.streams.forEach(__assignType(stream => {
                if (!stream.destroyed)
                    stream.destroy();
            }, ['stream', '', 'P"2!"/"']));
        });
    }, ['resolve', 'reject', '', 'P"2!"2""/#'])));
}
withResourceCleanup.__type = [() => ResourceTracker, 'tracker', '', 'fn', 'err', 'onError', 'withResourceCleanup', 'PPP7!2""`/#2$P"2%$/#2&8"/\''];
//# sourceMappingURL=stream.js.map