"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 element_exports = {};
__export(element_exports, {
  element: () => element
});
module.exports = __toCommonJS(element_exports);
var import_errors = require("#decoration/errors.js");
var import_FieldSemantics = require("#decoration/semantics/FieldSemantics.js");
var import_Semantics = require("#decoration/semantics/Semantics.js");
var import_CommandModel = require("#models/CommandModel.js");
var import_Model = require("#models/Model.js");
var import_Schema = require("#models/Schema.js");
var import_general = require("@matter/general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function element(kind, ...modifiers) {
  let modelType;
  if (kind.Tag === "response") {
    modelType = import_CommandModel.CommandModel;
  } else {
    modelType = kind;
  }
  const forceType = !(modifiers[0] instanceof modelType);
  return (0, import_general.Decorator)((target, context) => {
    let semantics = import_Semantics.Semantics.of(context);
    if (kind.Tag === "response") {
      semantics.modelType = import_CommandModel.CommandModel;
      if (semantics.response === void 0) {
        if (typeof context.name !== "string") {
          throw new import_errors.InvalidMetadataError(
            `Cannot specify response for ${String(context.name) || "(anonymous)"} because element name is ${typeof context.name}`
          );
        }
        const response = new import_FieldSemantics.FieldSemantics(semantics.owner, context.name);
        response.modelType = import_CommandModel.CommandModel;
        response.mutableModel.name = `${context.name}Response`;
        response.mutableModel.isResponse = true;
        semantics = semantics.response = response;
      }
    }
    if (forceType) {
      semantics.modelType = modelType;
    }
    if (context.kind === "class") {
      semantics.new = target;
    }
    let result = void 0;
    for (const modifier of modifiers) {
      switch (typeof modifier) {
        case "number":
          semantics.mutableModel.id = modifier;
          continue;
        case "string":
          semantics.mutableModel.name = modifier;
          continue;
        case "function":
          if ("Tag" in modifier) {
            semantics.modelType = modifier;
          } else if (import_general.Decorator.is(modifier)) {
            const subresult = modifier(target, context);
            if (subresult) {
              if (result) {
                throw new import_errors.MetadataConflictError(
                  `Multiple modifiers returned a value for ${String(context.name || "(anonymous)")}`
                );
              }
              result = subresult;
            }
          } else {
            const model = (0, import_Schema.Schema)(modifier);
            if (model === void 0) {
              throw new import_errors.InvalidMetadataError(
                `Cannot use undecorated class ${modifier.name || "(anonymous)"} as type for ${String(context.name || "(anonymous)")}`
              );
            }
            model.finalize();
            semantics.mutableModel = model;
          }
          continue;
        case "object":
          if (modifier instanceof import_Model.Model) {
            semantics.mutableModel = modifier;
            continue;
          }
          {
            const model = (0, import_Schema.Schema)(modifier);
            if (model !== void 0) {
              model.finalize();
              semantics.mutableModel = model;
              continue;
            }
          }
          break;
      }
      throw new import_errors.InvalidMetadataError(
        `Unsupported modifier ${modifier} for ${String(context.name || "(anonymous)")}`
      );
    }
    return result;
  });
}
((element2) => {
  function property(kind) {
    return (...modifiers) => {
      return element2(kind, ...modifiers);
    };
  }
  element2.property = property;
  function klass(kind) {
    return (...modifiers) => {
      return element2(kind, ...modifiers);
    };
  }
  element2.klass = klass;
})(element || (element = {}));
//# sourceMappingURL=element.js.map
