const fileName_1 = "(@effect/core) _src/io/Hub/operations/_internal/makeHub.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/collections/mutable/MutableHashSet/empty";
import * as tsplus_module_2 from "@tsplus/stdlib/data/AtomicBoolean";
import * as tsplus_module_3 from "@effect/core/io/Deferred/operations/make";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_5 from "@effect/core/io/Scope/operations/make";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/interruption";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/suspendSucceed";
import * as tsplus_module_10 from "@effect/core/io/Exit/operations/interrupt";
import * as tsplus_module_11 from "@effect/core/io/Scope/operations/close";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/whenEffect";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_15 from "@effect/core/io/Effect/operations/suspendSucceedWith";
import * as tsplus_module_16 from "@effect/core/io/Scope/operations/addFinalizer";
import * as tsplus_module_17 from "@effect/core/io/Effect/operations/tap";
import * as tsplus_module_18 from "@effect/core/io/Effect/operations/acquireRelease";
import * as tsplus_module_19 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_20 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_21 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import { makeSubscription } from "@effect/core/io/Hub/operations/_internal/makeSubscription";
import { unsafePublishAll } from "@effect/core/io/Hub/operations/_internal/unsafePublishAll";
/**
 * Creates a hub with the specified strategy.
 */

export function makeHub(hub, strategy) {
  return tsplus_module_6.flatMap_(tsplus_module_5.make, scope => tsplus_module_4.map_(tsplus_module_3.make(fileName_1 + ":12:18"), deferred => unsafeMakeHub(hub, tsplus_module_1.empty(), scope, deferred, new tsplus_module_2.AtomicBoolean(false), strategy), fileName_1 + ":12:37"), fileName_1 + ":11:28");
}
const HubProto = {
  get capacity() {
    return this.hub.capacity;
  },

  get size() {
    return tsplus_module_9.suspendSucceed(() => this.shutdownFlag.get ? tsplus_module_7.interrupt : tsplus_module_8.succeed(() => this.hub.size(), fileName_1 + ":33:23"), fileName_1 + ":30:33");
  },

  get awaitShutdown() {
    return this.shutdownHook.await(fileName_1 + ":37:62");
  },

  get shutdown() {
    return tsplus_module_7.uninterruptible(tsplus_module_15.suspendSucceedWith((_, fiberId) => {
      ;
      this.shutdownFlag.set(true);
      return tsplus_module_14.unit_(tsplus_module_13.whenEffect(() => this.shutdownHook.succeed(() => undefined, fileName_1 + ":43:61"), () => tsplus_module_12.zipRight_(tsplus_module_11.close_(this.scope, () => tsplus_module_10.interrupt(fiberId)), () => this.strategy.shutdown, fileName_1 + ":44:73"), fileName_1 + ":42:31"), fileName_1 + ":45:13");
    }, fileName_1 + ":40:37"), fileName_1 + ":46:23");
  },

  get isShutdown() {
    return tsplus_module_8.succeed(() => this.shutdownFlag.get, fileName_1 + ":49:26");
  },

  get subscribe() {
    return tsplus_module_18.acquireRelease(() => tsplus_module_17.tap_(makeSubscription(this.hub, this.subscribers, this.strategy), dequeue => tsplus_module_16.addFinalizer_(this.scope, () => dequeue.shutdown), fileName_1 + ":53:70"), dequeue => dequeue.shutdown, fileName_1 + ":52:33");
  },

  offer(a, __tsplusTrace) {
    return this.publish(a);
  },

  offerAll(as, __tsplusTrace) {
    return this.publishAll(as);
  },

  publish(a, __tsplusTrace) {
    return tsplus_module_9.suspendSucceed(() => {
      if (this.shutdownFlag.get) {
        return tsplus_module_7.interrupt;
      }

      if (this.hub.publish(a)) {
        this.strategy.unsafeCompleteSubscribers(this.hub, this.subscribers);
        return tsplus_module_19.succeedNow(true, fileName_1 + ":73:33");
      }

      return this.strategy.handleSurplus(this.hub, this.subscribers, tsplus_module_20.single(a), this.shutdownFlag);
    }, fileName_1 + ":66:33");
  },

  publishAll(as, __tsplusTrace) {
    return tsplus_module_9.suspendSucceed(() => {
      if (this.shutdownFlag.get) {
        return tsplus_module_7.interrupt;
      }

      const surplus = unsafePublishAll(this.hub, as);
      this.strategy.unsafeCompleteSubscribers(this.hub, this.subscribers);

      if (tsplus_module_21.isEmpty(surplus)) {
        return tsplus_module_19.succeedNow(true, fileName_1 + ":92:33");
      }

      return this.strategy.handleSurplus(this.hub, this.subscribers, surplus, this.shutdownFlag);
    }, fileName_1 + ":85:33");
  }

};
/**
 * Unsafely creates a hub with the specified strategy.
 */

export function unsafeMakeHub(hub, subscribers, scope, shutdownHook, shutdownFlag, strategy) {
  return Object.setPrototypeOf({
    hub,
    subscribers,
    scope,
    shutdownHook,
    shutdownFlag,
    strategy
  }, HubProto);
}
//# sourceMappingURL=makeHub.mjs.map