"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.BoundedHubSingle = void 0
// tracing: off
require("../../Operator")
const Chunk = __importStar(require("../../Collections/Immutable/Chunk"))
const Hub_1 = require("./Hub")
class BoundedHubSingle extends Hub_1.Hub {
  constructor() {
    super()
    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 = Chunk.from(as)
    if (Chunk.isEmpty(list)) {
      return Chunk.empty()
    }
    if (this.publish(Chunk.unsafeHead(list))) {
      return Chunk.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 Hub_1.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 Chunk.empty()
    }
    const a = this.self.value
    this.self.subscribers -= 1
    if (this.self.subscribers === 0) {
      this.self.value = null
    }
    this.subscriberIndex += 1
    return Chunk.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
