import { Repository, TreeRepository } from 'typeorm';
import { BaseReadService } from './base-read.service';

export abstract class BaseTreeReadService<
  Entity,
> extends BaseReadService<Entity> {
  constructor(
    private dataRepository: Repository<Entity>,
    private treeRepository: TreeRepository<Entity>,
  ) {
    super(dataRepository);
  }

  async findRoots() {
    return this.treeRepository.findRoots();
  }

  async findDescendants(parent, relations = []): Promise<Entity[]> {
    return this.treeRepository.findDescendants(parent, {
      relations: relations,
    });
  }

  async findDescendantsTree(parent, relations = []): Promise<Entity> {
    return this.treeRepository.findDescendantsTree(parent, {
      relations: relations,
    });
  }

  async findAncestors(parent, relations = []): Promise<Entity[]> {
    return await this.treeRepository.findAncestors(parent, {
      relations: relations,
    });
  }

  async findAncestorsTree(parent, relations = []): Promise<Entity> {
    return await this.treeRepository.findAncestorsTree(parent, {
      relations: relations,
    });
  }
}
