"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.make = exports.globalClockScheduler = exports.clockTag = exports.MAX_TIMER_MILLIS = exports.ClockTypeId = void 0;
var Context = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Context"));
var Debug = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Debug"));
var Either = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/data/Either"));
var _Function = /*#__PURE__*/require("@effect/data/Function");
var core = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/io/internal_effect_untraced/core"));
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; }
var _a;
/** @internal */
const ClockSymbolKey = "@effect/io/Clock";
/** @internal */
const ClockTypeId = /*#__PURE__*/Symbol.for(ClockSymbolKey);
/** @internal */
exports.ClockTypeId = ClockTypeId;
const clockTag = /*#__PURE__*/Context.Tag(ClockTypeId);
/** @internal */
exports.clockTag = clockTag;
const MAX_TIMER_MILLIS = 2 ** 31 - 1;
/** @internal */
exports.MAX_TIMER_MILLIS = MAX_TIMER_MILLIS;
const globalClockScheduler = {
  unsafeSchedule(task, duration) {
    // If the duration is greater than the value allowable by the JS timer
    // functions, treat the value as an infinite duration
    if (duration.millis > MAX_TIMER_MILLIS) {
      return _Function.constFalse;
    }
    let completed = false;
    const handle = setTimeout(() => {
      completed = true;
      task();
    }, duration.millis);
    return () => {
      clearTimeout(handle);
      return !completed;
    };
  }
};
exports.globalClockScheduler = globalClockScheduler;
const performanceNowNanos = /*#__PURE__*/function () {
  const origin = /*#__PURE__*/BigInt( /*#__PURE__*/Math.round(performance.timeOrigin * 1000000));
  return () => {
    const now = performance.now();
    const millis = Math.floor(now);
    return origin + BigInt(millis * 1000000) + BigInt(Math.round((now - millis) * 1000000));
  };
}();
/** @internal */
class ClockImpl {
  constructor() {
    this[_a] = ClockTypeId;
    this.processHrtime = typeof process === "object" && "hrtime" in process ? process.hrtime : undefined;
    this.timeOrigin = this.processHrtime ? performanceNowNanos() - this.processHrtime.bigint() : 0n;
  }
  unsafeCurrentTimeMillis() {
    return Date.now();
  }
  unsafeCurrentTimeNanos() {
    return this.processHrtime ? this.timeOrigin + this.processHrtime.bigint() : performanceNowNanos();
  }
  currentTimeMillis() {
    return Debug.bodyWithTrace(trace => core.sync(() => this.unsafeCurrentTimeMillis()).traced(trace));
  }
  currentTimeNanos() {
    return Debug.bodyWithTrace(trace => core.sync(() => this.unsafeCurrentTimeNanos()).traced(trace));
  }
  scheduler() {
    return Debug.bodyWithTrace(trace => core.succeed(globalClockScheduler).traced(trace));
  }
  sleep(duration) {
    return Debug.bodyWithTrace(trace => core.asyncInterruptEither(cb => {
      const canceler = globalClockScheduler.unsafeSchedule(() => cb(core.unit()), duration);
      return Either.left(core.asUnit(core.sync(canceler)));
    }).traced(trace));
  }
}
_a = ClockTypeId;
/** @internal */
const make = () => new ClockImpl();
exports.make = make;
//# sourceMappingURL=clock.js.map