/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { isDeepEqual, serialize } from "@matter/general";
const empty = /* @__PURE__ */ new Map();
const aspectCache = /* @__PURE__ */ new Map();
class Aspect {
  definition;
  isEmpty = false;
  get valid() {
    return !this.errors;
  }
  constructor(definition) {
    this.definition = definition;
  }
  /**
   * Test for logical equivalence.
   */
  equals(other) {
    if (!(other instanceof this.constructor)) {
      return false;
    }
    return isDeepEqual(this.valueOf(), other.valueOf());
  }
  valueOf() {
    return this.isEmpty ? void 0 : this.toString();
  }
  error(code, message) {
    if (!this.errors) {
      this.errors = [];
    }
    this.errors.push({
      code,
      source: `${this.constructor.name} ${serialize(this.definition)}`,
      message
    });
  }
  static create(definition) {
    if (definition === void 0) {
      let none = empty.get(this);
      if (!none) {
        none = new this(definition);
        empty.set(this, none);
      }
      return none;
    }
    if (definition instanceof this) {
      return definition;
    }
    if (typeof definition === "string") {
      let slot = aspectCache.get(this);
      if (slot === void 0) {
        slot = {};
        aspectCache.set(this, slot);
      }
      let some = slot[definition];
      if (some) {
        return some;
      }
      some = definition === "" ? this.create() : new this(definition);
      slot[definition] = some;
      return some;
    }
    return new this(definition);
  }
  freeze() {
    Object.freeze(this);
  }
}
export {
  Aspect
};
//# sourceMappingURL=Aspect.js.map
