import {
  LinkRedirect,
  LinkRedirectDto,
  LinkRedirectResponse,
  LinkRedirectUpdateStatusDto
} from 'types/linkRedirects'
import { Maybe } from 'yup/lib/types'
import { Api } from './api'
import notification from 'utils/notification'

export default class LinkRedirectService {
  constructor(private readonly api: Api) {}

  async getLinkRedirect(): Promise<Maybe<{ linkRedirect: LinkRedirect }>> {
    const res: any = await this.api.getLinkRedirect()

    if (!res.ok) return void notification.error({ message: res.message })

    return res.response
  }

  async saveLinkRedirect(
    linkRedirect: LinkRedirectDto
  ): Promise<Maybe<LinkRedirect>> {
    const res: any = await this.api.saveLinkRedirect(linkRedirect)

    if (!res.ok) return void notification.error({ message: res.message })

    return res.response
  }

  async updateLinkRedirectStatus(
    linkRedirect: LinkRedirectUpdateStatusDto
  ): Promise<Maybe<LinkRedirect>> {
    const res: any = await this.api.updateLinkRedirectStatus(linkRedirect)

    if (!res.ok) return void notification.error({ message: res.message })

    return (res.response as LinkRedirectResponse).linkRedirect
  }

  async deleteLinkRedirect(): Promise<Maybe<LinkRedirectDto>> {
    const res: any = await this.api.deleteLinkRedirect()
    if (!res.ok) return void notification.error({ message: res.message })
  }
}
