import * as tracing_1 from "../../../Tracing"
import * as tracing_2 from "../../../Tracing"
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/_internal/Handoff.ts"
import * as Tp from "../../../Collections/Immutable/Tuple"
import * as T from "../../../Effect"
import { pipe } from "../../../Function"
import * as O from "../../../Option"
import * as P from "../../../Promise"
import * as Ref from "../../../Ref"
export class Handoff {
  constructor(ref) {
    this.ref = ref
  }
}
export 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"
  )
}
export const StateTypeId = /*#__PURE__*/ Symbol()
export const EmptyTypeId = /*#__PURE__*/ Symbol()
export class Empty {
  constructor(notifyConsumer) {
    this.notifyConsumer = notifyConsumer
    this._stateTypeId = StateTypeId
    this._typeId = EmptyTypeId
  }
}
export const FullTypeId = /*#__PURE__*/ Symbol()
export class Full {
  constructor(a, notifyConsumer) {
    this.a = a
    this.notifyConsumer = notifyConsumer
    this._stateTypeId = StateTypeId
    this._typeId = FullTypeId
  }
}
export function offer(handoff, a) {
  return T.chain_(
    P.make(),
    (p) => {
      return T.flatten(
        Ref.modify_(handoff.ref, (s) => {
          if (s._typeId === 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"
  )
}
export function take(handoff) {
  return T.chain_(
    P.make(),
    (p) => {
      return T.flatten(
        Ref.modify_(handoff.ref, (s) => {
          if (s._typeId === 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"
  )
}
export function poll(handoff) {
  return T.chain_(
    P.make(),
    (p) => {
      return T.flatten(
        Ref.modify_(handoff.ref, (s) => {
          if (s._typeId === 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"
  )
}
export const HandoffSignalTypeId = /*#__PURE__*/ Symbol()
export const EmitTypeId = /*#__PURE__*/ Symbol()
export class Emit {
  constructor(els) {
    this.els = els
    this._handoffSignalTypeId = HandoffSignalTypeId
    this._typeId = EmitTypeId
  }
}
export const HaltTypeId = /*#__PURE__*/ Symbol()
export class Halt {
  constructor(error) {
    this.error = error
    this._handoffSignalTypeId = HandoffSignalTypeId
    this._typeId = HaltTypeId
  }
}
export const EndTypeId = /*#__PURE__*/ Symbol()
export class End {
  constructor(reason) {
    this.reason = reason
    this._handoffSignalTypeId = HandoffSignalTypeId
    this._typeId = EndTypeId
  }
}
//# sourceMappingURL=Handoff.js.map
