"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 NodeId_exports = {};
__export(NodeId_exports, {
  NodeId: () => NodeId,
  TlvNodeId: () => TlvNodeId
});
module.exports = __toCommonJS(NodeId_exports);
var import_GroupId = require("#datatype/GroupId.js");
var import_general = require("#general");
var import_TlvNumber = require("../tlv/TlvNumber.js");
var import_TlvWrapper = require("../tlv/TlvWrapper.js");
var import_CaseAuthenticatedTag = require("./CaseAuthenticatedTag.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
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 import_general.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" + import_general.hex.byte((0, import_GroupId.GroupId)(groupId))));
  };
  NodeId2.fromTemporaryLocalNodeId = (id) => {
    if (id < 0 || id > 4294967295) {
      throw new import_general.UnexpectedDataError(`Invalid ID: ${id}`);
    }
    return NodeId2(BigInt("0xFFFFFFFE" + import_general.hex.fixed(id, 8)));
  };
  NodeId2.fromCaseAuthenticatedTag = (id) => {
    if (id < 0 || id > 4294967295) {
      throw new import_general.UnexpectedDataError(`Invalid CASE Authenticated tag: ${id}`);
    }
    return NodeId2(BigInt("0xFFFFFFFD" + import_general.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 import_general.UnexpectedDataError(`Invalid CASE Authenticated tag: ${nodeId}`);
    }
    return (0, import_CaseAuthenticatedTag.CaseAuthenticatedTag)(parseInt(nodeId.toString(16).slice(8), 16));
  };
  NodeId2.getFromPakeKeyIdentifier = (id) => {
    if (id < 0 || id > 4294967295) {
      throw new import_general.UnexpectedDataError(`Invalid ID: ${id}`);
    }
    return NodeId2(BigInt("0xFFFFFFFB" + import_general.hex.fixed(id, 8)));
  };
})(NodeId || (NodeId = {}));
const TlvNodeId = new import_TlvWrapper.TlvWrapper(
  import_TlvNumber.TlvUInt64,
  (nodeId) => nodeId,
  (value) => NodeId(BigInt(value))
);
//# sourceMappingURL=NodeId.js.map
