import WRISerializer from 'wri-json-api-serializer';

// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';

/**
 * fetches Geostore
 * @param {Object[]} id - geostore ID.
 * @returns {Object[]} serialized geostore object.
 */
export const fetchGeostore = (id) => {
  logger.info(`Fetch geostore ${id}`);

  return WRIAPI.get(`geostore/${id}`)
    .then((response) => {
      const { status, statusText, data } = response;

      if (status >= 300) {
        logger.error('Error fetching geostore:', `${status}: ${statusText}`);
        throw new Error(statusText);
      }

      return WRISerializer(data);
    })
    .catch(({ response }) => {
      const { status, statusText } = response;
      logger.error(`Error fetching geostore ${id}: ${status}: ${statusText}`);
      throw new Error(`Error fetching geostore ${id}: ${status}: ${statusText}`);
    });
};

export const createGeostore = (geojson) => {
  logger.info('Create geostore');
  return WRIAPI.post('geostore', geojson)
    .then(response => response.data.data)
    .catch((response) => {
      const { status, statusText } = response;
      logger.error(`Error creating geostore: ${status}: ${statusText}`);
      throw new Error(`Error creating geostore: ${status}: ${statusText}`);
    });
};

/**
 * Fetch countries
 */
export const fetchCountries = () => {
  logger.info('Fetch countries');
  return WRIAPI.get('geostore/admin/list')
    .then(array =>
      array.data.data.sort((a, b) => {
        if (a.name < b.name) {
          return -1;
        } else if (a.name > b.name) {
          return 1;
        }
        // eslint-disable-line no-else-return
        return 0;
      }))
    .catch((response) => {
      const { status, statusText } = response;
      logger.error(`Error fetching countries: ${status}: ${statusText}`);
      throw new Error(`Error fetching countries: ${status}: ${statusText}`);
    });
};

/**
 * Get country
 */
export const fetchCountry = (iso) => {
  logger.info(`Fetch country: ${iso}`);
  return WRIAPI.get(`query/134caa0a-21f7-451d-a7fe-30db31a424aa?sql=SELECT name_engli as label, st_asgeojson(the_geom_simple) as geojson, bbox as bounds from gadm28_countries WHERE iso = '${iso}'`)
    .catch((response) => {
      const { status, statusText } = response;
      logger.error(`Error fetching country ${iso}: ${status}: ${statusText}`);
      throw new Error(`Error fetching country ${iso}: ${status}: ${statusText}`);
    });
};

export default {
  createGeostore,
  fetchGeostore,
  fetchCountries,
  fetchCountry
};

@@ -139,17 +139,17 @@
 %0A/**%0A * 
-f
+F
 etches G
@@ -163,24 +163,143 @@
 %0A * 
-@param %7BObject%5B%5D
+Check out the API docs for this endpoint %7B@link https://resource-watch.github.io/doc-api/index-rw.html#geostore%7Chere%7D%0A * @param %7BString
 %7D id
@@ -337,10 +337,8 @@
 ject
-%5B%5D
 %7D se
@@ -1003,24 +1003,235 @@
     %7D);%0A%7D;%0A%0A
+/**%0A * Create a Geostore%0A * Check out the API docs for this endpoint %7B@link https://resource-watch.github.io/doc-api/index-rw.html#create-geostore%7Chere%7D%0A * @param %7BObject%7D geojson Geojson with your geometry%0A */%0A
 export const
@@ -1639,16 +1639,160 @@
 untries%0A
+ * Check out the API docs for this endpoint %7B@link https://resource-watch.github.io/doc-api/index-rw.html#geostore%7Chere%7D%0A * @returns %7BObject%5B%5D%7D%0A
  */%0Aexpo
@@ -2395,16 +2395,16 @@
 %7D;%0A%0A/**%0A
-
  * Get c
@@ -2406,24 +2406,47 @@
 Get country%0A
+ * @param %7BString%7D iso%0A
  */%0Aexport c

Update JSDocs on Geostore