"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 import_general.Callable {
  #items;
  #nameIndex;
  #idIndex;
  constructor(items = []) {
    let self;
    super(function(key) {
      return self.for(key);
    });
    self = this;
    delete this.length;
    this.#items = items;
  }
  /**
   * Add items.  Used during construction only.
   */
  push(...items) {
    this.#items.push(...items);
    this.#nameIndex = void 0;
    this.#idIndex = void 0;
  }
  get length() {
    return this.#items.length;
  }
  at(index) {
    return this.#items.at(index);
  }
  [Symbol.iterator]() {
    return this.#items[Symbol.iterator]();
  }
  forEach(fn) {
    this.#items.forEach(fn);
  }
  map(fn) {
    return this.#items.map(fn);
  }
  filter(fn) {
    return this.#items.filter(fn);
  }
  find(fn) {
    return this.#items.find(fn);
  }
  some(fn) {
    return this.#items.some(fn);
  }
  every(fn) {
    return this.#items.every(fn);
  }
  reduce(fn, initialValue) {
    return this.#items.reduce(fn, initialValue);
  }
  for(key, type) {
    if (key === void 0) {
      return void 0;
    }
    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;
  }
  require(key, type) {
    if (key === void 0) {
      return void 0;
    }
    const result = this.for(key, type);
    if (result === void 0) {
      throw new import_general.ImplementationError(`Required member "${key}" not found`);
    }
    return result;
  }
  extend(key, properties, ...children) {
    return this.require(key).extend(properties, ...children);
  }
  ofType(type) {
    return this.#items.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.#items) {
      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.#items) {
      const name = model.propertyName;
      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
