"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.make =
  exports.pullChunk =
  exports.pullElement =
  exports.update =
  exports.ifNotDone =
  exports.ifNotDone_ =
  exports.BufferedPull =
    void 0
// tracing: off
require("../../Operator")
const A = __importStar(require("../../Collections/Immutable/Chunk"))
const Tp = __importStar(require("../../Collections/Immutable/Tuple"))
const Function_1 = require("../../Function")
const O = __importStar(require("../../Option"))
const T = __importStar(require("../_internal/effect"))
const R = __importStar(require("../_internal/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 ifNotDone_(self, fa) {
  return T.chain_(self.done.get, (b) => (b ? T.fail(O.none) : fa))
}
exports.ifNotDone_ = 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.chain_(self.done.set(true), () => Pull.end),
        (e) => Pull.fail(e)
      ),
      (a) => self.cursor.set(Tp.tuple(a, 0))
    )
  )
}
exports.update = update
function pullElement(self) {
  return ifNotDone_(
    self,
    T.flatten(
      R.modify_(self.cursor, ({ tuple: [c, i] }) => {
        if (i >= A.size(c)) {
          return Tp.tuple(
            T.chain_(update(self), () => pullElement(self)),
            Tp.tuple(A.empty(), 0)
          )
        } else {
          return Tp.tuple(T.succeed(A.unsafeGet_(c, i)), Tp.tuple(c, i + 1))
        }
      })
    )
  )
}
exports.pullElement = pullElement
function pullChunk(self) {
  return ifNotDone_(
    self,
    T.flatten(
      R.modify_(self.cursor, ({ tuple: [chunk, idx] }) => {
        if (idx >= A.size(chunk)) {
          return Tp.tuple(
            T.chain_(update(self), () => pullChunk(self)),
            Tp.tuple(A.empty(), 0)
          )
        } else {
          return Tp.tuple(T.succeed(A.drop_(chunk, idx)), Tp.tuple(A.empty(), 0))
        }
      })
    )
  )
}
exports.pullChunk = pullChunk
function make(pull) {
  return T.map_(
    T.bind_(
      T.bind_(T.do, "done", () => R.makeRef(false)),
      "cursor",
      () => R.makeRef(Tp.tuple(A.empty(), 0))
    ),
    ({ cursor, done }) => new BufferedPull(pull, done, cursor)
  )
}
exports.make = make
//# sourceMappingURL=index.js.map
