/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { DiagnosticPresentation } from "#log/DiagnosticPresentation.js";
import { NoProviderError } from "#MatterError.js";
import { CancelablePromise } from "#util/Cancelable.js";
import { Diagnostic } from "../log/Diagnostic.js";
import { DiagnosticSource } from "../log/DiagnosticSource.js";
import { Duration } from "./Duration.js";
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;
  }
  get nowMs() {
    return Date.now();
  }
  static get nowMs() {
    return Time.default.nowMs;
  }
  get nowUs() {
    return Math.floor(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 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 NoProviderError();
  }
  static getPeriodicTimer = (name, duration, callback) => Time.default.getPeriodicTimer(name, duration, callback);
  /**
   * Create a promise that resolves after a specific interval or when canceled, whichever comes first.
   */
  sleep(name, duration) {
    let timer;
    let resolver;
    return new 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 = 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;
    }
  });
}
DiagnosticSource.add({
  get [DiagnosticPresentation.value]() {
    return Diagnostic.node("\u23F1", "Timers", {
      children: [...registry].map((timer) => [
        timer.name,
        Diagnostic.dict({
          periodic: timer.isPeriodic,
          up: timer.elapsed,
          interval: Duration.format(timer.interval),
          "system#": timer.systemId
        })
      ])
    });
  }
});
export {
  Time
};
//# sourceMappingURL=Time.js.map
