"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/World.ts
var World_exports = {};
__export(World_exports, {
  createWorld: () => createWorld,
  getEntityComponents: () => getEntityComponents,
  namespaceWorld: () => namespaceWorld
});
module.exports = __toCommonJS(World_exports);

// src/Component.ts
var import_utils = require("@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));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createWorld,
  getEntityComponents,
  namespaceWorld
});
//# sourceMappingURL=World.cjs.map