"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.End =
  exports.EndTypeId =
  exports.Halt =
  exports.HaltTypeId =
  exports.Emit =
  exports.EmitTypeId =
  exports.HandoffSignalTypeId =
  exports.poll =
  exports.take =
  exports.offer =
  exports.Full =
  exports.FullTypeId =
  exports.Empty =
  exports.EmptyTypeId =
  exports.StateTypeId =
  exports.make =
  exports.Handoff =
    void 0
const tracing_1 = __importStar(require("../../../Tracing"))
const tracing_2 = __importStar(require("../../../Tracing"))
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/_internal/Handoff.ts"
const Tp = __importStar(require("../../../Collections/Immutable/Tuple"))
const T = __importStar(require("../../../Effect"))
const Function_1 = require("../../../Function")
const O = __importStar(require("../../../Option"))
const P = __importStar(require("../../../Promise"))
const Ref = __importStar(require("../../../Ref"))
class Handoff {
  constructor(ref) {
    this.ref = ref
  }
}
exports.Handoff = Handoff
function make() {
  return T.map_(
    T.chain_(P.make(), (p) => Ref.makeRef(new Empty(p)), fileName_1 + ":18:12"),
    (_) => new Handoff(_),
    fileName_1 + ":19:10"
  )
}
exports.make = make
exports.StateTypeId = Symbol()
exports.EmptyTypeId = Symbol()
class Empty {
  constructor(notifyConsumer) {
    this.notifyConsumer = notifyConsumer
    this._stateTypeId = exports.StateTypeId
    this._typeId = exports.EmptyTypeId
  }
}
exports.Empty = Empty
exports.FullTypeId = Symbol()
class Full {
  constructor(a, notifyConsumer) {
    this.a = a
    this.notifyConsumer = notifyConsumer
    this._stateTypeId = exports.StateTypeId
    this._typeId = exports.FullTypeId
  }
}
exports.Full = Full
function offer(handoff, a) {
  return T.chain_(
    P.make(),
    (p) => {
      return T.flatten(
        Ref.modify_(handoff.ref, (s) => {
          if (s._typeId === exports.FullTypeId) {
            return Tp.tuple(
              T.zipRight_(
                P.await(s.notifyConsumer),
                offer(handoff, a),
                fileName_1 + ":49:38"
              ),
              s
            )
          } else {
            return Tp.tuple(
              T.zipRight_(
                P.succeed_(s.notifyConsumer, undefined),
                P.await(p),
                fileName_1 + ":52:24"
              ),
              new Full(a, p)
            )
          }
        }),
        fileName_1 + ":57:16"
      )
    },
    fileName_1 + ":44:18"
  )
}
exports.offer = offer
function take(handoff) {
  return T.chain_(
    P.make(),
    (p) => {
      return T.flatten(
        Ref.modify_(handoff.ref, (s) => {
          if (s._typeId === exports.FullTypeId) {
            return Tp.tuple(
              T.as_(
                P.succeed_(s.notifyConsumer, undefined),
                s.a,
                fileName_1 + ":69:18"
              ),
              new Empty(p)
            )
          } else {
            return Tp.tuple(
              T.zipRight_(
                P.await(s.notifyConsumer),
                take(handoff),
                fileName_1 + ":73:38"
              ),
              s
            )
          }
        }),
        fileName_1 + ":76:16"
      )
    },
    fileName_1 + ":63:18"
  )
}
exports.take = take
function poll(handoff) {
  return T.chain_(
    P.make(),
    (p) => {
      return T.flatten(
        Ref.modify_(handoff.ref, (s) => {
          if (s._typeId === exports.FullTypeId) {
            return Tp.tuple(
              T.as_(
                P.succeed_(s.notifyConsumer, undefined),
                O.some(s.a),
                fileName_1 + ":88:18"
              ),
              new Empty(p)
            )
          } else {
            return Tp.tuple(T.succeed(O.none, fileName_1 + ":92:36"), s)
          }
        }),
        fileName_1 + ":95:16"
      )
    },
    fileName_1 + ":82:18"
  )
}
exports.poll = poll
exports.HandoffSignalTypeId = Symbol()
exports.EmitTypeId = Symbol()
class Emit {
  constructor(els) {
    this.els = els
    this._handoffSignalTypeId = exports.HandoffSignalTypeId
    this._typeId = exports.EmitTypeId
  }
}
exports.Emit = Emit
exports.HaltTypeId = Symbol()
class Halt {
  constructor(error) {
    this.error = error
    this._handoffSignalTypeId = exports.HandoffSignalTypeId
    this._typeId = exports.HaltTypeId
  }
}
exports.Halt = Halt
exports.EndTypeId = Symbol()
class End {
  constructor(reason) {
    this.reason = reason
    this._handoffSignalTypeId = exports.HandoffSignalTypeId
    this._typeId = exports.EndTypeId
  }
}
exports.End = End
//# sourceMappingURL=Handoff.js.map
