"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.poll = exports.take = exports.offer = exports.offer_ = exports.make = void 0
// tracing: off
require("../../Operator")
const Tp = __importStar(require("../../Collections/Immutable/Tuple"))
const Function_1 = require("../../Function")
const Option_1 = require("../../Option")
const P = __importStar(require("../../Promise"))
const Utils_1 = require("../../Utils")
const T = __importStar(require("../_internal/effect"))
const R = __importStar(require("../_internal/ref"))
class Empty {
  constructor(notifyConsumer) {
    this.notifyConsumer = notifyConsumer
    this._tag = "Empty"
  }
}
class Full {
  constructor(a, notifyProducer) {
    this.a = a
    this.notifyProducer = notifyProducer
    this._tag = "Full"
  }
}
/**
 * A synchronous queue-like abstraction that allows a producer to offer
 * an element and wait for it to be taken, and allows a consumer to wait
 * for an element to be available.
 */
class Handoff {
  constructor(ref) {
    this.ref = ref
    this._tag = "Handoff"
  }
}
function make() {
  return T.map_(
    T.chain_(P.make(), (p) => R.makeRef(new Empty(p))),
    (ref) => new Handoff(ref)
  )
}
exports.make = make
function offer_(h, a) {
  return T.chain_(P.make(), (p) =>
    T.flatten(
      R.modify_(
        h.ref,
        (0, Utils_1.matchTag)({
          Empty: ({ notifyConsumer }) =>
            Tp.tuple(
              T.zipRight_(
                P.succeed((0, Function_1.constVoid)())(notifyConsumer),
                P.await(p)
              ),
              new Full(a, p)
            ),
          Full: (s) =>
            Tp.tuple(
              T.chain_(P.await(s.notifyProducer), () => offer_(h, a)),
              s
            )
        })
      )
    )
  )
}
exports.offer_ = offer_
function offer(a) {
  return (h) => offer_(h, a)
}
exports.offer = offer
function take(h) {
  return T.chain_(P.make(), (p) =>
    T.flatten(
      R.modify_(
        h.ref,
        (0, Utils_1.matchTag)({
          Empty: (s) =>
            Tp.tuple(
              T.chain_(P.await(s.notifyConsumer), () => take(h)),
              s
            ),
          Full: ({ a, notifyProducer }) =>
            Tp.tuple(
              T.as_(P.succeed((0, Function_1.constVoid)())(notifyProducer), a),
              new Empty(p)
            )
        })
      )
    )
  )
}
exports.take = take
function poll(h) {
  return T.chain_(P.make(), (p) =>
    T.flatten(
      R.modify_(
        h.ref,
        (0, Utils_1.matchTag)({
          Empty: (s) => Tp.tuple(T.succeed(Option_1.none), s),
          Full: ({ a, notifyProducer }) =>
            Tp.tuple(
              T.as_(
                P.succeed((0, Function_1.constVoid)())(notifyProducer),
                (0, Option_1.some)(a)
              ),
              new Empty(p)
            )
        })
      )
    )
  )
}
exports.poll = poll
//# sourceMappingURL=index.js.map
