"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 Schema_exports = {};
__export(Schema_exports, {
  Schema: () => Schema
});
module.exports = __toCommonJS(Schema_exports);
var import_DatatypeModel = require("#models/DatatypeModel.js");
var import_general = require("@matter/general");
var import_errors = require("../decoration/errors.js");
var import_Semantics = require("../decoration/semantics/Semantics.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const schemaMap = /* @__PURE__ */ new WeakMap();
function Schema(source) {
  let model;
  if (typeof source === "object" && source !== null && "tag" in source) {
    source.finalize();
    model = source;
  } else if (typeof source === "function" && "Tag" in source) {
    model = void 0;
  } else {
    const mapped = schemaMap.get(source);
    if (mapped !== void 0) {
      if (typeof mapped === "function") {
        const resolved = mapped();
        resolved.finalize();
        schemaMap.set(source, resolved);
        model = resolved;
      } else {
        mapped.finalize();
        model = mapped;
      }
    } else if (typeof source === "function") {
      const semantics = import_Semantics.Semantics.classOf(source);
      semantics.finalize();
      model = semantics.semanticModel;
    }
  }
  if (!model) {
    return;
  }
  if (model.tag !== "cluster" && !model.isType) {
    throw new import_general.ImplementationError(`Model ${model.name} tag ${model.tag} is not legal for schema`);
  }
  return model;
}
((Schema2) => {
  function Required(source) {
    const schema = Schema2(source);
    if (schema === void 0) {
      throw new import_errors.MissingMetadataError(`Metadata missing for class ${source.name}`);
    }
    return schema;
  }
  Schema2.Required = Required;
  function set(target, source) {
    schemaMap.set(target, source);
  }
  Schema2.set = set;
  Schema2.empty = new import_DatatypeModel.DatatypeModel({ name: "Empty", type: "struct" });
  Schema2.empty.finalize();
})(Schema || (Schema = {}));
//# sourceMappingURL=Schema.js.map
