"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);

// src/Indexer.ts
var Indexer_exports = {};
__export(Indexer_exports, {
  createIndexer: () => createIndexer
});
module.exports = __toCommonJS(Indexer_exports);

// src/Component.ts
var import_utils = require("@mud-classic/utils");

// src/constants.ts
var OptionalTypes = [
  10 /* OptionalEntity */,
  12 /* OptionalEntityArray */,
  2 /* OptionalNumber */,
  6 /* OptionalNumberArray */,
  4 /* OptionalString */,
  8 /* OptionalStringArray */,
  14 /* OptionalT */
];

// src/Component.ts
function getComponentValue(component, entity) {
  const value = {};
  const schemaKeys = Object.keys(component.schema);
  for (const key of schemaKeys) {
    const val = component.values[key].get(entity);
    if (val === void 0 && !OptionalTypes.includes(component.schema[key]))
      return void 0;
    value[key] = val;
  }
  return value;
}
function getComponentEntities(component) {
  return component.entities();
}

// src/Indexer.ts
function createIndexer(component) {
  const valueToEntities = /* @__PURE__ */ new Map();
  function getEntitiesWithValue(value) {
    const entities = valueToEntities.get(getValueKey(value));
    return entities ? /* @__PURE__ */ new Set([...entities]) : /* @__PURE__ */ new Set();
  }
  function getValueKey(value) {
    return Object.values(value).join("/");
  }
  function add(entity, value) {
    if (!value)
      return;
    const valueKey = getValueKey(value);
    let entitiesWithValue = valueToEntities.get(valueKey);
    if (!entitiesWithValue) {
      entitiesWithValue = /* @__PURE__ */ new Set();
      valueToEntities.set(valueKey, entitiesWithValue);
    }
    entitiesWithValue.add(entity);
  }
  function remove(entity, value) {
    if (!value)
      return;
    const valueKey = getValueKey(value);
    const entitiesWithValue = valueToEntities.get(valueKey);
    if (!entitiesWithValue)
      return;
    entitiesWithValue.delete(entity);
  }
  for (const entity of getComponentEntities(component)) {
    const value = getComponentValue(component, entity);
    add(entity, value);
  }
  const subscription = component.update$.subscribe(({ entity, value }) => {
    remove(entity, value[1]);
    add(entity, value[0]);
  });
  component.world.registerDisposer(() => subscription == null ? void 0 : subscription.unsubscribe());
  return { ...component, getEntitiesWithValue };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createIndexer
});
//# sourceMappingURL=Indexer.cjs.map