"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.bufferSignal = void 0
const Tp = __importStar(require("../../../Collections/Immutable/Tuple"))
const Ex = __importStar(require("../../../Exit"))
const Function_1 = require("../../../Function")
const P = __importStar(require("../../../Promise"))
const Q = __importStar(require("../../../Queue"))
const T = __importStar(require("../../_internal/effect"))
const M = __importStar(require("../../_internal/managed"))
const Ref = __importStar(require("../../_internal/ref"))
const Pull = __importStar(require("../../Pull"))
const Take = __importStar(require("../../Take"))
/**
 * Allows a faster producer to progress independently of a slower consumer by buffering
 * to the provided queue.
 */
function bufferSignal(self, queue) {
  return M.map_(
    M.let_(
      M.tap_(
        M.let_(
          M.bind_(
            M.bind_(
              M.tap_(
                M.bind_(
                  M.bind_(M.do, "as", () => self.proc),
                  "start",
                  () => T.toManaged(P.make())
                ),
                ({ start }) => T.toManaged(P.succeed_(start, undefined))
              ),
              "ref",
              ({ start }) => T.toManaged(Ref.makeRef(start))
            ),
            "done",
            () => T.toManaged(Ref.makeRef(false))
          ),
          "upstream",
          ({ as, ref }) => {
            const offer = (take) =>
              Ex.fold_(
                take,
                (_) =>
                  T.asUnit(
                    T.tap_(
                      T.tap_(
                        T.tap_(
                          T.bind_(
                            T.tap_(
                              T.bind_(T.do, "latch", () => ref.get),
                              ({ latch }) => P.await(latch)
                            ),
                            "p",
                            () => P.make()
                          ),
                          ({ p }) => Q.offer_(queue, Tp.tuple(take, p))
                        ),
                        ({ p }) => ref.set(p)
                      ),
                      ({ p }) => P.await(p)
                    )
                  ),
                (_) =>
                  T.asUnit(
                    T.tap_(
                      T.bind_(
                        T.bind_(T.do, "p", () => P.make()),
                        "added",
                        ({ p }) => Q.offer_(queue, Tp.tuple(take, p))
                      ),
                      ({ added, p }) => T.when_(ref.set(p), () => added)
                    )
                  )
              )
            return T.asUnit(
              T.repeatWhile_(
                T.tap_(Take.fromPull(as), (take) => offer(take)),
                (_) => _ !== Take.end
              )
            )
          }
        ),
        ({ upstream }) => M.fork(T.toManaged(upstream))
      ),
      "pull",
      ({ done }) =>
        T.chain_(done.get, (_) => {
          if (_) {
            return Pull.end
          } else {
            return T.chain_(Q.take(queue), ({ tuple: [take, p] }) =>
              T.zipRight_(
                T.zipRight_(
                  P.succeed_(p, undefined),
                  T.when_(done.set(true), () => take === Take.end)
                ),
                Take.done(take)
              )
            )
          }
        })
    ),
    ({ pull }) => pull
  )
}
exports.bufferSignal = bufferSignal
//# sourceMappingURL=bufferSignal.js.map
