/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  Access,
  AccessLevel,
  AttributeElement,
  ClusterModel,
  CommandElement,
  EventElement,
  FieldElement
} from "@matter/model";
import { TlvVoid } from "../tlv/TlvVoid.js";
var RetiredClusterType;
((RetiredClusterType2) => {
  function ModelForOptions(options) {
    const children = [];
    const attrs = options.attributes ?? {};
    for (const [name, attr] of Object.entries(attrs)) {
      if (GLOBAL_ATTR_IDS.has(attr.id)) {
        continue;
      }
      const el = attr.schema.element;
      children.push(
        AttributeElement({
          id: attr.id,
          name,
          type: el?.type,
          constraint: el?.constraint,
          quality: attributeQuality(attr),
          access: attributeAccess(attr),
          conformance: attr.optional ? "O" : void 0,
          children: el?.children
        })
      );
    }
    const cmds = options.commands ?? {};
    const responsesSeen = /* @__PURE__ */ new Set();
    for (const [name, cmd] of Object.entries(cmds)) {
      const hasResponse = cmd.responseSchema !== TlvVoid;
      const responseName = hasResponse ? `${name}Response` : void 0;
      const reqEl = cmd.requestSchema.element;
      children.push(
        CommandElement({
          id: cmd.requestId,
          name,
          direction: CommandElement.Direction.Request,
          response: responseName,
          access: commandAccess(cmd),
          conformance: cmd.optional ? "O" : void 0,
          type: reqEl?.type,
          constraint: reqEl?.constraint,
          quality: reqEl?.quality,
          children: reqEl?.children
        })
      );
      if (hasResponse && !responsesSeen.has(cmd.responseId)) {
        responsesSeen.add(cmd.responseId);
        const respEl = cmd.responseSchema.element;
        children.push(
          CommandElement({
            id: cmd.responseId,
            name: responseName,
            direction: CommandElement.Direction.Response,
            type: respEl?.type,
            constraint: respEl?.constraint,
            quality: respEl?.quality,
            children: respEl?.children
          })
        );
      }
    }
    const events = options.events ?? {};
    for (const [name, evt] of Object.entries(events)) {
      const el = evt.schema.element;
      children.push(
        EventElement({
          id: evt.id,
          name,
          priority: PRIORITY_TO_STRING[evt.priority] ?? EventElement.Priority.Debug,
          access: eventAccess(evt),
          conformance: evt.optional ? "O" : void 0,
          type: el?.type,
          constraint: el?.constraint,
          quality: el?.quality,
          children: el?.children
        })
      );
    }
    const features = options.features ?? {};
    const featureChildren = [];
    for (const [name, flag] of Object.entries(features)) {
      const offset = flag.offset;
      featureChildren.push(
        FieldElement({
          name,
          constraint: offset
        })
      );
    }
    if (featureChildren.length) {
      children.push(
        AttributeElement({
          id: 65532,
          name: "featureMap",
          children: featureChildren
        })
      );
    }
    return new ClusterModel({
      id: options.id,
      name: options.name,
      revision: options.revision,
      children
    });
  }
  RetiredClusterType2.ModelForOptions = ModelForOptions;
})(RetiredClusterType || (RetiredClusterType = {}));
const GLOBAL_ATTR_IDS = /* @__PURE__ */ new Set([65533, 65532, 65531, 65529, 65528]);
const PRIVILEGE_TO_FLAG = {
  [AccessLevel.View]: Access.Privilege.View,
  [AccessLevel.Operate]: Access.Privilege.Operate,
  [AccessLevel.Manage]: Access.Privilege.Manage,
  [AccessLevel.Administer]: Access.Privilege.Administer
};
const PRIORITY_TO_STRING = {
  [0]: EventElement.Priority.Debug,
  [1]: EventElement.Priority.Info,
  [2]: EventElement.Priority.Critical
};
function attributeAccess(attr) {
  const parts = [];
  parts.push(attr.writable ? "RW" : "R");
  const readPriv = PRIVILEGE_TO_FLAG[attr.readAcl];
  if (readPriv) {
    parts.push(readPriv);
  }
  if (attr.writable && attr.writeAcl !== void 0) {
    const writePriv = PRIVILEGE_TO_FLAG[attr.writeAcl];
    if (writePriv && writePriv !== readPriv) {
      parts[parts.length - 1] += writePriv;
    }
  }
  if (attr.fabricScoped) {
    parts.push("F");
  }
  if (attr.timed) {
    parts.push("T");
  }
  return parts.join(" ");
}
function commandAccess(cmd) {
  const parts = [];
  const priv = PRIVILEGE_TO_FLAG[cmd.invokeAcl];
  if (priv) {
    parts.push(priv);
  }
  if (cmd.timed) {
    parts.push("T");
  }
  return parts.length ? parts.join(" ") : void 0;
}
function eventAccess(evt) {
  const priv = PRIVILEGE_TO_FLAG[evt.readAcl];
  return priv ?? void 0;
}
function attributeQuality(attr) {
  let q = attr.schema.element?.quality?.toString() ?? "";
  if (attr.persistent && !q.includes("N")) {
    q += "N";
  }
  if (attr.scene && !q.includes("S")) {
    q += "S";
  }
  if (attr.omitChanges && !q.includes("C")) {
    q += "C";
  }
  if (attr.fixed && !q.includes("F")) {
    q += "F";
  }
  return q || void 0;
}
export {
  RetiredClusterType
};
//# sourceMappingURL=RetiredClusterType.js.map
