/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { UnexpectedDataError } from "#MatterError.js";
import { Time } from "./Time.js";
function Timestamp(source) {
  if (source === void 0) {
    return Time.nowMs;
  }
  if (typeof source === "string") {
    const date = Date.parse(source);
    if (!Number.isFinite(date.valueOf())) {
      throw new TimestampFormatError("Invalid timestamp format");
    }
    source = date;
  } else if (typeof source === "object" && source !== null && source instanceof Date) {
    if (!Number.isFinite(source.valueOf())) {
      throw new TimestampFormatError("Cannot convert date to timestamp because it is invalid");
    }
    source = source.getTime();
  } else if (typeof source === "bigint") {
    source = Number(source);
  }
  if (typeof source === "number") {
    if (!Number.isFinite(source)) {
      throw new TimestampFormatError("A timestamp must be a finite number");
    }
  } else {
    throw new TimestampFormatError(`Timestamp is not a number (received ${typeof source})`);
  }
  return source;
}
class TimestampFormatError extends UnexpectedDataError {
}
((Timestamp2) => {
  function fromSeconds(source) {
    return Math.floor(source * 1e3);
  }
  Timestamp2.fromSeconds = fromSeconds;
  function fromMicroseconds(source) {
    if (typeof source === "bigint") {
      source = Number(source);
    }
    return Timestamp2(source / 1e3);
  }
  Timestamp2.fromMicroseconds = fromMicroseconds;
  function toMicroseconds(timestamp) {
    return BigInt(Math.round(timestamp * 1e3));
  }
  Timestamp2.toMicroseconds = toMicroseconds;
  Timestamp2.zero = Timestamp2(0);
  function dateOf(timestamp) {
    return new Date(timestamp);
  }
  Timestamp2.dateOf = dateOf;
  function delta(from, to = Time.nowMs) {
    return to - from;
  }
  Timestamp2.delta = delta;
})(Timestamp || (Timestamp = {}));
export {
  Timestamp,
  TimestampFormatError
};
//# sourceMappingURL=Timestamp.js.map
