'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _formatJavaScript = require('../../utils/formatJavaScript');

var _formatJavaScript2 = _interopRequireDefault(_formatJavaScript);

var _commentsFactory = require('../../utils/commentsFactory');

var _commentsFactory2 = _interopRequireDefault(_commentsFactory);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var comments = {
  nodeFetch: 'Node doesn\'t implement fetch so we have to import it',
  graphqlError: 'handle OneGraph errors',
  graphqlData: 'do something with data',
  fetchError: 'handle fetch error'
};

var snippet = {
  language: 'JavaScript',
  prismLanguage: 'javascript',
  name: 'fetch',
  options: [{
    id: 'comments',
    label: 'show comments',
    initial: false
  }, {
    id: 'server',
    label: 'server-side usage',
    initial: false
  }, {
    id: 'asyncAwait',
    label: 'async/await',
    initial: false
  }],
  generate: function generate(_ref) {
    var serverUrl = _ref.serverUrl,
        headers = _ref.headers,
        operations = _ref.operations,
        context = _ref.context,
        options = _ref.options;
    var _operations$ = operations[0],
        variableName = _operations$.variableName,
        variables = _operations$.variables,
        query = _operations$.query;


    console.log(operations);

    var getComment = (0, _commentsFactory2.default)(options.comments, comments);

    var serverComment = options.server ? getComment('nodeFetch') : '';
    var serverImport = options.server ? 'import fetch from "node-fetch"\n' : '';

    var graphqlQuery = 'const ' + variableName + ' = `\n' + query + '`';
    var urlVariable = 'const serverUrl = "' + serverUrl + '"';
    var vars = JSON.stringify(variables, null, 2);
    var heads = JSON.stringify(headers, null, 2);

    var fetchBody = void 0;
    if (options.asyncAwait) {
      fetchBody = '\n    const res = await fetch(serverUrl, {\n      method: \'POST\',\n      headers: ' + heads + ',\n      body: JSON.stringify({ query: ' + variableName + ', variables: ' + vars + ' })\n    })\n    const { errors, data } = await res.json()\n\n    if (errors) {\n      ' + getComment('graphqlError') + '\n      console.error(errors)\n    }\n\n    ' + getComment('graphqlData') + '\n    console.log(data)\n    ';
    } else {
      fetchBody = 'fetch(serverUrl, {\n        method: \'POST\',\n        headers: ' + heads + ',\n        body: JSON.stringify({ query: ' + variableName + ', variables: ' + vars + ' })\n      })\n        .then(res => res.json())\n        .then(({ data, errors }) => {\n          if (errors) {\n            ' + getComment('graphqlError') + '\n            console.error(errors)\n          }\n\n          ' + getComment('graphqlData') + '\n          console.log(data)\n        })\n        .catch(err => {\n          ' + getComment('fetchError') + '\n          console.error(err)\n        })\n      ';
    }

    var snippet = '\n    ' + serverComment + '\n    ' + serverImport + '\n    ' + graphqlQuery + '\n\n    ' + urlVariable + '\n\n    ' + fetchBody;

    return (0, _formatJavaScript2.default)(snippet);
  }
};

exports.default = snippet;