import { Injectable } from '@angular/core';
import { environment } from '@src/environments/environment';
import { CICRegistry, FileGetter } from '@cicnet/cic-client';
import { HttpGetter } from '@app/_helpers';
import { Web3Service } from '@app/_services/web3.service';

@Injectable({
  providedIn: 'root',
})
export class RegistryService {
  static fileGetter: FileGetter = new HttpGetter();
  private static registry: CICRegistry;

  constructor() {}

  public static async getRegistry(): Promise<CICRegistry> {
    if (!RegistryService.registry) {
      RegistryService.registry = new CICRegistry(
        Web3Service.getInstance(),
        environment.registryAddress,
        'Registry',
        RegistryService.fileGetter,
        ['../../assets/js/block-sync/data']
      );
      RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
      await RegistryService.registry.load();
    }
    return RegistryService.registry;
  }
}
