/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { camelize } from "@matter/general";
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(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 = 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;
  }
}
export {
  MutableModelIndex
};
//# sourceMappingURL=ModelIndex.js.map
