'use strict';

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

var _reason = require('reason');

var _reason2 = _interopRequireDefault(_reason);

var _capitalizeFirstLetter = require('../../utils/capitalizeFirstLetter');

var _capitalizeFirstLetter2 = _interopRequireDefault(_capitalizeFirstLetter);

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

exports.default = {
  language: 'ReasonML',
  prismLanguage: 'reason',
  name: 'reason-apollo',
  options: [{
    id: 'client',
    label: 'with ApolloClient',
    initial: false
  }],
  generate: function generate(_ref) {
    var serverUrl = _ref.serverUrl,
        variableName = _ref.variableName,
        operationType = _ref.operationType,
        operationName = _ref.operationName,
        operation = _ref.operation,
        options = _ref.options;

    var queryName = (0, _capitalizeFirstLetter2.default)(operationName);
    var componentName = queryName + (0, _capitalizeFirstLetter2.default)(operationType);

    var apolloClient = options.client ? '\n  \n    let inMemoryCache = ApolloInMemoryCache.createInMemoryCache();\n    \n    let serverUrl = "' + serverUrl + '"\n    \n    let httpLink =\n      ApolloLinks.createHttpLink(~uri=serverUrl, ());\n    \n    let instance =\n      ReasonApollo.createApolloClient(~link=httpLink, ~cache=inMemoryCache, ());\n      ' : '';

    return _reason2.default.printRE(_reason2.default.parseRE('   \n' + apolloClient + '\n\nmodule ' + queryName + ' = [%graphql\n{|\n' + operation + '\n|}\n];\n\nmodule ' + componentName + ' = ReasonApollo.CreateQuery(' + queryName + ');\n\nlet make = _children => {\n  render: _ => {\n    ' + (options.client ? '<ReasonApollo.Provider client=instance>' : '') + '\n    <' + componentName + '>\n      ...{\n            ({result}) =>\n              switch (result) {\n              | Loading => <div> {ReasonReact.string("Loading")} </div>\n              | Error(error) =>\n                <div> {ReasonReact.string(error##message)} </div>\n              | Data(response) =>\n                <div> {\n                /* Handle response */\n                } </div>\n              }\n          }\n    </' + componentName + '>\n    ' + (options.client ? '</ReasonApollo.Provider>' : '') + '\n  }\n};'));
  }
};