import {HttpRequest, RestConstant, StatusCode} from 'constants';

const initialState = {
  _data: undefined,
  _error: undefined,
};

export const searchPlace = async keyword => {
  const newState = {...initialState};
  // eslint-disable-next-line prettier/prettier
  const access_token = 'pk.eyJ1IjoiYW5kaXdhd2FuayIsImEiOiJja2ZuZmViejUxbHltMnFrdTFreHZ1MDIyIn0.n7s5b5vF6YqcKrMHxB8p1Q';
  const config = `country=id&types=place,address,country,postcode,region,locality,district,neighborhood,poi&language=id&access_token=${access_token}`;
  const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${keyword}.json?${config}`;
  try {
    const response = await HttpRequest.get(url);
    switch (response.request.status) {
      case StatusCode.OK:
        newState._data = response.data.features;
        return newState;
      case StatusCode.UNAUTHORIZED:
        newState._error = response.response.data;
        return newState;
      case StatusCode.SERVER_ERROR:
        newState._error = response.response.data;
        return newState;
    }
  } catch (error) {
    console.log(error);
  }
};
