"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transduce = exports.transduce_ = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const AB = __importStar(require("../../../../Support/AtomicBoolean"));
const AR = __importStar(require("../../../../Support/AtomicReference"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
/**
 * Applies the transducer to the stream and emits its outputs.
 */
function transduce_(self, sink) {
    return new C.Stream(CH.suspend(() => {
        const leftovers = new AR.AtomicReference(CK.empty());
        const upstreamDone = new AB.AtomicBoolean(false);
        const buffer = CH.suspend(() => {
            const l = leftovers.get;
            if (CK.isEmpty(l)) {
                return CH.readWith((c) => CH.zipRight_(CH.write(c), buffer), (e) => CH.fail(e), (done) => CH.end(done));
            }
            else {
                leftovers.set(CK.empty());
                return CH.zipRight_(CH.writeChunk(l), buffer);
            }
        });
        const concatAndGet = (c) => {
            const ls = leftovers.get;
            const concat = CK.concat_(ls, CK.filter_(c, (a) => !CK.isEmpty(a)));
            leftovers.set(concat);
            return concat;
        };
        const upstreamMarker = CH.readWith((_in) => CH.zipRight_(CH.write(_in), upstreamMarker), (err) => CH.fail(err), (done) => CH.zipRight_(CH.succeedWith(() => upstreamDone.set(true)), CH.end(done)));
        const transducer = CH.chain_(CH.doneCollect(sink.channel), ({ tuple: [leftover, z] }) => CH.chain_(CH.succeedWith(() => Tp.tuple(upstreamDone.get, concatAndGet(leftover))), ({ tuple: [done, newLeftovers] }) => {
            const nextChannel = done && CK.isEmpty(newLeftovers) ? CH.end(undefined) : transducer;
            return CH.zipRight_(CH.write(CK.single(z)), nextChannel);
        }));
        return self.channel[">>>"](upstreamMarker)[">>>"](buffer)[">>>"](transducer);
    }));
}
exports.transduce_ = transduce_;
/**
 * Applies the transducer to the stream and emits its outputs.
 *
 * @ets_data_first transduce_
 */
function transduce(sink) {
    return (self) => transduce_(self, sink);
}
exports.transduce = transduce;
//# sourceMappingURL=transduce.js.map