/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { GroupId } from "#datatype/GroupId.js";
import { hex, UnexpectedDataError } from "#general";
import { TlvUInt64 } from "../tlv/TlvNumber.js";
import { TlvWrapper } from "../tlv/TlvWrapper.js";
import { CaseAuthenticatedTag } from "./CaseAuthenticatedTag.js";
function NodeId(v) {
  return BigInt(v);
}
((NodeId2) => {
  const OPERATIONAL_NODE_MIN = BigInt("0x0000000000000001");
  const OPERATIONAL_NODE_MAX = BigInt("0xFFFFFFEFFFFFFFFF");
  NodeId2.UNSPECIFIED_NODE_ID = NodeId2(0);
  function strOf(nodeId) {
    return hex.fixed(nodeId, 16);
  }
  NodeId2.strOf = strOf;
  NodeId2.randomOperationalNodeId = (crypto) => {
    while (true) {
      const randomBigInt = crypto.randomBigInt(8);
      if (randomBigInt >= OPERATIONAL_NODE_MIN && randomBigInt <= OPERATIONAL_NODE_MAX) {
        return NodeId2(randomBigInt);
      }
    }
  };
  NodeId2.isOperationalNodeId = (nodeId) => {
    return nodeId >= OPERATIONAL_NODE_MIN && nodeId <= OPERATIONAL_NODE_MAX;
  };
  NodeId2.fromGroupId = (groupId) => {
    return NodeId2(BigInt("0xFFFFFFFFFFFF" + hex.byte(GroupId(groupId))));
  };
  NodeId2.fromTemporaryLocalNodeId = (id) => {
    if (id < 0 || id > 4294967295) {
      throw new UnexpectedDataError(`Invalid ID: ${id}`);
    }
    return NodeId2(BigInt("0xFFFFFFFE" + hex.fixed(id, 8)));
  };
  NodeId2.fromCaseAuthenticatedTag = (id) => {
    if (id < 0 || id > 4294967295) {
      throw new UnexpectedDataError(`Invalid CASE Authenticated tag: ${id}`);
    }
    return NodeId2(BigInt("0xFFFFFFFD" + hex.fixed(id, 8)));
  };
  NodeId2.isCaseAuthenticatedTag = (nodeId) => {
    const nodeIdHex = nodeId.toString(16);
    return nodeIdHex.startsWith("fffffffd") && nodeIdHex.length === 16;
  };
  NodeId2.extractAsCaseAuthenticatedTag = (nodeId) => {
    if (!(0, NodeId2.isCaseAuthenticatedTag)(nodeId)) {
      throw new UnexpectedDataError(`Invalid CASE Authenticated tag: ${nodeId}`);
    }
    return CaseAuthenticatedTag(parseInt(nodeId.toString(16).slice(8), 16));
  };
  NodeId2.getFromPakeKeyIdentifier = (id) => {
    if (id < 0 || id > 4294967295) {
      throw new UnexpectedDataError(`Invalid ID: ${id}`);
    }
    return NodeId2(BigInt("0xFFFFFFFB" + hex.fixed(id, 8)));
  };
})(NodeId || (NodeId = {}));
const TlvNodeId = new TlvWrapper(
  TlvUInt64,
  (nodeId) => nodeId,
  (value) => NodeId(BigInt(value))
);
export {
  NodeId,
  TlvNodeId
};
//# sourceMappingURL=NodeId.js.map
