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

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

/**
 * Fetchs widgets according to params.
 *
 * @param {Object[]} params - params sent to the API.
 * @returns {Object[]} array of serialized widgets.
 */
export const fetchWidgets = (params = {}, headers = {}, _meta = false) => {
  logger.info('fetches widgets');
  return WRIAPI.get('/widget', {
    headers: {
      ...WRIAPI.defaults.headers,
      // TO-DO: forces the API to not cache, this should be removed at some point
      'Upgrade-Insecure-Requests': 1,
      ...headers
    },
    params: {
      env: process.env.API_ENV,
      application: process.env.APPLICATIONS,
      ...params
    },
    transformResponse: [].concat(
      WRIAPI.defaults.transformResponse,
      (({ data, meta }) => ({ widgets: data, meta }))
    )
  })
    .then((response) => {
      const { status, statusText, data } = response;
      const { widgets, meta } = data;
      if (status >= 300) {
        logger.error('Error fetching widgets:', `${status}: ${statusText}`);
        throw new Error(statusText);
      }

      if (_meta) {
        return {
          widgets: WRISerializer({ data: widgets }),
          meta
        };
      }

      return WRISerializer({ data: widgets });
    })
    .catch((response) => {
      const { status, statusText } = response;

      logger.error(`Error fetching widgets: ${status}: ${statusText}`);
      throw new Error(`Error fetching widgets: ${status}: ${statusText}`);
    });
};


/**
 * fetches data for a specific widget.
 *
 * @param {String} id - widget id.
 * @param {Object[]} params - params sent to the API.
 * @returns {Object} serialized specified widget.
 */
export const fetchWidget = (id, params = {}) => {
  if (!id) throw Error('widget id is mandatory to perform this fetching.');
  logger.info(`Fetches widget: ${id}`);

  return WRIAPI.get(`/widget/${id}`, {
    headers: {
      ...WRIAPI.defaults.headers,
      // TO-DO: forces the API to not cache, this should be removed at some point
      'Upgrade-Insecure-Requests': 1
    },
    params: {
      ...params,
      env: process.env.API_ENV,
      application: process.env.APPLICATIONS
    }
  })
    .then((response) => {
      const { status, statusText, data } = response;

      if (status >= 300) {
        if (status === 404) {
          logger.debug(`Widget '${id}' not found, ${status}: ${statusText}`);
        } else {
          logger.error(`Error fetching widget: ${id}: ${status}: ${statusText}`);
        }
        throw new Error(statusText);
      }
      return WRISerializer(data);
    })
    .catch(({ response }) => {
      const { status, statusText } = response;

      logger.error(`Error fetching widget ${id}: ${status}: ${statusText}`);
      throw new Error(`Error fetching widget ${id}: ${status}: ${statusText}`);
    });
};

/**
 * Deletes a specified widget.
 * This fetch needs authentication.
 *
 * @param {*} id - widget ID to be deleted.
 * @param {string} token - user's token.
 * @returns {Object} fetch response.
 */
export const deleteWidget = (widgetId, datasetId, token) => {
  logger.info(`deletes widget: ${widgetId}`);

  return WRIAPI.delete(`/dataset/${datasetId}/widget/${widgetId}`, {
    headers: {
      ...WRIAPI.defaults.headers,
      Authorization: token
    }
  })
    .then((response) => {
      const { status, statusText } = response;

      if (status >= 300) {
        if (status === 404) {
          logger.debug(`Widget '${widgetId}' not found, ${status}: ${statusText}`);
        } else {
          logger.error(`Error deleting widget: ${widgetId}: ${status}: ${statusText}`);
        }
        throw new Error(statusText);
      }
      return response;
    })
    .catch(({ response }) => {
      const { status, statusText } = response;

      logger.error(`Error deleting widget ${widgetId}: ${status}: ${statusText}`);
      throw new Error(`Error deleting widget ${widgetId}: ${status}: ${statusText}`);
    });
};

export default {
  fetchWidgets,
  fetchWidget,
  deleteWidget
};

@@ -173,24 +173,159 @@
 o params.%0A *
+ Check out the API docs for this endpoint %7B@link https://resource-watch.github.io/doc-api/index-rw.html#how-to-obtain-all-widgets%7Chere%7D
 %0A * @param %7B
@@ -322,34 +322,32 @@
 * @param %7BObject
-%5B%5D
 %7D params - param
@@ -357,32 +357,149 @@
 ent to the API.%0A
+ * @param %7BObject%7D headers - headers sent to the API.%0A * @param %7Bboolean%7D _meta - should meta be in response or not.%0A
  * @returns %7BObj
@@ -1815,17 +1815,17 @@
 %0A/**%0A * 
-f
+F
 etches d
@@ -1849,24 +1849,160 @@
 c widget.%0A *
+ Check out the API docs for this endpoint %7B@link https://resource-watch.github.io/doc-api/index-rw.html#how-obtain-a-single-widget%7Chere%7D
 %0A * @param %7B
@@ -2042,18 +2042,16 @@
  %7BObject
-%5B%5D
 %7D params
@@ -3361,16 +3361,141 @@
 tion.%0A *
+ Check out the API docs for this endpoint %7B@link https://resource-watch.github.io/doc-api/index-rw.html#delete-a-widget%7Chere%7D
 %0A * @par
@@ -3502,13 +3502,24 @@
 am %7B
-*
+String
 %7D 
+w
 id
+getId
  - w
@@ -3553,17 +3553,60 @@
 @param %7B
-s
+String%7D datasetId - dataset ID.%0A * @param %7BS
 tring%7D t

Add JSDoc to Widget