"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BoundedHubSingle = void 0;

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

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

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

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

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

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

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

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; }

class BoundedHubSingle {
  constructor() {
    this.publisherIndex = 0;
    this.subscriberCount = 0;
    this.subscribers = 0;
    this.value = null;
    this.capacity = 1;
  }

  isEmpty() {
    return this.subscribers === 0;
  }

  isFull() {
    return !this.isEmpty();
  }

  publish(a) {
    if (this.isFull()) {
      return false;
    }

    if (this.subscriberCount !== 0) {
      this.value = a;
      this.subscribers = this.subscriberCount;
      this.publisherIndex += 1;
    }

    return true;
  }

  publishAll(as) {
    const list = tsplus_module_1.from(as);

    if (tsplus_module_2.isEmpty(list)) {
      return tsplus_module_3.empty();
    }

    if (this.publish(tsplus_module_4.unsafeHead(list))) {
      return tsplus_module_5.drop_(list, 1);
    } else {
      return list;
    }
  }

  size() {
    return this.isEmpty() ? 0 : 1;
  }

  slide() {
    if (this.isFull()) {
      this.subscribers = 0;
      this.value = null;
    }
  }

  subscribe() {
    this.subscriberCount += 1;
    return new BoundedHubSingleSubscription(this, this.publisherIndex, false);
  }

}

exports.BoundedHubSingle = BoundedHubSingle;

class BoundedHubSingleSubscription extends _Subscription.Subscription {
  constructor(self, subscriberIndex, unsubscribed) {
    super();
    this.self = self;
    this.subscriberIndex = subscriberIndex;
    this.unsubscribed = unsubscribed;
  }

  isEmpty() {
    return this.unsubscribed || this.self.subscribers === 0 || this.subscriberIndex === this.self.publisherIndex;
  }

  poll(default_) {
    if (this.isEmpty()) {
      return default_;
    }

    const a = this.self.value;
    this.self.subscribers -= 1;

    if (this.self.subscribers === 0) {
      this.self.value = null;
    }

    this.subscriberIndex += 1;
    return a;
  }

  pollUpTo(n) {
    if (this.isEmpty() || n < 1) {
      return tsplus_module_3.empty();
    }

    const a = this.self.value;
    this.self.subscribers -= 1;

    if (this.self.subscribers === 0) {
      this.self.value = null;
    }

    this.subscriberIndex += 1;
    return tsplus_module_6.single(a);
  }

  size() {
    return this.isEmpty() ? 0 : 1;
  }

  unsubscribe() {
    if (!this.unsubscribed) {
      this.unsubscribed = true;
      this.self.subscriberCount -= 1;

      if (this.subscriberIndex !== this.self.publisherIndex) {
        this.self.subscribers -= 1;

        if (this.self.subscribers === 0) {
          this.self.value = null;
        }
      }
    }
  }

}
//# sourceMappingURL=BoundedHubSingle.js.map