require('es6-promise').polyfill();
require('isomorphic-fetch');

/**
 * pnut-butter.js
 *
 * Wrapper library for the pnut.io API.
 *
 * @returns {Object}
 */
const pnut = () => {
  const _base = 'https://api.pnut.io/v0'

  let _token = '';

  const call = (uri, {httpMethod = 'GET', resultAs = 'json', data = {}} = {}) => {

    let auth = {};
    if (_token !== '') {
      auth = {
        "Authorization": `Bearer ${_token}`
      }
    }

    let options = {
      method: httpMethod,
      headers: auth
    }

    console.dir(data)

    if (httpMethod !== 'GET' && Object.keys(data).length > 0) {
      options.body = JSON.stringify(data)
      options.headers['Content-Type'] = 'application/json'
    }

    console.dir(options)

    return fetch(uri, options).then(function (response) {
      if (resultAs === 'response') {
        return response;
      } else {
        return response[resultAs]();
      }
    });
  }

  return {
    /**
     * Set auth token
     * @param {string} newToken - API access token
     */
    set token(newToken) {
      _token = newToken;
    },

    /**
     * Send request with custom endpoint / parameters
     * @param {string} endpoint - The custom call
     * @param {string} method - HTTP verb, defaults to GET
     * @return {Promise}
     */
     custom(endpoint, method = 'GET', data = {}) {
      endpoint = endpoint.startsWith('/') ? `${endpoint}` : `/${endpoint}`
      return call(`${_base}${endpoint}`, {httpMethod: method, data: data})
     },

    /**
     * The global timeline
     * @returns {Promise}
     */
    global() {
      return call(`${_base}/posts/streams/global`);
    },

    /**
     * Retrieve a user object.
     * @param {string} userId - The user id
     * @returns {Promise}
     */
    user(userId) {
      return call(`${_base}/users/${userId}`);
    },

    /**
     * Retrieve a users avatar image.
     * @param {string} userId - The user id
     * @param {boolean} [true] - Should the return value be prebuffered via the fetch API?
     */
    avatar(userId, buffered = true) {
      let options = buffered ? {resultAs: 'buffer'} : {resultAs: 'response'}
      return call(`${_base}/users/${userId}/avatar`, options);
    },

    /**
     * Retrieve a users cover image.
     * @param {string} userId - The user id
     * @param {boolean} [true] - Should the return value be prebuffered via the fetch API?
     */
    cover(userId, buffered = true) {
      let options = buffered ? {resultAs: 'buffer'} : {resultAs: 'response'}
      return call(`${_base}/users/${userId}/cover`, options);
    },

    /**
     * Retrieve a list of specified user objects. Only retrieves the first 200 found
     * @param {array} userIds - An array of user ids
     * @returns {Promise}
     */
    users(userIds) {
      return call(`${_base}/users?ids=${userIds.join(',')}`);
    },

    /**
     * Retrieve a post object.
     * @param {string} postId - The post id
     * @returns {Promise}
     */
    post(postId) {
      return call(`${_base}/posts/${postId}`);
    },

    /**
     * Retrieve a list of specified post objects. Only retrieves the first 200 found
     * @param {array} ids - An array of post ids
     * @returns {Promise}
     */
    posts(ids) {
      return call(`${_base}/posts?ids=${ids.join(',')}`);
    },

    /**
     * Retrieve a list of previous versions of a post, not including the most recent.
     * @param {string} postId - The post id
     * @returns {Promise}
     */
    revisions(postId) {
      return call(`${_base}/posts/${postId}/revisions`);
    },

    /**
     * Retrieve posts mentioning the specified user.
     * @param {string} userid - The user id
     * @returns {Promise}
     */
    mentions(userId) {
      return call(`${_base}/users/${userId}/mentions`);
    },

    /**
     * Retrieve posts created by a specific user
     * @param {string} userId - The user id
     * @returns {Promise}
     */
    postsFrom(userId) {
      return call(`${_base}/users/${userId}/posts`);
    },

    /**
     * Retrieve a stream of all posts that include the specified tag.
     * @param {string} tag – The tag name
     * @returns {Promise}
     */
    postsTaggedWith(tag) {
      return call(`${_base}/posts/tag/${encodeURI(tag)}`);
    },

    /**
     * Retrieve posts within a thread.
     * @param {string} postId - The post id
     * @returns {Promise}
     */
    thread(postId) {
      return call(`${_base}/posts/${postId}/thread`);
    },

    /**
     * Retrieve actions executed against a post.
     * @param {string} postId - The post id
     * @returns {Promise}
     */
    actions(postId) {
      return call(`${_base}/posts/${postId}/actions`);
    }
  }
}


module.exports = pnut();
@@ -513,31 +513,8 @@
  %7D%0A%0A
-    console.dir(data)%0A%0A
     
@@ -685,34 +685,8 @@
  %7D%0A%0A
-    console.dir(options)%0A%0A
     

Remove debug statements :)