"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.untilOutputEffect = exports.untilOutputEffect_ = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../Collections/Immutable/Chunk"));
const T = __importStar(require("../../../Effect"));
const Function_1 = require("../../../Function");
const O = __importStar(require("../../../Option"));
const Ref = __importStar(require("../../../Ref"));
const CH = __importStar(require("../Channel"));
const C = __importStar(require("./core"));
/**
 * Creates a sink that produces values until one verifies
 * the predicate `f`.
 */
function untilOutputEffect_(self, 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(_), (_) => CH.as_(CH.fromEffect(upstreamDoneRef.set(true)), _));
        const loop = CH.foldChannel_(CH.doneCollect(self.channel), CH.fail, ({ tuple: [leftovers, doneValue] }) => (CH.map_(CH.bind("res", ({ satisfied, upstreamDone }) => {
            if (satisfied) {
                return CH.as_(CH.write(CK.flatten(leftovers)), O.some(doneValue));
            }
            else if (upstreamDone) {
                return CH.as_(CH.write(CK.flatten(leftovers)), O.none);
            }
            else {
                return loop;
            }
        })(CH.bind("upstreamDone", () => CH.fromEffect(upstreamDoneRef.get))(CH.bind("_", () => CH.fromEffect(leftoversRef.set(CK.flatten(leftovers))))(CH.bind("satisfied", () => CH.fromEffect(f(doneValue)))(CH.do)))), ({ res }) => res)));
        return upstreamMarker[">>>"](CH.bufferChunk(leftoversRef))[">>>"](loop);
    }));
}
exports.untilOutputEffect_ = untilOutputEffect_;
/**
 * Creates a sink that produces values until one verifies
 * the predicate `f`.
 *
 * @ets_data_first untilOutputEffect_
 */
function untilOutputEffect(f) {
    return (self) => untilOutputEffect_(self, f);
}
exports.untilOutputEffect = untilOutputEffect;
//# sourceMappingURL=untilOutputEffect.js.map