import {
  buildChangeEvents,
  buildLifecycleEvent,
  diffSnapshots,
  emitSafely,
  entitySnapshot
} from "./chunk-KYR3B3OW.js";

// runtime/base-classes/base-service.ts
var BaseService = class {
  constructor(repository) {
    this.repository = repository;
  }
  repository;
  /**
   * Entity name for event types (e.g., 'account' → 'account.created').
   * Set by generated services. If empty, lifecycle events are skipped.
   */
  entityName;
  /**
   * Event bus for lifecycle/change event emission.
   * Injected via @Inject(EVENT_BUS) @Optional() on generated services.
   * If undefined (no events subsystem installed), emission is silently skipped.
   */
  eventBus;
  /**
   * Whether to emit lifecycle events. Default: true.
   * Override to false in entity YAML via behaviors or in the service class.
   */
  emitLifecycleEvents = true;
  /**
   * Find a single entity by its primary key.
   * Returns null if not found.
   */
  findById(id) {
    return this.repository.findById(id);
  }
  /**
   * Find multiple entities by their primary keys.
   */
  findByIds(ids) {
    return this.repository.findByIds(ids);
  }
  /**
   * List entities with optional filtering/pagination options.
   */
  list(options) {
    return this.repository.list(options);
  }
  /**
   * Count entities matching an optional filter.
   */
  count(where) {
    return this.repository.count(where);
  }
  /**
   * Check whether an entity with the given id exists.
   */
  exists(id) {
    return this.repository.exists(id);
  }
  /**
   * Insert a new entity.
   * Emits a LIFECYCLE 'created' event with entity snapshot.
   */
  async create(input, tx) {
    const result = await this.repository.create(input, tx);
    if (this._shouldEmit()) {
      const snap = entitySnapshot(result);
      const id = result.id;
      const event = buildLifecycleEvent(this.entityName, "created", id, snap);
      void emitSafely(this.eventBus, [event]);
    }
    return result;
  }
  /**
   * Update an existing entity by id.
   * Emits a LIFECYCLE 'updated' event + CHANGE events for each modified field.
   */
  async update(id, input, tx) {
    let before;
    if (this._shouldEmit()) {
      const existing = await this.repository.findById(id);
      if (existing) {
        before = entitySnapshot(existing);
      }
    }
    const result = await this.repository.update(id, input, tx);
    if (this._shouldEmit()) {
      const after = entitySnapshot(result);
      const events = [
        buildLifecycleEvent(this.entityName, "updated", id, after)
      ];
      if (before) {
        const changes = diffSnapshots(before, after);
        if (changes.length > 0) {
          events.push(...buildChangeEvents(this.entityName, id, changes));
        }
      }
      void emitSafely(this.eventBus, events);
    }
    return result;
  }
  /**
   * Delete an entity by id.
   * Emits a LIFECYCLE 'deleted' event.
   */
  async delete(id, tx) {
    await this.repository.delete(id, tx);
    if (this._shouldEmit()) {
      const event = buildLifecycleEvent(this.entityName, "deleted", id);
      void emitSafely(this.eventBus, [event]);
    }
  }
  /** Check whether lifecycle event emission is active. */
  _shouldEmit() {
    return Boolean(
      this.emitLifecycleEvents && this.entityName && this.eventBus
    );
  }
};

export {
  BaseService
};
//# sourceMappingURL=chunk-W72PRNJY.js.map