'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _is = require('is');

var _is2 = _interopRequireDefault(_is);

var _airbnbPropTypes = require('airbnb-prop-types');

var _ButtonWithIcon = require('../../atoms/buttons/ButtonWithIcon');

var _ButtonWithIcon2 = _interopRequireDefault(_ButtonWithIcon);

var _TypeAheadDropdown = require('../../forms/TypeAheadDropdown');

var _TypeAheadDropdown2 = _interopRequireDefault(_TypeAheadDropdown);

require('./style.css');

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

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 HeaderSearch = function (_React$Component) {
  _inherits(HeaderSearch, _React$Component);

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

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleChange = function (event) {
      var query = event.target.value;
      _this.setState({ value: query });
      // invokes custom function if passed in the component
      if (typeof _this.props.onChange === 'function') {
        _this.props.onChange(query);
      }
    };

    _this.handleSubmit = function (event) {
      if (_is2.default.fn(_this.props.onSubmit)) {
        _this.props.onSubmit(event);
      }
    };

    _this.state = { value: _this.props.defaultText };
    return _this;
  }

  HeaderSearch.prototype.render = function render() {
    var headerSearch = this.props;
    var orgDropdown = headerSearch.orgDropdown;
    var shouldShowTypeAhead = orgDropdown && orgDropdown.dropdownButton && orgDropdown.inputText;
    var inputProps = {
      id: headerSearch.id,
      className: 'ma__header-search__input',
      placeholder: headerSearch.placeholder,
      onChange: this.handleChange,
      type: 'search',
      value: this.state.value
    };
    if (this.props.inputRef) {
      inputProps.ref = this.props.inputRef;
    }
    return _react2.default.createElement(
      'div',
      { className: 'ma__header-search__wrapper ma__header-search__wrapper--responsive' },
      shouldShowTypeAhead && _react2.default.createElement(
        'div',
        { className: 'ma__header-search__pre-filter' },
        _react2.default.createElement(_TypeAheadDropdown2.default, orgDropdown)
      ),
      _react2.default.createElement(
        'div',
        { className: 'ma__header-search' },
        _react2.default.createElement(
          'form',
          { action: '#', className: 'ma__form', onSubmit: this.handleSubmit, role: 'search' },
          _react2.default.createElement(
            'label',
            {
              htmlFor: headerSearch.id,
              className: 'ma__header-search__label'
            },
            headerSearch.label
          ),
          _react2.default.createElement('input', inputProps),
          this.props.suggestions && this.props.suggestions,
          this.props.postInputFilter && _react2.default.createElement(
            'div',
            { className: 'ma__header-search__post-filter' },
            this.props.postInputFilter
          ),
          _react2.default.createElement(_ButtonWithIcon2.default, headerSearch.buttonSearch)
        )
      )
    );
  };

  return HeaderSearch;
}(_react2.default.Component);

HeaderSearch.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The ID for the input */
  id: _propTypes2.default.string,
  /** The label text for the input */
  label: _propTypes2.default.string,
  /** The placeholder text for the input */
  placeholder: _propTypes2.default.string,
  /** The Search button */
  buttonSearch: _propTypes2.default.shape(_ButtonWithIcon2.default.propTypes),
  /** Custom submit function */
  onSubmit: _propTypes2.default.func,
  /** Custom change function for the text input */
  onChange: _propTypes2.default.func,
  /** Default input text value */
  defaultText: _propTypes2.default.string,
  /** Render suggestions as passable element */
  suggestions: _propTypes2.default.element,
  /** @molecules/TypeAheadDropdown */
  orgDropdown: _propTypes2.default.shape(_propTypes2.default.TypeAheadDropdown),
  /** postInputFilter passable component */
  postInputFilter: (0, _airbnbPropTypes.componentWithName)('SelectBox'),
  /** A ref object as created by React.createRef(). Will be applied to the input element. */
  inputRef: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.shape({ current: _propTypes2.default.any })])
} : {};

HeaderSearch.defaultProps = {
  id: 'header-search',
  label: 'Search terms',
  placeholder: 'Search Mass.gov',
  buttonSearch: {
    'aria-label': '',
    usage: 'secondary'
  }
};

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