{"version":3,"file":"buildClusterParams.cjs","sources":["../../../../../src/services/ESPRMMatterHelpers/TransformMatterNodeDetails/buildClusterParams.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { ESPRMDeviceParamInterface } from \"@espressif/rainmaker-base-sdk\";\nimport type { MatterMappedParamFields } from \"./MatterParamCommon\";\nimport {\n  MATTER_PARAM_VALUE_UNKNOWN,\n  type ClusterParamOption,\n} from \"../../../config/cluster.types\";\nimport { getClusterRegistryEntry } from \"../../../config/clusterRegistry\";\nimport type { ClusterParamDefinition } from \"../../../config/cluster.types\";\nimport {\n  MATTER_ATTRIBUTE_GATED_CLUSTER_IDS,\n  MatterClusterSide,\n  MatterParamProperty,\n  MatterXyColorControl,\n} from \"../../../utils/constants\";\nimport { clusterType } from \"./MatterParamCommon\";\n\nfunction resolveParamProperties(\n  paramDef: ClusterParamDefinition,\n  clusterSide: MatterClusterSide,\n): string[] {\n  if (paramDef.properties?.length) {\n    return [...paramDef.properties];\n  }\n\n  return clusterSide === MatterClusterSide.SERVER\n    ? [MatterParamProperty.READ, MatterParamProperty.WRITE]\n    : [MatterParamProperty.READ];\n}\n\nfunction shouldDefaultToFirstOption(\n  paramDef: ClusterParamDefinition,\n  value: string,\n): boolean {\n  if (value !== MATTER_PARAM_VALUE_UNKNOWN && value !== \"\") {\n    return false;\n  }\n\n  if (paramDef.type === \"command\" || paramDef.type === \"int\") {\n    return false;\n  }\n\n  return (\n    paramDef.type === \"enum\" &&\n    paramDef.optionsAttribute !== paramDef.valueAttribute\n  );\n}\n\nfunction resolveMatterAttributeId(\n  paramDef: ClusterParamDefinition,\n): number | undefined {\n  if (paramDef.writeAsCommand || paramDef.matterCommandId !== undefined) {\n    return undefined;\n  }\n\n  return paramDef.valueAttribute;\n}\n\n/** Compiled cluster param enum config (SDK param `bounds` + top-level fields). */\nexport interface ClusterParamConfig {\n  validStrings: string[];\n  labels: Record<string, string>;\n  rawModes: Record<string, number>;\n}\n\n/**\n * Builds top-level enum config for compiled cluster params.\n * @param options - Compiled cluster options.\n * @returns Param config written to SDK param `bounds` and top-level enum fields.\n */\nfunction buildClusterParamConfig(\n  options: ClusterParamOption[],\n): ClusterParamConfig {\n  return {\n    validStrings: options.map((option) => option.value),\n    labels: Object.fromEntries(\n      options.map((option) => [option.value, option.label]),\n    ),\n    rawModes: Object.fromEntries(\n      options.map((option) => [option.value, option.rawMode]),\n    ),\n  };\n}\n\n/** SDK param with compiled cluster enum config at top level. */\nexport type CompiledClusterParam = ESPRMDeviceParamInterface &\n  MatterMappedParamFields &\n  Partial<ClusterParamConfig>;\n\n/**\n * Compiles registered Matter clusters into semantic UI params from cluster registry only.\n * Live attribute values are not read from cloud `params`; use {@link ESPRMMatterDeviceParam.getValue}\n * or Matter subscription after transform.\n * Unregistered clusters return an empty array so callers can fall back to raw mapping.\n *\n * For attribute-gated clusters ({@link MATTER_ATTRIBUTE_GATED_CLUSTER_IDS}),\n * params whose `valueAttribute` the device does not advertise are dropped\n * (with no attribute list we fail open). On an XY-only color\n * endpoint (CurrentX/CurrentY advertised, neither HS attribute), params with\n * `xyColorComponent` compile XY-backed instead of being dropped: no\n * `matterAttributeId`/`resolver`, value from the pair, writes via `MoveToColor`.\n * @param clusterId - Matter cluster id.\n * @param clusterSide - Server or client cluster side.\n * @param endpointId - Matter endpoint id.\n * @param deviceName - RainMaker device name (endpoint hex id).\n * @param advertisedAttributes - Attribute ids the device advertises for this\n * cluster, when the metadata carried them.\n * @returns Compiled params when the cluster is registered; otherwise `[]`.\n */\nexport function buildClusterParams(\n  clusterId: number,\n  clusterSide: MatterClusterSide,\n  endpointId: number,\n  deviceName: string,\n  advertisedAttributes?: number[],\n): CompiledClusterParam[] {\n  const registry = getClusterRegistryEntry(clusterId);\n  if (!registry) {\n    return [];\n  }\n\n  const gatedAttributes =\n    MATTER_ATTRIBUTE_GATED_CLUSTER_IDS.includes(clusterId) &&\n    advertisedAttributes &&\n    advertisedAttributes.length > 0\n      ? advertisedAttributes\n      : undefined;\n  // XY-only color light: the (CurrentX, CurrentY) pair is advertised but\n  // neither HS attribute is, so `xyColorComponent` params compile XY-backed\n  // instead of being dropped. Mixed HS+XY firmwares keep the HS backing.\n  const xyColorFallback =\n    gatedAttributes !== undefined &&\n    gatedAttributes.includes(MatterXyColorControl.CURRENT_X_ATTRIBUTE) &&\n    gatedAttributes.includes(MatterXyColorControl.CURRENT_Y_ATTRIBUTE) &&\n    !gatedAttributes.includes(MatterXyColorControl.CURRENT_HUE_ATTRIBUTE) &&\n    !gatedAttributes.includes(\n      MatterXyColorControl.CURRENT_SATURATION_ATTRIBUTE,\n    );\n  const paramDefs = gatedAttributes\n    ? registry.params.filter(\n        (def) =>\n          gatedAttributes.includes(def.valueAttribute) ||\n          (xyColorFallback && def.xyColorComponent !== undefined),\n      )\n    : registry.params;\n\n  return paramDefs.map((paramDef) => {\n    let options = paramDef.resolver.decodeOptions(undefined);\n    if (options.length === 0) {\n      options = registry.defaultOptions ?? [];\n    }\n    const paramConfig = buildClusterParamConfig(options);\n    let value = paramDef.resolver.decodeValue(undefined, paramConfig.rawModes);\n    if (shouldDefaultToFirstOption(paramDef, value) && options.length > 0) {\n      value = options[0].value;\n    }\n    const xyBacked = xyColorFallback && paramDef.xyColorComponent !== undefined;\n\n    return {\n      clusterSide,\n      deviceName,\n      name: paramDef.name,\n      type: clusterType(clusterId, clusterSide),\n      uiType: paramDef.uiType,\n      dataType: paramDef.dataType,\n      properties: resolveParamProperties(paramDef, clusterSide),\n      value,\n      validStrings: paramConfig.validStrings.length\n        ? paramConfig.validStrings\n        : undefined,\n      labels: paramConfig.labels,\n      rawModes: paramConfig.rawModes,\n      bounds: {\n        ...paramConfig,\n        ...(paramDef.meta ?? {}),\n      },\n      clusterId,\n      endpointId,\n      clusterType: clusterSide,\n      matterAttributeId: xyBacked\n        ? undefined\n        : resolveMatterAttributeId(paramDef),\n      writeAsCommand: xyBacked ? undefined : paramDef.writeAsCommand,\n      matterCommandId: xyBacked ? undefined : paramDef.matterCommandId,\n      resolver: xyBacked ? undefined : paramDef.resolver,\n      xyColorComponent: xyBacked ? paramDef.xyColorComponent : undefined,\n    };\n  });\n}\n\nexport type { ClusterParamOption };\n"],"names":["MatterClusterSide","MatterParamProperty","MATTER_PARAM_VALUE_UNKNOWN","getClusterRegistryEntry","MATTER_ATTRIBUTE_GATED_CLUSTER_IDS","MatterXyColorControl","clusterType"],"mappings":";;;;;;;AAAA;;;;AAIG;AAkBH,SAAS,sBAAsB,CAC7B,QAAgC,EAChC,WAA8B,EAAA;AAE9B,IAAA,IAAI,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE;AAC/B,QAAA,OAAO,CAAC,GAAG,QAAQ,CAAC,UAAU,CAAC;IACjC;AAEA,IAAA,OAAO,WAAW,KAAKA,2BAAiB,CAAC;UACrC,CAACC,6BAAmB,CAAC,IAAI,EAAEA,6BAAmB,CAAC,KAAK;AACtD,UAAE,CAACA,6BAAmB,CAAC,IAAI,CAAC;AAChC;AAEA,SAAS,0BAA0B,CACjC,QAAgC,EAChC,KAAa,EAAA;IAEb,IAAI,KAAK,KAAKC,wCAA0B,IAAI,KAAK,KAAK,EAAE,EAAE;AACxD,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,IAAI,QAAQ,CAAC,IAAI,KAAK,SAAS,IAAI,QAAQ,CAAC,IAAI,KAAK,KAAK,EAAE;AAC1D,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,QACE,QAAQ,CAAC,IAAI,KAAK,MAAM;AACxB,QAAA,QAAQ,CAAC,gBAAgB,KAAK,QAAQ,CAAC,cAAc;AAEzD;AAEA,SAAS,wBAAwB,CAC/B,QAAgC,EAAA;IAEhC,IAAI,QAAQ,CAAC,cAAc,IAAI,QAAQ,CAAC,eAAe,KAAK,SAAS,EAAE;AACrE,QAAA,OAAO,SAAS;IAClB;IAEA,OAAO,QAAQ,CAAC,cAAc;AAChC;A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