/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Diagnostic } from "@matter/general";
import { AttributeModel, CommandModel, EventModel, Matter } from "@matter/model";
function toHex(value) {
  return value === void 0 ? "*" : `0x${value.toString(16)}`;
}
function getClusterNameById(clusterId) {
  return Matter.clusters(clusterId)?.name ?? `Unknown cluster ${Diagnostic.hex(clusterId)}`;
}
function resolveEndpointClusterName(nodeId, endpointId, clusterId) {
  let elementName = nodeId === void 0 ? "" : `${toHex(nodeId)}/`;
  if (endpointId === void 0) {
    elementName += "*";
  } else {
    elementName += `${toHex(endpointId)}`;
  }
  if (clusterId === void 0) {
    return `${elementName}/*`;
  }
  const name = Matter.clusters(clusterId)?.name;
  if (name === void 0) {
    return `${elementName}/unknown(${toHex(clusterId)})`;
  }
  return `${elementName}/${name}(${toHex(clusterId)})`;
}
function resolveElementName(cluster, tag, elementId) {
  if (cluster === void 0) {
    return void 0;
  }
  for (const child of cluster.children) {
    if (child.tag === tag && child.id === elementId) {
      return child.name;
    }
  }
  return void 0;
}
function resolveAttributeName({
  nodeId,
  endpointId,
  clusterId,
  attributeId
}) {
  const endpointClusterName = resolveEndpointClusterName(nodeId, endpointId, clusterId);
  if (endpointId === void 0 || clusterId === void 0 || attributeId === void 0) {
    return `${endpointClusterName}/${toHex(attributeId)}`;
  }
  const cluster = Matter.clusters(clusterId);
  const name = resolveElementName(cluster, AttributeModel.Tag, attributeId);
  if (name === void 0) {
    return `${endpointClusterName}/unknown(${toHex(attributeId)})`;
  }
  return `${endpointClusterName}/${name}(${toHex(attributeId)})`;
}
function resolveEventName({
  nodeId,
  endpointId,
  clusterId,
  eventId,
  isUrgent
}) {
  const isUrgentStr = isUrgent ? "!" : "";
  const endpointClusterName = resolveEndpointClusterName(nodeId, endpointId, clusterId);
  if (endpointId === void 0 || clusterId === void 0 || eventId === void 0) {
    return `${isUrgentStr}${endpointClusterName}/${toHex(eventId)}`;
  }
  const cluster = Matter.clusters(clusterId);
  const name = resolveElementName(cluster, EventModel.Tag, eventId);
  if (name === void 0) {
    return `${isUrgentStr}${endpointClusterName}/unknown(${toHex(eventId)})`;
  }
  return `${isUrgentStr}${endpointClusterName}/${name}(${toHex(eventId)})`;
}
function resolveCommandName({ endpointId, clusterId, commandId }) {
  const endpointClusterName = resolveEndpointClusterName(void 0, endpointId, clusterId);
  if (endpointId === void 0 || clusterId === void 0 || commandId === void 0) {
    return `${endpointClusterName}/${toHex(commandId)}`;
  }
  const cluster = Matter.clusters(clusterId);
  const name = resolveElementName(cluster, CommandModel.Tag, commandId);
  if (name === void 0) {
    return `${endpointClusterName}/unknown(${toHex(commandId)})`;
  }
  return `${endpointClusterName}/${name}(${toHex(commandId)})`;
}
export {
  getClusterNameById,
  resolveAttributeName,
  resolveCommandName,
  resolveEventName
};
//# sourceMappingURL=ClusterHelper.js.map
