'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _index = require('../../../../index');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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; }

var TeaserSearch = function (_Component) {
  _inherits(TeaserSearch, _Component);

  function TeaserSearch(props) {
    _classCallCheck(this, TeaserSearch);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.onClick = function (e) {
      e.preventDefault();
      var _this$props = _this.props,
          target = _this$props.target,
          queryInput = _this$props.queryInput;

      var query = _this.inputRef.current.value;
      if (query.length > 0) {
        var searchURL = queryInput ? target.replace('{' + queryInput + '}', query) : target;
        _this.redirect(searchURL);
      }
    };

    _this.redirect = function (searchURL) {
      if (window.location !== window.parent.location) {
        window.parent.location.assign(searchURL);
      } else {
        window.location.assign(searchURL);
      }
    };

    _this.inputRef = _react2.default.createRef();
    return _this;
  }

  TeaserSearch.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        placeholder = _props.placeholder,
        id = _props.id,
        queryInput = _props.queryInput,
        rest = _objectWithoutProperties(_props, ['placeholder', 'id', 'queryInput']);

    return _react2.default.createElement(_index.HeaderSearch, _extends({
      buttonSearch: {
        'aria-label': '',
        onClick: function onClick(e) {
          return _this2.onClick(e);
        },
        text: 'Search',
        usage: ''
      },
      defaultText: '',
      id: id,
      label: 'Search terms',
      onSubmit: function onSubmit(e) {
        return _this2.onSubmit(e);
      },
      inputRef: this.inputRef,
      placeholder: placeholder
    }, rest));
  };

  return TeaserSearch;
}(_react.Component);

TeaserSearch.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The target url of the search bar */
  target: _propTypes2.default.string.isRequired,
  /** The id of the search bar */
  id: _propTypes2.default.string.isRequired,
  /** The query input variable to replace in the target url with the user entered term */
  queryInput: _propTypes2.default.string.isRequired,
  /** Placeholder text of the search bar. */
  placeholder: _propTypes2.default.string.isRequired
} : {};

exports.default = TeaserSearch;
module.exports = exports['default'];