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 from 'react';
import PropTypes from 'prop-types';
import is from 'is';
import { componentWithName } from 'airbnb-prop-types';
import ButtonWithIcon from '../../atoms/buttons/ButtonWithIcon';
import TypeAheadDropdown from '../../forms/TypeAheadDropdown';
import './style.css';

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 (is.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 React.createElement(
      'div',
      { className: 'ma__header-search__wrapper ma__header-search__wrapper--responsive' },
      shouldShowTypeAhead && React.createElement(
        'div',
        { className: 'ma__header-search__pre-filter' },
        React.createElement(TypeAheadDropdown, orgDropdown)
      ),
      React.createElement(
        'div',
        { className: 'ma__header-search' },
        React.createElement(
          'form',
          { action: '#', className: 'ma__form', onSubmit: this.handleSubmit, role: 'search' },
          React.createElement(
            'label',
            {
              htmlFor: headerSearch.id,
              className: 'ma__header-search__label'
            },
            headerSearch.label
          ),
          React.createElement('input', inputProps),
          this.props.suggestions && this.props.suggestions,
          this.props.postInputFilter && React.createElement(
            'div',
            { className: 'ma__header-search__post-filter' },
            this.props.postInputFilter
          ),
          React.createElement(ButtonWithIcon, headerSearch.buttonSearch)
        )
      )
    );
  };

  return HeaderSearch;
}(React.Component);

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

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

export default HeaderSearch;