"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 Specifier_exports = {};
__export(Specifier_exports, {
  Specifier: () => Specifier,
  resolvePathForSpecifier: () => resolvePathForSpecifier,
  toWildcardOrHexPath: () => toWildcardOrHexPath
});
module.exports = __toCommonJS(Specifier_exports);
var import_types = require("#types");
var import_MalformedRequestError = require("./MalformedRequestError.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const GlobalAttrMap = (0, import_types.GlobalAttributes)({});
var Specifier;
((Specifier2) => {
  function clusterFor(specifier) {
    if (specifier === void 0) {
      throw new import_MalformedRequestError.MalformedRequestError(`Cannot designate command "${specifier}" without a cluster`);
    }
    if ("cluster" in specifier) {
      return specifier["cluster"];
    }
    return specifier;
  }
  Specifier2.clusterFor = clusterFor;
  function attributeFor(cluster, specifier) {
    if (typeof specifier === "object") {
      return specifier;
    }
    if (cluster === void 0) {
      const attr2 = GlobalAttrMap[specifier];
      if (attr2 === void 0) {
        throw new import_MalformedRequestError.MalformedRequestError(`Cannot designate event "${specifier}" without a cluster`);
      }
      return attr2;
    }
    const attr = cluster.attributes?.[specifier];
    if (attr === void 0) {
      throw new import_MalformedRequestError.MalformedRequestError(`Cluster ${cluster.name} does not define attribute ${specifier}`);
    }
    return attr;
  }
  Specifier2.attributeFor = attributeFor;
  function commandFor(cluster, specifier) {
    if (typeof specifier !== "string") {
      return specifier;
    }
    if (cluster === void 0) {
      throw new import_MalformedRequestError.MalformedRequestError(`Cannot designate command "${specifier}" without a cluster`);
    }
    const command = cluster.commands?.[specifier];
    if (command === void 0) {
      throw new import_MalformedRequestError.MalformedRequestError(`Cluster ${cluster.name} does not define command ${specifier}`);
    }
    return command;
  }
  Specifier2.commandFor = commandFor;
  function eventFor(cluster, specifier) {
    if (typeof specifier === "object") {
      return specifier;
    }
    if (cluster === void 0) {
      throw new import_MalformedRequestError.MalformedRequestError(`Cannot designate event "${specifier}" without a cluster`);
    }
    const event = cluster.events?.[specifier];
    if (event === void 0) {
      throw new import_MalformedRequestError.MalformedRequestError(`Cluster ${cluster.name} does not define event ${specifier}`);
    }
    return event;
  }
  Specifier2.eventFor = eventFor;
  function clusterOf(request) {
    if (request.cluster) {
      return Specifier2.clusterFor(request.cluster);
    }
    return void 0;
  }
  Specifier2.clusterOf = clusterOf;
  function endpointIdOf(request) {
    if (typeof request.endpoint === "number") {
      return request.endpoint;
    }
    return request.endpoint?.number;
  }
  Specifier2.endpointIdOf = endpointIdOf;
})(Specifier || (Specifier = {}));
function toWildcardOrHexPath(name, value) {
  if (value === void 0) {
    return "*";
  }
  return `${name ? `${name}:` : ""}0x${value.toString(16)}`;
}
function resolvePathForSpecifier(location) {
  const endpointId = Specifier.endpointIdOf(location);
  const cluster = location.cluster ? Specifier.clusterFor(location.cluster) : void 0;
  const attribute = location.attribute && cluster ? Specifier.attributeFor(cluster, location.attribute) : void 0;
  const event = location.event && cluster ? Specifier.eventFor(cluster, location.event) : void 0;
  const command = location.command && cluster ? Specifier.commandFor(cluster, location.command) : void 0;
  const isUrgentString = "isUrgent" in location && location.isUrgent ? "!" : "";
  const listIndexString = "listIndex" in location && location.listIndex === null ? "[ADD]" : "";
  const postString = `${listIndexString}${isUrgentString}`;
  const clusterId = cluster?.id;
  const elementId = attribute ? attribute.id : event ? event.id : command ? command.requestId : void 0;
  if (endpointId === void 0) {
    return `*.${toWildcardOrHexPath("", clusterId)}.${toWildcardOrHexPath("", elementId)}${postString}`;
  }
  const endpointName = toWildcardOrHexPath("", endpointId);
  if (cluster === void 0 || clusterId === void 0) {
    return `${endpointName}.*.${toWildcardOrHexPath("", elementId)}${postString}`;
  }
  const clusterName = toWildcardOrHexPath(cluster.name, clusterId);
  if (elementId !== void 0) {
    if (event) {
      return `${endpointName}.${clusterName}.${toWildcardOrHexPath(typeof location.event === "string" ? location.event : "?", elementId)}${postString}`;
    } else if (attribute) {
      return `${endpointName}.${clusterName}.${toWildcardOrHexPath(typeof location.attribute === "string" ? location.attribute : "?", elementId)}${postString}`;
    } else if (command) {
      return `${endpointName}.${clusterName}.${toWildcardOrHexPath(typeof location.command === "string" ? location.command : "?", elementId)}${postString}`;
    } else {
      return "unknown";
    }
  } else {
    return `${endpointName}.${clusterName}.*${postString}`;
  }
}
//# sourceMappingURL=Specifier.js.map
