var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __using = (stack, value, async) => {
  if (value != null) {
    if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
    var dispose, inner;
    if (async) dispose = value[__knownSymbol("asyncDispose")];
    if (dispose === void 0) {
      dispose = value[__knownSymbol("dispose")];
      if (async) inner = dispose;
    }
    if (typeof dispose !== "function") __typeError("Object not disposable");
    if (inner) dispose = function() {
      try {
        inner.call(this);
      } catch (e) {
        return Promise.reject(e);
      }
    };
    stack.push([async, dispose, value]);
  } else if (async) {
    stack.push([async]);
  }
  return value;
};
var __callDispose = (stack, error, hasError) => {
  var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
    return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
  };
  var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
  var next = (it) => {
    while (it = stack.pop()) {
      try {
        var result = it[1] && it[1].call(it[2]);
        if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
      } catch (e) {
        fail(e);
      }
    }
    if (hasError) throw error;
  };
  return next();
};
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ImplementationError } from "#MatterError.js";
import { Boot } from "#util/Boot.js";
import { decamelize } from "#util/identifier-case.js";
import { Lifetime } from "#util/Lifetime.js";
import { Time } from "./Time.js";
import { Instant } from "./TimeUnit.js";
class StandardTime extends Time {
  getTimer(name, duration, callback) {
    return new StandardTimer(name, duration, callback, false);
  }
  getPeriodicTimer(name, duration, callback) {
    return new StandardTimer(name, duration, callback, true);
  }
}
class StandardTimer {
  constructor(name, duration, callback, isPeriodic) {
    this.name = name;
    this.callback = callback;
    this.isPeriodic = isPeriodic;
    this.interval = duration;
  }
  #timerId;
  #utility = false;
  #interval = Instant;
  // Real value installed in constructor
  isRunning = false;
  get systemId() {
    return Number(this.#timerId);
  }
  /**
   * The timer's interval.
   *
   * You can change this value but changes have no effect until the timer restarts.
   */
  set interval(interval) {
    if (interval < 0 || interval > 2147483647) {
      throw new ImplementationError(
        `Invalid intervalMs: ${interval}. The value must be between 0 and 32-bit maximum value (2147483647)`
      );
    }
    this.#interval = interval;
  }
  get interval() {
    return this.#interval;
  }
  get utility() {
    return this.#utility;
  }
  set utility(utility) {
    if (utility === this.#utility) {
      return;
    }
    if (this.#timerId !== void 0) {
      const timerId = this.#timerId;
      if (utility) {
        timerId.unref?.();
      } else {
        timerId.ref?.();
      }
    }
    this.#utility = utility;
  }
  start() {
    if (this.isRunning) this.stop();
    Time.register(this);
    this.isRunning = true;
    this.#timerId = (this.isPeriodic ? setInterval : setTimeout)(() => {
      var _stack = [];
      try {
        const lifetime = __using(_stack, Lifetime(decamelize(this.name, " ")));
        if (!this.isPeriodic) {
          Time.unregister(this);
          this.isRunning = false;
        }
        this.callback(lifetime);
      } catch (_) {
        var _error = _, _hasError = true;
      } finally {
        __callDispose(_stack, _error, _hasError);
      }
    }, this.interval);
    return this;
  }
  stop() {
    (this.isPeriodic ? clearInterval : clearTimeout)(this.#timerId);
    Time.unregister(this);
    this.isRunning = false;
    return this;
  }
}
Boot.init(() => {
  Time.default = new StandardTime();
  Time.startup.systemMs = Time.startup.processMs = Time.nowMs;
  if (typeof MatterHooks !== "undefined") {
    MatterHooks?.timeSetup?.(Time);
  }
});
export {
  StandardTime,
  StandardTimer
};
//# sourceMappingURL=StandardTime.js.map
