"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 ModelIndex_exports = {};
__export(ModelIndex_exports, {
  MutableModelIndex: () => MutableModelIndex
});
module.exports = __toCommonJS(ModelIndex_exports);
var import_general = require("@matter/general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class MutableModelIndex extends Array {
  #nameIndex;
  #idIndex;
  for(key, type) {
    let untyped;
    if (typeof key === "number") {
      untyped = this.#ids.get(key);
    } else {
      untyped = this.#names.get((0, import_general.camelize)(key));
    }
    if (untyped === void 0) {
      return void 0;
    }
    if (Array.isArray(untyped)) {
      if (type) {
        return untyped.find((m) => m instanceof type);
      }
      return untyped[0];
    }
    if (type && !(untyped instanceof type)) {
      return void 0;
    }
    return untyped;
  }
  ofType(type) {
    return this.filter((m) => m instanceof type);
  }
  get #ids() {
    if (this.#idIndex) {
      return this.#idIndex;
    }
    const index = this.#idIndex = /* @__PURE__ */ new Map();
    for (const model of this) {
      if (model.id === void 0) {
        continue;
      }
      const already = index.get(model.id);
      if (already) {
        if (Array.isArray(already)) {
          already.push(model);
        } else {
          index.set(model.id, [already, model]);
        }
      } else {
        index.set(model.id, model);
      }
    }
    return this.#idIndex;
  }
  get #names() {
    if (this.#nameIndex) {
      return this.#nameIndex;
    }
    const index = this.#nameIndex = /* @__PURE__ */ new Map();
    for (const model of this) {
      const name = (0, import_general.camelize)(model.name);
      const already = index.get(name);
      if (already) {
        if (Array.isArray(already)) {
          already.push(model);
        } else {
          index.set(name, [already, model]);
        }
      } else {
        index.set(name, model);
      }
    }
    return this.#nameIndex;
  }
}
//# sourceMappingURL=ModelIndex.js.map
