"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 ClusterType_exports = {};
__export(ClusterType_exports, {
  ClusterType: () => ClusterType
});
module.exports = __toCommonJS(ClusterType_exports);
var import_general = require("@matter/general");
var import_model = require("@matter/model");
var import_StatusResponseError = require("../common/StatusResponseError.js");
var import_ClusterId = require("../datatype/ClusterId.js");
var import_Status = require("../globals/Status.js");
var import_ClassForValueModel = require("./ClassForValueModel.js");
var import_EnumForValueModel = require("./EnumForValueModel.js");
var import_RetiredClusterType = require("./RetiredClusterType.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const cache = /* @__PURE__ */ new WeakMap();
function ClusterType(input) {
  const model = input instanceof import_model.ClusterModel ? input : import_RetiredClusterType.RetiredClusterType.ModelForOptions(input);
  let ns = cache.get(model);
  if (ns !== void 0) {
    return ns;
  }
  ns = /* @__PURE__ */ Object.create(null);
  cache.set(model, ns);
  const props = ns;
  if (model.id !== void 0) {
    props.id = (0, import_ClusterId.ClusterId)(model.id);
  }
  props.name = model.name;
  props.revision = model.revision;
  props.schema = model;
  installLazyProperties(ns, model);
  return ns;
}
function installLazyProperties(ns, model) {
  const lazy = (name, factory) => {
    Object.defineProperty(ns, name, {
      get() {
        const value = factory();
        Object.defineProperty(ns, name, { value, enumerable: true, configurable: true });
        return value;
      },
      enumerable: true,
      configurable: true
    });
  };
  lazy("attributes", () => ClusterType.attributes(model));
  lazy("commands", () => ClusterType.commands(model));
  lazy("events", () => ClusterType.events(model));
  lazy("features", () => ClusterType.features(model));
  if (!Object.hasOwn(ns, "Cluster")) {
    lazy("Cluster", () => ns);
  }
  lazy("Complete", () => ns);
  if (model.features.length) {
    lazy("Feature", () => {
      const result = {};
      for (const f of model.features) {
        const name = (0, import_general.camelize)(f.title ?? f.name, true);
        result[name] = name;
      }
      return Object.freeze(result);
    });
  }
  installDatatypeGetters(model, lazy);
}
function installDatatypeGetters(model, lazy) {
  const scope = (0, import_model.GeneratorScope)(model);
  for (const [definer, name, location] of scope.namedModels()) {
    if (!location.isLocal || !(definer instanceof import_model.ValueModel)) {
      continue;
    }
    const metatype = definer.effectiveMetatype;
    if (metatype === import_model.Metatype.enum) {
      if (!definer.children.length) {
        continue;
      }
      lazy(name, () => (0, import_EnumForValueModel.EnumForValueModel)(definer));
      if (definer.name === "StatusEnum" || definer.name === "StatusCodeEnum") {
        for (const field of definer.children) {
          if (field.name === "Success") {
            continue;
          }
          let errName = field.name;
          if (!errName.endsWith("Error")) {
            errName = `${errName}Error`;
          }
          lazy(errName, () => {
            let message = field.description;
            if (message === void 0) {
              message = (0, import_general.capitalize)((0, import_general.decamelize)(field.name, " "));
            }
            if (message.endsWith(".")) {
              message = message.slice(0, message.length - 1);
            }
            const clusterCode = typeof field.id === "number" ? field.id : void 0;
            const defaultMessage = message;
            return {
              [errName]: class extends import_StatusResponseError.StatusResponseError {
                constructor(message2 = defaultMessage, code = import_Status.Status.Failure, clusterCode2 = clusterCode) {
                  super(message2, code, clusterCode2);
                }
              }
            }[errName];
          });
        }
      }
    } else if (metatype === import_model.Metatype.object || metatype === import_model.Metatype.bitmap) {
      lazy(name, () => (0, import_ClassForValueModel.ClassForValueModel)(definer));
    }
  }
}
((ClusterType2) => {
  ClusterType2.Unknown = {
    Typing: {},
    id: (0, import_ClusterId.ClusterId)(0),
    name: "Unknown",
    schema: new import_model.ClusterModel({ name: "Unknown" })
  };
  function attributes(model) {
    const result = {};
    for (const attribute of model.attributes) {
      if (import_model.GLOBAL_IDS.has(attribute.id) || attribute.isDisallowed || attribute.effectiveMetatype === void 0) {
        continue;
      }
      const key = attribute.propertyName;
      result[key] = {
        id: attribute.id,
        name: key,
        schema: attribute
      };
    }
    return result;
  }
  ClusterType2.attributes = attributes;
  function commands(model) {
    const result = {};
    for (const command of model.commands) {
      if (!command.isRequest || command.isDisallowed) {
        continue;
      }
      const key = command.propertyName;
      result[key] = {
        id: command.id,
        name: key,
        schema: command
      };
    }
    return result;
  }
  ClusterType2.commands = commands;
  function events(model) {
    const result = {};
    for (const event of model.events) {
      if (event.isDisallowed) {
        continue;
      }
      const key = event.propertyName;
      result[key] = {
        id: event.id,
        name: key,
        schema: event
      };
    }
    return result;
  }
  ClusterType2.events = events;
  function features(model) {
    const result = {};
    for (const feature of model.features) {
      const key = (0, import_general.camelize)(feature.title ?? feature.name);
      if (typeof feature.constraint.value === "number") {
        result[key] = {
          id: feature.constraint.value,
          name: key
        };
      }
    }
    return result;
  }
  ClusterType2.features = features;
})(ClusterType || (ClusterType = {}));
//# sourceMappingURL=ClusterType.js.map
