import { request } from '../../../utils';
import { API } from '../../../types';
import { ResponseType } from '../response-type';
import { FeatureCollection } from '@turf/turf';

export interface PostGetSettingsReturn extends ResponseType {
  sharing: 0 | 1;
  sharing_regions: 0 | 1;
  sharing_refresh_interval: number;
}

export interface PostGetUsersLocationReturn extends ResponseType {
  geojson: GeoJSON.FeatureCollection;
}

export interface PostGetUserCountReturn extends ResponseType {
  count: number;
  friends_count: number;
}

export interface PostGetCountriesReturn extends ResponseType {
  countries: {
    count: number;
    country: number;
    flag: string | null;
    name: string;
  }[];
}

export const locationApi = {
  getSettings: (token: string) =>
    request.postForm<PostGetSettingsReturn>(API.GET_LOCATION_SETTINGS, { token }),
  setSettings: (token: string, sharing: 0 | 1) =>
    request.postForm<ResponseType>(API.SET_LOCATION_SETTINGS, { token, sharing }),
  updateLocation: (token: string, lat: number, lng: number) =>
    request.postForm<ResponseType>(API.UPDATE_LOCATION, { token, lat, lng }),
  getUsersLocation: (token: string) =>
    request.postForm<PostGetUsersLocationReturn>(API.GET_USERS_LOCATION, { token }),
  getUsersCount: (token: string) =>
    request.postForm<PostGetUserCountReturn>(API.GET_USERS_COUNT, { token }),
  setSettingsRegions: (token: string, sharing: 0 | 1) =>
    request.postForm<ResponseType>(API.SET_LOCATION_REGIONS, { token, sharing }),
  getCountries: (token: string) =>
    request.postForm<PostGetCountriesReturn>(API.GET_COUNTRIES_FOR_LOCATION_DATA, { token }),
  getLocationFiltered: (token: string, friends: 0 | 1, trusted: 0 | 1, countries?: string) =>
    request.postForm<ResponseType>(API.GET_USERS_LOCATION_FILTERED, {
      token,
      friends,
      trusted,
      countries
    })
};
