/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { capitalize } from "@matter/general";
import { Matter } from "@matter/model";
import { ClusterId, DeviceTypeId } from "@matter/types";
var DeviceClasses = /* @__PURE__ */ ((DeviceClasses2) => {
  DeviceClasses2["Node"] = "Node";
  DeviceClasses2["Utility"] = "Utility";
  DeviceClasses2["App"] = "App";
  DeviceClasses2["Simple"] = "Simple";
  DeviceClasses2["Dynamic"] = "Dynamic";
  DeviceClasses2["Client"] = "Client";
  DeviceClasses2["Server"] = "Server";
  DeviceClasses2["Composed"] = "Composed";
  DeviceClasses2["Multiple"] = "Multiple";
  DeviceClasses2["EZInitiator"] = "EZ-Initiator";
  DeviceClasses2["EZTarget"] = "EZ-Target";
  DeviceClasses2["BridgedPowerSourceInfo"] = "BridgedPowerSourceInfo";
  return DeviceClasses2;
})(DeviceClasses || {});
const DeviceTypeDefinition = ({
  name,
  code,
  deviceClass,
  superSet,
  revision,
  requiredServerClusters = [],
  optionalServerClusters = [],
  requiredClientClusters = [],
  optionalClientClusters = [],
  unknown = false
}) => ({
  name,
  code: DeviceTypeId(code),
  deviceClass,
  superSet,
  revision,
  requiredServerClusters,
  optionalServerClusters,
  requiredClientClusters,
  optionalClientClusters,
  unknown
});
const DeviceTypes = {};
const UnknownDeviceType = (code, revision) => DeviceTypeDefinition({
  code,
  name: `Unknown Device Type ${code}`,
  deviceClass: "Simple" /* Simple */,
  revision,
  unknown: true
});
function getDeviceTypeDefinitionByCode(code) {
  return getDeviceTypeDefinitionFromModelByCode(code);
}
const DynamicDeviceType = {};
function getDeviceTypeDefinitionFromModelByCode(code) {
  if (DynamicDeviceType[code] !== void 0) {
    return DynamicDeviceType[code];
  }
  const device = Matter.deviceTypes(code);
  if (device === void 0) {
    return void 0;
  }
  const deviceDetails = {
    name: `MA-${device.name.toLowerCase()}`,
    revision: 0,
    code: device.id,
    deviceClass: capitalize(device.classification),
    superSet: device.type?.replace(/([A-Z])/g, "_$1").substring(1).toUpperCase(),
    requiredServerClusters: new Array(),
    optionalServerClusters: new Array(),
    requiredClientClusters: new Array(),
    optionalClientClusters: new Array()
  };
  device.requirements.forEach((req) => {
    if (req.id === void 0) {
      return;
    }
    if (req.name === "Descriptor") {
      const deviceTypeList = req.requirements.find((r) => r.element === "attribute" && r.name === "DeviceTypeList");
      if (deviceTypeList) {
        deviceDetails.revision = deviceTypeList.default[0].revision;
      }
    } else if (req.element === "serverCluster") {
      if (req.isMandatory) {
        deviceDetails.requiredServerClusters.push(ClusterId(req.id));
      } else {
        deviceDetails.optionalServerClusters.push(ClusterId(req.id));
      }
    } else if (req.element === "clientCluster") {
      if (req.isMandatory) {
        deviceDetails.requiredClientClusters.push(ClusterId(req.id));
      } else {
        deviceDetails.optionalClientClusters.push(ClusterId(req.id));
      }
    }
  });
  if (deviceDetails.revision === 0 || deviceDetails.requiredClientClusters.length === 0 && deviceDetails.requiredServerClusters.length === 0 && deviceDetails.optionalClientClusters.length === 0 && deviceDetails.optionalServerClusters.length === 0) {
    return void 0;
  }
  DynamicDeviceType[code] = DeviceTypeDefinition(deviceDetails);
  return DynamicDeviceType[code];
}
export {
  DeviceClasses,
  DeviceTypeDefinition,
  DeviceTypes,
  UnknownDeviceType,
  getDeviceTypeDefinitionByCode,
  getDeviceTypeDefinitionFromModelByCode
};
//# sourceMappingURL=DeviceTypes.js.map
