// src/Component.ts
import { mapObject, uuid } from "@mud-classic/utils";
function hasComponent(component, entity) {
  const map = Object.values(component.values)[0];
  return map.has(entity);
}

// src/World.ts
function createWorld() {
  const entityToIndex = /* @__PURE__ */ new Map();
  const entities = [];
  const components = [];
  let disposers = [];
  function getEntityIndexStrict(entity) {
    const index = entityToIndex.get(entity);
    if (index == null)
      throw new Error("entity does not exist");
    return index;
  }
  function registerEntity({ id, idSuffix } = {}) {
    const entity = id || entities.length + (idSuffix ? "-" + idSuffix : "");
    let index = entityToIndex.get(entity);
    if (index != null)
      return index;
    index = entities.push(entity) - 1;
    entityToIndex.set(entity, index);
    return index;
  }
  function registerComponent(component) {
    components.push(component);
  }
  function dispose(namespace) {
    for (const [, disposer] of disposers.filter((d) => !namespace || d[0] === namespace)) {
      disposer();
    }
    disposers = disposers.filter((d) => namespace && d[0] !== namespace);
  }
  function registerDisposer(disposer, namespace = "") {
    disposers.push([namespace, disposer]);
  }
  function hasEntity(entity) {
    return entityToIndex.get(entity) != null;
  }
  return {
    entities,
    entityToIndex,
    registerEntity,
    components,
    registerComponent,
    dispose,
    registerDisposer,
    getEntityIndexStrict,
    hasEntity
  };
}
function namespaceWorld(world, namespace) {
  return {
    ...world,
    registerDisposer: (disposer) => world.registerDisposer(disposer, namespace),
    dispose: () => world.dispose(namespace)
  };
}
function getEntityComponents(world, entity) {
  return world.components.filter((component) => hasComponent(component, entity));
}
export {
  createWorld,
  getEntityComponents,
  namespaceWorld
};
//# sourceMappingURL=World.js.map