"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.collectAllWhileWith = exports.collectAllWhileWith_ = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../Collections/Immutable/Chunk"));
const T = __importStar(require("../../../Effect"));
const Function_1 = require("../../../Function");
const Ref = __importStar(require("../../../Ref"));
const CH = __importStar(require("../Channel"));
const C = __importStar(require("./core"));
/**
 * Repeatedly runs the sink for as long as its results satisfy
 * the predicate `p`. The sink's results will be accumulated
 * using the stepping function `f`.
 */
function collectAllWhileWith_(self, z, p, f) {
    return new C.Sink((CH.chain_(CH.fromEffect(T.zip_(Ref.makeRef(CK.empty()), Ref.makeRef(false))), ({ tuple: [leftoversRef, upstreamDoneRef] }) => {
        const upstreamMarker = CH.readWith((in_) => CH.zipRight_(CH.write(in_), upstreamMarker), (_) => CH.fail(_), (x) => CH.as_(CH.fromEffect(upstreamDoneRef.set(true)), x));
        const loop = (currentResult) => CH.foldChannel_(CH.doneCollect(self.channel), (_) => CH.fail(_), ({ tuple: [leftovers, doneValue] }) => {
            if (p(doneValue)) {
                return (CH.map_(CH.bind("result", ({ accumulatedResult, upstreamDone }) => upstreamDone
                    ? CH.as_(CH.write(CK.flatten(leftovers)), currentResult)
                    : loop(accumulatedResult))(CH.let("accumulatedResult", () => f(currentResult, doneValue))(CH.bind("upstreamDone", () => CH.fromEffect(upstreamDoneRef.get))(CH.fromEffect(leftoversRef.set(CK.flatten(leftovers)))))), ({ result }) => result));
            }
            else {
                return CH.as_(CH.write(CK.flatten(leftovers)), currentResult);
            }
        });
        return upstreamMarker[">>>"](CH.bufferChunk(leftoversRef))[">>>"](loop(z));
    })));
}
exports.collectAllWhileWith_ = collectAllWhileWith_;
/**
 * Repeatedly runs the sink for as long as its results satisfy
 * the predicate `p`. The sink's results will be accumulated
 * using the stepping function `f`.
 *
 * @ets_data_first collectAllWhileWith_
 */
function collectAllWhileWith(z, p, f) {
    return (self) => collectAllWhileWith_(self, z, p, f);
}
exports.collectAllWhileWith = collectAllWhileWith;
//# sourceMappingURL=collectAllWhileWith.js.map