import { Injectable } from '@angular/core';
import '@angular/compiler';


declare var EqualSDK: any;

@Injectable({
  providedIn: 'root'
})
export class EqualSdkService {
  private equalClient: any;

  constructor() { }

  loadScript(): Promise<void> {
    return new Promise((resolve, reject) => {
      const script = document.createElement('script');
      script.src = 'https://test.equal.in/sdk/js/idg-business.js';
      script.onload = () => resolve();
      script.onerror = () => reject();
      document.body.appendChild(script);
    });
  }

  initSDK(clientId: string, token: string, idp: string, onOpenGateway: Function, onClosureGateway: Function, options: any): void {
    this.equalClient = new EqualSDK({
      "client_id": clientId,
      "idempotency_id": idp,
      "token": token,
      "options": options
    });

    this.equalClient.openGateway(
      (response: any) => onOpenGateway(response),
      (response: any) => onClosureGateway(response)
    );
  }
}
