/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ElementTag } from "#common/ElementTag.js";
import { MatterElement } from "../elements/index.js";
import { ModelTraversal } from "../logic/ModelTraversal.js";
import { FabricModel } from "./FabricModel.js";
import { Globals } from "./Globals.js";
import { Model } from "./Model.js";
import { ResourceBundle } from "./Resource.js";
import { ScopeModel } from "./ScopeModel.js";
import { SemanticNamespaceModel } from "./SemanticNamespaceModel.js";
class MatterModel extends ScopeModel {
  tag = MatterElement.Tag;
  revision;
  #permanentDatatypes;
  #resources;
  /**
   * The default instance of the canonical MatterModel (also exported directly simply as "Matter").
   */
  static standard = new MatterModel({
    name: "Matter",
    children: Object.values(Globals)
  });
  /**
   * Clusters.
   */
  get clusters() {
    return this.scope.membersOf(this, { tags: [ElementTag.Cluster] });
  }
  /**
   * Device types.
   */
  get deviceTypes() {
    return this.scope.membersOf(this, { tags: [ElementTag.DeviceType] });
  }
  /**
   * Semantic tag namespaces.
   */
  get semanticNamespaces() {
    return this.all(SemanticNamespaceModel);
  }
  /**
   * Global datatypes.
   */
  get datatypes() {
    return this.scope.membersOf(this, { tags: [ElementTag.Datatype] });
  }
  /**
   * Global fields.
   */
  get fields() {
    return this.scope.membersOf(this, { tags: [ElementTag.Field] });
  }
  /**
   * Global attributes.
   */
  get attributes() {
    return this.scope.membersOf(this, { tags: [ElementTag.Attribute] });
  }
  /**
   * Fabrics.
   */
  get fabrics() {
    return this.all(FabricModel);
  }
  /**
   * All sub-cluster global elements from this model.
   *
   * This is the set of utility datatypes required by cluster definitions.
   *
   * The returned elements are clones as we use this to initialize empty models for testing or diagnostic purposes.
   */
  get seedGlobals() {
    return this.children.filter((child) => child.isSeed).map((child) => child.clone());
  }
  /**
   * MatterModel always owns itself.
   */
  get root() {
    return this;
  }
  /**
   * The set of "permanent" datatypes.
   *
   * These are datatypes owned by this model with the "isSeed" value set.  For performance reasons we disallow
   * overriding these values.
   */
  get permanentDatatypes() {
    if (this.#permanentDatatypes) {
      return this.#permanentDatatypes;
    }
    this.#permanentDatatypes = Object.fromEntries(
      this.children.filter((model) => model.tag === ElementTag.Datatype && model.isSeed).map((model) => [model.name, model])
    );
    this.children.onNameChanged = (name, model) => {
      if (model === void 0) {
        delete this.#permanentDatatypes[name];
      } else {
        this.#permanentDatatypes[model.name] = model;
      }
    };
    return this.#permanentDatatypes;
  }
  /**
   * The resource pool for the node.
   */
  get resources() {
    return this.#resources ?? ResourceBundle.default;
  }
  set resources(resources) {
    this.#resources = resources;
  }
  constructor(definition, ...children) {
    if (definition instanceof Model) {
      super(definition, ...children);
      return;
    }
    super(
      {
        name: "Matter",
        children: [],
        ...definition
      },
      ...children
    );
    if (!(definition instanceof Model)) {
      this.revision = definition?.revision;
    }
  }
  toElement(omitResources = false, extra) {
    return super.toElement(omitResources, {
      revision: this.revision,
      ...extra
    });
  }
  static Tag = MatterElement.Tag;
}
MatterModel.register();
ModelTraversal.fallbackRoot = MatterModel.standard;
export {
  MatterModel
};
//# sourceMappingURL=MatterModel.js.map
