"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.peel = exports.peel_ = exports.End = exports.Halt = exports.Emit = void 0;
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../../Effect"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const P = __importStar(require("../../../../Promise"));
const CH = __importStar(require("../../Channel"));
const SK = __importStar(require("../../Sink"));
const C = __importStar(require("../core"));
const HO = __importStar(require("../Handoff"));
const RunManaged = __importStar(require("./runManaged"));
const SignalTypeId = Symbol();
const EmitTypeId = Symbol();
class Emit {
    constructor(els) {
        this.els = els;
        this._signalTypeId = SignalTypeId;
        this._typeId = EmitTypeId;
    }
}
exports.Emit = Emit;
const HaltTypeId = Symbol();
class Halt {
    constructor(cause) {
        this.cause = cause;
        this._signalTypeId = SignalTypeId;
        this._typeId = HaltTypeId;
    }
}
exports.Halt = Halt;
const EndTypeId = Symbol();
class End {
    constructor() {
        this._signalTypeId = SignalTypeId;
        this._typeId = EndTypeId;
    }
}
exports.End = End;
/**
 * Peels off enough material from the stream to construct a `Z` using the
 * provided `Sink` and then returns both the `Z` and the rest of the
 * `Stream` in a managed resource. Like all `Managed` values, the provided
 * stream is valid only within the scope of `Managed`.
 */
function peel_(self, sink) {
    return (M.flatten(M.map_(M.bind_(M.bind_(M.do, "p", () => T.toManaged(P.make())), "handoff", () => T.toManaged(HO.make())), ({ handoff, p }) => {
        const consumer = SK.foldSink_(SK.exposeLeftover(sink), (e) => SK.zipRight_(SK.fromEffect(P.fail_(p, e)), SK.fail(e)), ({ tuple: [z1, leftovers] }) => {
            const loop = CH.readWithCause((in_) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, new Emit(in_))), loop), (e) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, new Halt(e))), CH.failCause(e)), (_) => CH.zipRight_(CH.fromEffect(HO.offer(handoff, new End())), CH.unit));
            return new SK.Sink(CH.zipRight_(CH.zipRight_(CH.fromEffect(P.succeed_(p, z1)), CH.fromEffect(HO.offer(handoff, new Emit(leftovers)))), loop));
        });
        const producer = CH.unwrap(T.map_(HO.take(handoff), (sig) => {
            switch (sig._typeId) {
                case EmitTypeId:
                    return CH.zipRight_(CH.write(sig.els), producer);
                case HaltTypeId:
                    return CH.failCause(sig.cause);
                default:
                    return CH.unit;
            }
        }));
        return (M.map_(M.chain_(M.fork(RunManaged.runManaged_(self, consumer)), (_) => T.toManaged(P.await(p))), (z) => Tp.tuple(z, new C.Stream(producer))));
    })));
}
exports.peel_ = peel_;
/**
 * Peels off enough material from the stream to construct a `Z` using the
 * provided `Sink` and then returns both the `Z` and the rest of the
 * `Stream` in a managed resource. Like all `Managed` values, the provided
 * stream is valid only within the scope of `Managed`.
 *
 * @ets_data_first peel_
 */
function peel(sink) {
    return (self) => peel_(self, sink);
}
exports.peel = peel;
//# sourceMappingURL=peel.js.map