import { API_URL } from '../../config'
import { fallbackTo, isString } from '../../utils/types'
import { Token, TokenResponse } from './types'

const composeTokenURL = () => (
    API_URL + '/token?'
)

const composeSignInBody = (formData: FormData) => {
    const resFD = new FormData()

    resFD.append('username', fallbackTo(formData.get('email'), isString, ''))
    resFD.append('password', fallbackTo(formData.get('password'), isString, ''))

    return resFD
}

const processToken = (data: TokenResponse): Token => {
    return data.access_token
}

export { composeTokenURL, composeSignInBody, processToken }
