"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/Entity.ts
var Entity_exports = {};
__export(Entity_exports, {
  createEntity: () => createEntity
});
module.exports = __toCommonJS(Entity_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 setComponent(component, entity, value) {
  const prevValue = getComponentValue(component, entity);
  for (const [key, val] of Object.entries(value)) {
    component.values[key].set(entity, val);
  }
  component.update$.next({ entity, value: [value, prevValue], component });
}
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;
}

// src/Entity.ts
function createEntity(world, components, options) {
  const entity = world.registerEntity(options != null ? options : {});
  if (components) {
    for (const [component, value] of components) {
      setComponent(component, entity, value);
    }
  }
  return entity;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createEntity
});
//# sourceMappingURL=Entity.cjs.map