import { validateRelations } from 'src/core/strings/constants/interface.constants';
import { BaseManager } from '../base.manager';
import { OPERATION } from 'src/core/strings/constants/base.constants';

export abstract class BaseCustomManager<Entity> extends BaseManager {
  protected result: any;
  abstract get entityTarget(): any;

  setData(entity: any): void {
    this.data = entity;
  }

  get validateRelations(): validateRelations[] {
    return [];
  }

  async prepareData(): Promise<void> {
    if (this.data)
      Object.assign(this.data, {
        editor_id: this.user.id,
        editor_name: this.user.name,
        updated_at: new Date().getTime(),
      });
  }

  abstract getResult(): any;

  async publishEvents() {
    if (!this.eventTopics.length) return;
    for (const topic of this.eventTopics) {
      let data;
      if (!topic.data) {
        data = await this.dataService.getOneByOptions({
          where: {
            id: this.result['id'],
          },
          relations: topic.relations,
        });
      }

      this.eventBus.publishAll([
        new topic.topic({
          id: data?.['id'] ?? topic?.data?.['id'],
          old: null,
          data: data ?? topic.data,
          user: this.user,
          description: '',
          module: this.tableName,
          op: OPERATION.UPDATE,
        }),
      ]);
    }
  }
}
