import axios from 'axios'
import { invertMapKeysAndValues } from './utility'
import {
  AuthHeaders,
  AuthResponse,
  DeviceStorage,
  SingleLayerStringMap
} from '../types'

const authHeaderKeys: Array<string> = [
  'access-token',
  'token-type',
  'client',
  'expiry',
  'uid'
]

export const setAuthHeaders = (
  Storage: DeviceStorage,
  headers: AuthHeaders
): void => {
  authHeaderKeys.forEach((key: string) => {
    Storage.getItem(key).then((fromStorage: string) => {
      const value = headers[key] || fromStorage
      axios.defaults.headers.common[key] = value
    })
  })
}

export const persistAuthHeadersInDeviceStorage = (
  Storage: DeviceStorage,
  headers: AuthHeaders
): void => {
  authHeaderKeys.forEach((key: string) => {
    Storage.getItem(key).then((fromStorage: string) => {
      const value = headers[key] || fromStorage
      Storage.setItem(key, value) // <--- Not really needed
    })
  })
}

export const deleteAuthHeaders = (): void => {
  authHeaderKeys.forEach((key: string) => {
    delete axios.defaults.headers.common[key]
  })
}

export const deleteAuthHeadersFromDeviceStorage = async (
  Storage: DeviceStorage
): Promise<void> => {
  authHeaderKeys.forEach((key: string) => {
    Storage.removeItem(key)
  })
}

export const getUserAttributesFromResponse = (
  userAttributes: SingleLayerStringMap,
  response: AuthResponse
): SingleLayerStringMap => {
  const invertedUserAttributes: SingleLayerStringMap = invertMapKeysAndValues(
    userAttributes
  )
  const userAttributesBackendKeys: string[] = Object.keys(
    invertedUserAttributes
  )
  const userAttributesToReturn: SingleLayerStringMap = {}
  Object.keys(response.data.data).forEach((key: string) => {
    if (userAttributesBackendKeys.indexOf(key) !== -1) {
      userAttributesToReturn[invertedUserAttributes[key]] =
        response.data.data[key]
    }
  })
  return userAttributesToReturn
}
