"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.makeHub = makeHub;
exports.unsafeMakeHub = unsafeMakeHub;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/mutable/MutableHashSet/empty"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/AtomicBoolean"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Deferred/operations/make"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Scope/operations/make"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/interruption"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeed"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/suspendSucceed"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/interrupt"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Scope/operations/close"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/zipRight"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/whenEffect"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/unit"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/suspendSucceedWith"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Scope/operations/addFinalizer"));

var tsplus_module_17 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/tap"));

var tsplus_module_18 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/acquireRelease"));

var tsplus_module_19 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeedNow"));

var tsplus_module_20 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/single"));

var tsplus_module_21 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/isEmpty"));

var _makeSubscription = /*#__PURE__*/require("@effect/core/io/Hub/operations/_internal/makeSubscription");

var _unsafePublishAll = /*#__PURE__*/require("@effect/core/io/Hub/operations/_internal/unsafePublishAll");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/io/Hub/operations/_internal/makeHub.ts";

/**
 * Creates a hub with the specified strategy.
 */
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_((0, _makeSubscription.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 = (0, _unsafePublishAll.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.
 */

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