import mobileOK from '@lib/utils/mok_Key_Manager_v1.0.3.js';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class MokClientService implements OnModuleInit {
  private mok?: any;

  constructor(private configService: ConfigService) {}

  get serviceId() {
    return this.mok['getServiceId']();
  }

  get siteUrl() {
    return this.mok['getSiteUrl']();
  }

  onModuleInit() {
    mobileOK['keyInit'](
      this.configService.get('mok.keyPath'),
      this.configService.get('mok.password'),
    );
    mobileOK['setSiteUrl'](this.configService.get('mok.siteUrl'));
    this.mok = mobileOK;
  }

  encrypt(data: string) {
    return this.mok['RSAEncrypt'](data);
  }

  serverEncrypt(key: string, data: string) {
    return this.mok['RSAServerEncrypt'](key, data);
  }

  getResult(data: string) {
    return JSON.parse(this.mok['getResult'](data));
  }
}
