"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.pullChunk = exports.pullElement = exports.update = exports.ifNotDone = exports.ifNotDone_ = exports.make = exports.BufferedPull = void 0;
const tracing_1 = __importStar(require("../../../Tracing"));
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/_internal/BufferedPull.ts";
const A = __importStar(require("../../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../Effect"));
const Function_1 = require("../../../Function");
const O = __importStar(require("../../../Option"));
const Ref = __importStar(require("../../../Ref"));
const Pull = __importStar(require("../Pull"));
class BufferedPull {
    constructor(upstream, done, cursor) {
        this.upstream = upstream;
        this.done = done;
        this.cursor = cursor;
    }
}
exports.BufferedPull = BufferedPull;
function make(upstream) {
    return (T.map_(T.bind_(T.bind_(T.do, "done", () => Ref.makeRef(false), fileName_1 + ":20:11"), "cursor", () => Ref.makeRef(Tp.tuple(A.empty(), 0)), fileName_1 + ":21:11"), ({ cursor, done }) => new BufferedPull(upstream, done, cursor), fileName_1 + ":22:10"));
}
exports.make = make;
function ifNotDone_(self, fa) {
    return T.chain_(Ref.get(self.done), (_) => {
        if (_) {
            return Pull.end;
        }
        else {
            return fa;
        }
    }, fileName_1 + ":30:18");
}
exports.ifNotDone_ = ifNotDone_;
/**
 * @ets_data_first ifNotDone_
 */
function ifNotDone(fa) {
    return (self) => ifNotDone_(self, fa);
}
exports.ifNotDone = ifNotDone;
function update(self) {
    return ifNotDone_(self, T.foldM_(self.upstream, O.fold(() => T.zipRight_(Ref.set_(self.done, true), Pull.end, fileName_1 + ":54:26"), (e) => Pull.fail(e)), (chunk) => Ref.set_(self.cursor, Tp.tuple(chunk, 0)), fileName_1 + ":51:13"));
}
exports.update = update;
function pullElement(self) {
    return ifNotDone_(self, (T.flatten(Ref.modify_(self.cursor, ({ tuple: [chunk, idx] }) => {
        if (idx >= A.size(chunk)) {
            return Tp.tuple(T.zipRight_(update(self), pullElement(self), fileName_1 + ":72:24"), Tp.tuple(A.empty(), 0));
        }
        else {
            return Tp.tuple(T.succeed(A.unsafeGet_(chunk, idx), fileName_1 + ":77:22"), Tp.tuple(A.empty(), idx + 1));
        }
    }), fileName_1 + ":82:16")));
}
exports.pullElement = pullElement;
function pullChunk(self) {
    return ifNotDone_(self, (T.flatten(Ref.modify_(self.cursor, ({ tuple: [chunk, idx] }) => {
        if (idx >= A.size(chunk)) {
            return Tp.tuple(T.zipRight_(update(self), pullChunk(self), fileName_1 + ":97:24"), Tp.tuple(A.empty(), 0));
        }
        else {
            return Tp.tuple(T.succeed(A.drop_(chunk, idx), fileName_1 + ":101:36"), Tp.tuple(A.empty(), 0));
        }
    }), fileName_1 + ":104:16")));
}
exports.pullChunk = pullChunk;
//# sourceMappingURL=BufferedPull.js.map