var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import InstantSearch from '../components/InstantSearch';
import version from './version';

/**
 * Creates a specialized root InstantSearch component. It accepts
 * an algolia client and a specification of the root Element.
 * @param {function} defaultAlgoliaClient - a function that builds an Algolia client
 * @param {object} root - the defininition of the root of an InstantSearch sub tree.
 * @returns {object} an InstantSearch root
 */
export default function createInstantSearch(defaultAlgoliaClient, root) {
  var _class, _temp;

  return _temp = _class = function (_Component) {
    _inherits(CreateInstantSearch, _Component);

    function CreateInstantSearch() {
      var _ref;

      _classCallCheck(this, CreateInstantSearch);

      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      var _this = _possibleConstructorReturn(this, (_ref = CreateInstantSearch.__proto__ || Object.getPrototypeOf(CreateInstantSearch)).call.apply(_ref, [this].concat(args)));

      if (_this.props.searchClient) {
        if (_this.props.appId || _this.props.apiKey || _this.props.algoliaClient) {
          throw new Error('react-instantsearch:: `searchClient` cannot be used with `appId`, `apiKey` or `algoliaClient`.');
        }
      }

      if (_this.props.algoliaClient) {
        // eslint-disable-next-line no-console
        console.warn('`algoliaClient` option was renamed `searchClient`. Please use this new option before the next major version.');
      }

      _this.client = _this.props.searchClient || _this.props.algoliaClient || defaultAlgoliaClient(_this.props.appId, _this.props.apiKey, {
        _useRequestCache: true
      });

      if (typeof _this.client.addAlgoliaAgent === 'function') {
        _this.client.addAlgoliaAgent('react-instantsearch ' + version);
      }
      return _this;
    }

    _createClass(CreateInstantSearch, [{
      key: 'componentWillReceiveProps',
      value: function componentWillReceiveProps(nextProps) {
        var props = this.props;

        if (nextProps.searchClient) {
          this.client = nextProps.searchClient;
        } else if (nextProps.algoliaClient) {
          this.client = nextProps.algoliaClient;
        } else if (props.appId !== nextProps.appId || props.apiKey !== nextProps.apiKey) {
          this.client = defaultAlgoliaClient(nextProps.appId, nextProps.apiKey);
        }

        if (typeof this.client.addAlgoliaAgent === 'function') {
          this.client.addAlgoliaAgent('react-instantsearch ' + version);
        }
      }
    }, {
      key: 'render',
      value: function render() {
        return React.createElement(
          InstantSearch,
          {
            createURL: this.props.createURL,
            indexName: this.props.indexName,
            searchState: this.props.searchState,
            onSearchStateChange: this.props.onSearchStateChange,
            onSearchParameters: this.props.onSearchParameters,
            root: this.props.root,
            searchClient: this.client,
            algoliaClient: this.client,
            refresh: this.props.refresh,
            resultsState: this.props.resultsState
          },
          this.props.children
        );
      }
    }]);

    return CreateInstantSearch;
  }(Component), _class.propTypes = {
    algoliaClient: PropTypes.object,
    searchClient: PropTypes.object,
    appId: PropTypes.string,
    apiKey: PropTypes.string,
    children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]),
    indexName: PropTypes.string.isRequired,
    createURL: PropTypes.func,
    searchState: PropTypes.object,
    refresh: PropTypes.bool.isRequired,
    onSearchStateChange: PropTypes.func,
    onSearchParameters: PropTypes.func,
    resultsState: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
    root: PropTypes.shape({
      Root: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]).isRequired,
      props: PropTypes.object
    })
  }, _class.defaultProps = {
    refresh: false,
    root: root
  }, _temp;
}