"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 Duration_exports = {};
__export(Duration_exports, {
  Duration: () => Duration,
  DurationFormatError: () => DurationFormatError
});
module.exports = __toCommonJS(Duration_exports);
var import_MatterError = require("#MatterError.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function Duration(source) {
  if (typeof source === "string") {
    return Duration.parse(source);
  }
  if (typeof source === "number") {
    if (!Number.isFinite(source)) {
      throw new DurationFormatError(`A duration must be a finite number`);
    }
    return source;
  }
  throw new DurationFormatError(`Interval value is not a number (received ${typeof source})`);
}
class DurationFormatError extends import_MatterError.UnexpectedDataError {
}
((Duration2) => {
  function max(a, b) {
    if (b > a) {
      return b;
    }
    return a;
  }
  Duration2.max = max;
  function min(a, b) {
    if (b < a) {
      return b;
    }
    return a;
  }
  Duration2.min = min;
  function format(duration) {
    if (duration === void 0) {
      return void 0;
    }
    const ms = duration;
    if (typeof ms !== "number" || Number.isNaN(ms)) {
      return "invalid";
    }
    switch (ms) {
      case 0:
        return "0";
      case Infinity:
        return "forever";
      case -Infinity:
        return "until now";
    }
    const negative = ms < 0 ? "-" : "";
    let absMs = Math.abs(ms);
    if (absMs < 1) {
      return `${negative}${toPrecision(absMs * 1e3, 3)}\u03BCs`;
    } else if (absMs < 1e3) {
      return `${negative}${toPrecision(absMs, 3)}ms`;
    } else if (absMs < 6e4) {
      return `${negative}${toPrecision(absMs / 1e3, 3)}s`;
    }
    const parts = Array();
    if (absMs > 864e5) {
      parts.push(`${Math.floor(absMs / 864e5)}d`);
      absMs %= 864e5;
    }
    const hours = Math.floor(absMs / 36e5);
    if (hours) {
      parts.push(`${hours}h`);
    }
    absMs %= 36e5;
    const minutes = Math.floor(absMs / 6e4);
    if (minutes) {
      parts.push(`${minutes}m`);
    }
    absMs %= 6e4;
    const seconds = Math.floor(absMs / 1e3);
    if (seconds) {
      parts.push(`${seconds}s`);
    }
    return `${negative}${parts.join(" ")}`;
  }
  Duration2.format = format;
  function parse(text) {
    const parts = text.split(/\s+/).filter((part) => part !== "");
    let interval = 0;
    for (const part of parts) {
      const suffix = part.match(/[a-zμ]+$/i)?.[0];
      if (suffix === void 0) {
        throw new DurationFormatError(`Interval component "${part}" is missing a time suffix`);
      }
      const numericPart = part.slice(0, part.length - suffix.length);
      if (numericPart === "") {
        throw new DurationFormatError(`Interval component "${part}" contains no numeric component`);
      }
      const value = Number(numericPart);
      if (!Number.isFinite(value)) {
        throw new DurationFormatError(`Interval component "${part}" contains an invalid numeric value`);
      }
      switch (suffix.toLowerCase()) {
        case "\u03BCs":
        case "us":
          interval += value / 1e3;
          break;
        case "ms":
          interval += value;
          break;
        case "s":
          interval += value * 1e3;
          break;
        case "m":
          interval += value * 6e4;
          break;
        case "h":
          interval += value * 36e5;
          break;
        case "d":
          interval += value * 864e5;
          break;
        default:
          throw new DurationFormatError(`Interval component ${part} contains an unsupported unit suffix`);
      }
    }
    return interval;
  }
  Duration2.parse = parse;
})(Duration || (Duration = {}));
function toPrecision(number, precision) {
  return number.toPrecision(precision).replace(/(\.\d*?[1-9])0+$/, "$1").replace(/\.0+$/, "");
}
//# sourceMappingURL=Duration.js.map
