import map from 'lodash/map'

import { currencySymbols } from 'config'

import type {
  SubscriptionsData,
  Subscription,
  Packages,
  Season,
} from 'requests/getSubscriptions'

import { isSubscribePopup } from 'helpers'

import { getName } from 'features/Name'

import type {
  MatchSubscriptions,
  MatchSubscription,
  MatchPackage,
  Match,
  Desciption,
} from '../types'
import { SubscriptionType } from '../types'
import {
  passLexics,
  passNameKeys,
  descriptionLexics,
} from './config'

type SubscriptionArgs = {
  match: Match,
  season: Season,
  subscription: Subscription,
  suffix: string,
}

const transformPackage = ({
  match,
  season,
  subscription,
  suffix,
}: SubscriptionArgs) => (
  type: SubscriptionType.Month | SubscriptionType.Year,
): Array<MatchPackage> => (
  map(subscription.packages[type], (subscriptionPackage, rawKey) => {
    const key = rawKey as keyof Packages
    const isLeaguePass = key === 'all'
    const teamName = getName({
      nameObj: match[passNameKeys[key]],
      suffix,
    })
    const description: Desciption = isLeaguePass
      ? {
        lexic: subscription.lexic3,
        values: {},
      }
      : {
        lexic: descriptionLexics[key],
        values: {
          season: season.name,
          team: teamName,
        },
      }
    const nameLexic = isLeaguePass ? subscription.lexic2 : null
    return {
      currency: currencySymbols[subscriptionPackage.currency_iso],
      description,
      id: `${key} ${subscriptionPackage.sub.id}`,
      name: teamName,
      nameLexic,
      originalObject: subscriptionPackage,
      pass: passLexics[key],
      price: subscriptionPackage.price,
      type,
    }
  })
)

type PackagesArgs = {
  match: Match,
  season: Season,
  subscription: Subscription,
  suffix: string,
}

const transformPackages = ({
  match,
  season,
  subscription,
  suffix,
}: PackagesArgs): MatchSubscription => {
  const { pay_per_view: payPerView } = subscription.packages
  const team1Name = getName({ nameObj: match.team1, suffix })
  const team2Name = getName({ nameObj: match.team2, suffix })
  const transformByType = transformPackage({
    match,
    season,
    subscription,
    suffix,
  })

  const packages = {
    [SubscriptionType.Month]: transformByType(SubscriptionType.Month),
    [SubscriptionType.Year]: transformByType(SubscriptionType.Year),
    [SubscriptionType.PayPerView]:
      payPerView && !isSubscribePopup()
        ? [{
          currency: currencySymbols[payPerView.currency_iso],
          description: {
            lexic: 'description_match_live_and_on_demand',
            values: {},
          },
          id: '0',
          name: `${team1Name} - ${team2Name}`,
          originalObject: payPerView,
          pass: 'pass_match_access',
          price: payPerView.price,
          type: SubscriptionType.PayPerView,
        }]
        : [],
  }

  return {
    ...subscription,
    packages,
  }
}

type SubsciptionsArgs = {
  match: Match,
  subscriptions: SubscriptionsData,
  suffix: string,
}

export const transformSubscriptions = ({
  match,
  subscriptions,
  suffix,
}: SubsciptionsArgs): MatchSubscriptions => map(
  subscriptions.data,
  (subscription) => transformPackages({
    match,
    season: subscriptions.season,
    subscription,
    suffix,
  }),
)
