import { rxHttpGet } from 'redux-rx-http'
import {SEARCH_ADDRESS} from '../constants'
import {AbstractFormField} from '../interfaces'

export interface SearchAddressAction {
    type: 'SEARCH_ADDRESS_REQUEST'
    args: {
        name: string,
        formName: string,
    }
}

export interface SearchAddressSuccessAction {
    type: 'SEARCH_ADDRESS_SUCCESS'
    args: {
        name: string,
        formName: string,
    }
}

export interface SearchAddressErrorAction {
    type: 'SEARCH_ADDRESS_ERROR'
    args: {
        name: string,
        formName: string,
    }
}

export type AddressAction = SearchAddressAction
    | SearchAddressSuccessAction
    | SearchAddressErrorAction

export const searchAddress = (name: string, formName: string, field: AbstractFormField<string>) =>
    rxHttpGet('/search', SEARCH_ADDRESS, {
        country: 'GB',
        query: field.value,
        take: 7,
    })
