import { DaesangUrlInfoResponse } from '@ucap-webmessenger/api-external';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import { KEY_URL_INFO } from '@app/types';
import { NativeService } from '@ucap-webmessenger/native';
import { WebLinkType } from '../types/web-link.type';

export class SmsUtils {
  constructor(
    private sessionStorageService: SessionStorageService,
    private nativeService: NativeService
  ) {}

  url: string;

  getAuthSms(): boolean {
    const urlInfo: DaesangUrlInfoResponse = this.sessionStorageService.get<
      DaesangUrlInfoResponse
    >(KEY_URL_INFO);

    if (
      !!urlInfo &&
      !!urlInfo.webLinkAllowedList &&
      urlInfo.webLinkAllowedList.indexOf(WebLinkType.Sms) > -1 &&
      !!urlInfo.webLink &&
      urlInfo.webLink.length > 0 &&
      urlInfo.webLink.filter(weblink => weblink.key === WebLinkType.Sms)
        .length > 0
    ) {
      this.url = urlInfo.webLink.filter(
        weblink => weblink.key === WebLinkType.Sms
      )[0].url;
      return true;
    } else {
      return false;
    }
  }

  openSendSms(token: string, calleeNumber?: string[]) {
    const url = this.url.replace(/(\(%USER_TOKEN%\))/g, token);

    this.nativeService.openDefaultBrowser(url);
  }
}
