'use strict';

const _ = require('underscore');
const request = require('superagent');

const isClient = typeof window !== 'undefined';
const root = isClient ? window : process;

module.exports = (method, pathname, options, callback) => {
  options = _.omit(options, isClient ? ['actionType', 'cookie'] : ['actionType']);
  while (/^\//.test(pathname)) { pathname = pathname.slice(1); }
  options = options ? options : {};
  let req = request[method](`${root.env.APP_URL}/${pathname}`)
    .accept('application/vnd.coah-v1+json')
    .timeout(root.env.TIMEOUT);
  options.cookie && req.set('Cookie', options.cookie);
  options.query && req.query(options.query);
  return req.end(callback);
};
