/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes, isObject, UINT32_MAX, UINT64_MAX } from "@matter/general";
import { DatatypeError, IntegerRangeError } from "@matter/protocol";
import { MATTER_EPOCH_OFFSET_S, MATTER_EPOCH_OFFSET_US } from "@matter/types";
function assertNumber(value, path) {
  if (Number.isFinite(value)) {
    return;
  }
  throw new DatatypeError(path, "a number", value);
}
function assertBoolean(value, path) {
  if (typeof value === "boolean" || value === 0 || value === 1) {
    return;
  }
  throw new DatatypeError(path, "a boolean", value);
}
function assertObject(value, path) {
  if (isObject(value)) {
    return;
  }
  throw new DatatypeError(path, "an object", value);
}
function assertNumeric(value, path) {
  if (typeof value === "number" || typeof value === "bigint") {
    return;
  }
  throw new DatatypeError(path, "a number or bigint", value);
}
function assertString(value, path) {
  if (typeof value === "string") {
    return;
  }
  throw new DatatypeError(path, "a string", value);
}
function assertBytes(value, path) {
  if (Bytes.isBytes(value)) {
    return;
  }
  throw new DatatypeError(path, "a byte array", value);
}
function assertSequence(value, path) {
  if (typeof value === "string" || Bytes.isBytes(value)) {
    return;
  }
  throw new DatatypeError(path, "a string or byte array", value);
}
function assertArray(value, path) {
  if (!Array.isArray(value)) {
    throw new DatatypeError(path, "an array", value);
  }
}
const assertInt = {
  /**
   * Assertions for each integer type that is not nullable.
   */
  notNullable: {},
  /**
   * Assertions for nullable integer types.
   *
   * These are separate from the "not nullable" assertions because Matter reserves a high or low value (for unsigned
   * and signed, respectively) to indicate the field is null.
   */
  nullable: {}
};
for (let i = 1n; i < 9n; i++) {
  const intName = `int${i * 8n}`;
  const uintName = `u${intName}`;
  const numValues = 2n ** (i * 8n);
  const unsignedMax = numValues - 1n;
  assertInt.notNullable[uintName] = createIntAssertion(uintName, 0n, unsignedMax);
  assertInt.nullable[uintName] = createIntAssertion(`nullable ${uintName}`, 0n, unsignedMax - 1n);
  const halfNumValues = numValues / 2n;
  const signedMax = halfNumValues - 1n;
  const signedMin = -halfNumValues;
  assertInt.notNullable[intName] = createIntAssertion(intName, signedMin, signedMax);
  assertInt.nullable[intName] = createIntAssertion(`nullable ${intName}`, signedMin + 1n, signedMax);
}
assertInt.notNullable["epoch-s"] = createIntAssertion(
  "epoch-s",
  BigInt(MATTER_EPOCH_OFFSET_S),
  BigInt(UINT32_MAX + MATTER_EPOCH_OFFSET_S)
);
assertInt.nullable["epoch-s"] = createIntAssertion(
  `nullable epoch-s`,
  BigInt(MATTER_EPOCH_OFFSET_S),
  BigInt(UINT32_MAX + MATTER_EPOCH_OFFSET_S) - 1n
);
assertInt.notNullable["epoch-us"] = createIntAssertion(
  "epoch-us",
  MATTER_EPOCH_OFFSET_US,
  UINT64_MAX + MATTER_EPOCH_OFFSET_US
);
assertInt.nullable["epoch-us"] = createIntAssertion(
  `nullable epoch-us`,
  MATTER_EPOCH_OFFSET_US,
  UINT64_MAX + MATTER_EPOCH_OFFSET_US - 1n
);
function createIntAssertion(name, lowerBoundInclusive, upperBoundExclusive) {
  if (lowerBoundInclusive < Number.MIN_SAFE_INTEGER || upperBoundExclusive > Number.MAX_SAFE_INTEGER) {
    return createVarIntAssertion(name, lowerBoundInclusive, upperBoundExclusive);
  }
  return createVarIntAssertion(name, Number(lowerBoundInclusive), Number(upperBoundExclusive));
}
function createVarIntAssertion(name, min, max) {
  return function assertInt2(value, path) {
    assertNumeric(value, path);
    if (value < min) {
      throw new IntegerRangeError(path, `Value ${value} is below the ${name} minimum of ${min}`);
    }
    if (value > max) {
      throw new IntegerRangeError(path, `Value ${value} is above the ${name} maximum of ${max}`);
    }
  };
}
export {
  assertArray,
  assertBoolean,
  assertBytes,
  assertInt,
  assertNumber,
  assertNumeric,
  assertObject,
  assertSequence,
  assertString
};
//# sourceMappingURL=assertions.js.map
