import {
  Products,
  GetProductsQuery,
  GetProductsResponse,
  GenerateWaywardLinkBody,
  GenerateWaywardLinkSuccess
} from '@komi-app/affiliate-sdk'
import { getSDKConfig } from 'config'
import { SdkConfig } from '@komi-app/sales-sdk'
import { Maybe } from 'utils/types'

export default class AffiliateService {
  private affiliate: Products
  constructor() {
    const affiliateConfig: SdkConfig = getSDKConfig('affiliate')
    this.affiliate = new Products(affiliateConfig)
  }

  init(profileId: string) {
    this.affiliate.init(profileId)
  }

  getWaywardProducts = async (
    payload: GetProductsQuery
  ): Promise<Maybe<GetProductsResponse>> => {
    try {
      return await this.affiliate.getWaywardProducts(payload)
    } catch (err) {
      console.error(err)
      return void 0
    }
  }

  generateWaywardLink = async (
    payload: GenerateWaywardLinkBody
  ): Promise<Maybe<GenerateWaywardLinkSuccess>> => {
    try {
      return await this.affiliate.generateWaywardLink(payload)
    } catch (err) {
      console.error(err)
      return void 0
    }
  }
}
