"use strict";

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

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/pipeTo"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Clock/operations/currentTime"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fromEffect"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/succeed"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/zip"));

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

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/write"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/zipRight"));

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

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

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/unwrap"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fail"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/unit"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/readWith"));

var _StreamInternal = /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/StreamInternal");

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

/**
 * Throttles the chunks of this stream according to the given bandwidth
 * parameters using the token bucket algorithm. Allows for burst in the
 * processing of elements by allowing the token bucket to accumulate tokens up
 * to a `units + burst` threshold. Chunks that do not meet the bandwidth
 * constraints are dropped. The weight of each chunk is determined by the
 * `costFn` effectful function.
 *
 * @tsplus fluent ets/Stream throttleEnforceEffect
 */
function throttleEnforceEffect_(self, units, duration, costFn, burst = 0, __tsplusTrace) {
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_6.flatMap_(tsplus_module_5.zip_(tsplus_module_4.succeed(duration), () => tsplus_module_3.fromEffect(() => tsplus_module_2.currentTime)), ({
    tuple: [duration, timestamp]
  }) => tsplus_module_1.pipeTo_(self.channel, () => loop(units, duration, costFn, burst, units, timestamp, __tsplusTrace))));
}
/**
 * Throttles the chunks of this stream according to the given bandwidth
 * parameters using the token bucket algorithm. Allows for burst in the
 * processing of elements by allowing the token bucket to accumulate tokens up
 * to a `units + burst` threshold. Chunks that do not meet the bandwidth
 * constraints are dropped. The weight of each chunk is determined by the
 * `costFn` effectful function.
 *
 * @tsplus static ets/Stream/Aspects throttleEnforceEffect
 */


const throttleEnforceEffect = (units, duration, costFn, burst = 0, __tsplusTrace) => self => throttleEnforceEffect_(self, units, duration, costFn, burst, __tsplusTrace);

exports.throttleEnforceEffect = throttleEnforceEffect;

function loop(units, duration, costFn, burst, tokens, timestamp, __tsplusTrace) {
  return tsplus_module_14.readWith(input => tsplus_module_11.unwrap(() => tsplus_module_10.map_(tsplus_module_9.zip_(costFn(input), () => tsplus_module_2.currentTime, __tsplusTrace), ({
    tuple: [weight, current]
  }) => {
    const elapsed = current - timestamp;
    const cycles = elapsed / duration.millis;
    const sum = tokens + cycles * units;
    const max = units + burst < 0 ? Number.MAX_SAFE_INTEGER : units + burst;
    const available = sum < 0 ? max : Math.min(sum, max);
    return weight <= available ? tsplus_module_8.zipRight_(tsplus_module_7.write(() => input), () => loop(units, duration, costFn, burst, available - weight, current, __tsplusTrace)) : loop(units, duration, costFn, burst, available, current, __tsplusTrace);
  }, __tsplusTrace)), err => tsplus_module_12.fail(() => err), () => tsplus_module_13.unit);
}
//# sourceMappingURL=throttleEnforceEffect.js.map