// tracing: off
import "../../../../Operator"
import * as Cause from "../../../../Cause"
import * as Tp from "../../../../Collections/Immutable/Tuple"
import * as T from "../../../../Effect"
import * as E from "../../../../Either"
import * as Exit from "../../../../Exit"
import * as P from "../../../../Promise"
import * as Ref from "../../../../Ref"
export const DoneTypeId = /*#__PURE__*/ Symbol()
export class StateDone {
  constructor(a) {
    this.a = a
    this._typeId = DoneTypeId
  }
}
export const ErrorTypeId = /*#__PURE__*/ Symbol()
export class StateError {
  constructor(cause) {
    this.cause = cause
    this._typeId = ErrorTypeId
  }
}
export const EmptyTypeId = /*#__PURE__*/ Symbol()
export class StateEmpty {
  constructor(notifyConsumer) {
    this.notifyConsumer = notifyConsumer
    this._typeId = EmptyTypeId
  }
}
export const EmitTypeId = /*#__PURE__*/ Symbol()
export class StateEmit {
  constructor(a, notifyProducer) {
    this.a = a
    this.notifyProducer = notifyProducer
    this._typeId = EmitTypeId
  }
}
/**
 * An MVar-like abstraction for sending data to channels asynchronously. Designed
 * for one producer and multiple consumers.
 *
 * Features the following semantics:
 * - Buffer of size 1
 * - When emitting, the producer waits for a consumer to pick up the value
 *   to prevent "reading ahead" too much.
 * - Once an emitted element is read by a consumer, it is cleared from the buffer, so that
 *   at most one consumer sees every emitted element.
 * - When sending a done or error signal, the producer does not wait for a consumer
 *   to pick up the signal. The signal stays in the buffer after being read by a consumer,
 *   so it can be propagated to multiple consumers.
 * - Trying to publish another emit/error/done after an error/done have already been published
 *   results in an interruption.
 */

export class SingleProducerAsyncInput {
  constructor(ref) {
    this.ref = ref
    this.take = this.takeWith(
      (c) => Exit.halt(Cause.map_(c, E.left)),
      (el) => Exit.succeed(el),
      (d) => Exit.fail(E.right(d))
    )
    this.close = T.chain_(T.fiberId, (id) => this.error(Cause.interrupt(id)))
  }

  emit(el) {
    return T.chain_(P.make(), (p) =>
      T.flatten(
        Ref.modify_(this.ref, (state) => {
          switch (state._typeId) {
            case EmitTypeId: {
              return Tp.tuple(
                T.chain_(P.await(state.notifyProducer), () => this.emit(el)),
                state
              )
            }

            case ErrorTypeId: {
              return Tp.tuple(T.interrupt, state)
            }

            case DoneTypeId: {
              return Tp.tuple(T.interrupt, state)
            }

            case EmptyTypeId: {
              return Tp.tuple(
                T.chain_(P.succeed_(state.notifyConsumer, void 0), () => P.await(p)),
                new StateEmit(el, p)
              )
            }
          }
        })
      )
    )
  }

  done(a) {
    return T.chain_(P.make(), (p) =>
      T.flatten(
        Ref.modify_(this.ref, (state) => {
          switch (state._typeId) {
            case EmitTypeId: {
              return Tp.tuple(
                T.chain_(P.await(state.notifyProducer), () => this.done(a)),
                state
              )
            }

            case ErrorTypeId: {
              return Tp.tuple(T.interrupt, state)
            }

            case DoneTypeId: {
              return Tp.tuple(T.interrupt, state)
            }

            case EmptyTypeId: {
              return Tp.tuple(
                P.succeed_(state.notifyConsumer, void 0),
                new StateDone(a)
              )
            }
          }
        })
      )
    )
  }

  error(cause) {
    return T.chain_(P.make(), (p) =>
      T.flatten(
        Ref.modify_(this.ref, (state) => {
          switch (state._typeId) {
            case EmitTypeId: {
              return Tp.tuple(
                T.chain_(P.await(state.notifyProducer), () => this.error(cause)),
                state
              )
            }

            case ErrorTypeId: {
              return Tp.tuple(T.interrupt, state)
            }

            case DoneTypeId: {
              return Tp.tuple(T.interrupt, state)
            }

            case EmptyTypeId: {
              return Tp.tuple(
                P.succeed_(state.notifyConsumer, void 0),
                new StateError(cause)
              )
            }
          }
        })
      )
    )
  }

  takeWith(onError, onElement, onDone) {
    return T.chain_(P.make(), (p) =>
      T.flatten(
        Ref.modify_(this.ref, (state) => {
          switch (state._typeId) {
            case EmitTypeId: {
              return Tp.tuple(
                T.map_(P.succeed_(state.notifyProducer, void 0), () =>
                  onElement(state.a)
                ),
                new StateEmpty(p)
              )
            }

            case ErrorTypeId: {
              return Tp.tuple(T.succeed(onError(state.cause)), state)
            }

            case DoneTypeId: {
              return Tp.tuple(T.succeed(onDone(state.a)), state)
            }

            case EmptyTypeId: {
              return Tp.tuple(
                T.chain_(P.await(state.notifyConsumer), () =>
                  this.takeWith(onError, onElement, onDone)
                ),
                state
              )
            }
          }
        })
      )
    )
  }
}
/**
 * Creates a SingleProducerAsyncInput
 */

export function makeSingleProducerAsyncInput() {
  return T.map_(
    T.chain_(P.make(), (p) => Ref.makeRef(new StateEmpty(p))),
    (ref) => new SingleProducerAsyncInput(ref)
  )
}
//# sourceMappingURL=producer.js.map
