"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Time_exports = {};
__export(Time_exports, {
  Time: () => Time
});
module.exports = __toCommonJS(Time_exports);
var import_DiagnosticPresentation = require("#log/DiagnosticPresentation.js");
var import_MatterError = require("#MatterError.js");
var import_Cancelable = require("#util/Cancelable.js");
var import_Diagnostic = require("../log/Diagnostic.js");
var import_DiagnosticSource = require("../log/DiagnosticSource.js");
var import_Duration = require("./Duration.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const registry = /* @__PURE__ */ new Set();
class Time {
  static default;
  static startup = {
    systemMs: 0,
    processMs: 0
  };
  get now() {
    return /* @__PURE__ */ new Date();
  }
  static get now() {
    return Time.default.now;
  }
  /**
   * The current wall-clock time as a millisecond {@link Timestamp}.
   *
   * Backed by {@link Date.now} so it tracks UTC including clock adjustments (e.g. NTP steps). Use this for absolute
   * time such as epoch values sent on the wire.
   */
  get nowMs() {
    return Date.now();
  }
  static get nowMs() {
    return Time.default.nowMs;
  }
  /**
   * The current time as a high-resolution {@link Timestamp}.
   *
   * Backed by {@link performance} so it carries sub-millisecond precision where the platform supports it (falling
   * back to {@link nowMs} otherwise). It is monotonic and may drift from UTC, so use it for relative/elapsed timing,
   * not for absolute wall-clock values.
   *
   * Despite the name the value is a millisecond {@link Timestamp}, not microseconds. Use
   * {@link Timestamp.toMicroseconds} to obtain a microsecond value.
   */
  get nowUs() {
    return performance.now() + performance.timeOrigin;
  }
  static get nowUs() {
    return Time.default.nowUs;
  }
  /**
   * Create a timer that will call callback after durationMs has passed.
   */
  getTimer(_name, _duration, _callback) {
    throw new import_MatterError.NoProviderError();
  }
  static getTimer = (name, duration, callback) => Time.default.getTimer(name, duration, callback);
  /**
   * Create a timer that will periodically call callback at intervalMs intervals.
   */
  getPeriodicTimer(_name, _duration, _callback) {
    throw new import_MatterError.NoProviderError();
  }
  static getPeriodicTimer = (name, duration, callback) => Time.default.getPeriodicTimer(name, duration, callback);
  /**
   * Defer to a new macrotask.
   */
  get macrotask() {
    throw new import_MatterError.NoProviderError();
  }
  static get macrotask() {
    return Time.default.macrotask;
  }
  /**
   * Create a promise that resolves after a specific interval or when canceled, whichever comes first.
   */
  sleep(name, duration) {
    let timer;
    let resolver;
    return new import_Cancelable.CancelablePromise(
      (resolve) => {
        resolver = resolve;
        timer = Time.getTimer(name, duration, () => resolve());
        timer.start();
      },
      () => {
        timer.stop();
        resolver();
      }
    );
  }
  static sleep(name, duration) {
    return Time.default.sleep(name, duration);
  }
  static register(timer) {
    registry.add(timer);
    timer.elapsed = import_Diagnostic.Diagnostic.elapsed();
  }
  static unregister(timer) {
    registry.delete(timer);
  }
  static get timers() {
    return registry;
  }
}
if (!performance || typeof performance.now !== "function" || typeof performance.timeOrigin !== "number") {
  Object.defineProperty(Time.prototype, "nowUs", {
    get() {
      return Time.nowMs;
    }
  });
}
import_DiagnosticSource.DiagnosticSource.add({
  get [import_DiagnosticPresentation.DiagnosticPresentation.value]() {
    return import_Diagnostic.Diagnostic.node("\u23F1", "Timers", {
      children: [...registry].map((timer) => [
        timer.name,
        import_Diagnostic.Diagnostic.dict({
          periodic: timer.isPeriodic,
          up: timer.elapsed,
          interval: import_Duration.Duration.format(timer.interval),
          "system#": timer.systemId
        })
      ])
    });
  }
});
//# sourceMappingURL=Time.js.map
