'use strict';

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

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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 Search = function (_Component) {
  _inherits(Search, _Component);

  function Search() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Search);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Search.__proto__ || Object.getPrototypeOf(Search)).call.apply(_ref, [this].concat(args))), _this), _this.componentWillMount = function () {
      document.addEventListener('keydown', _this.onKeyDown, false);
    }, _this.componentWillUnmount = function () {
      document.removeEventListener('keydown', _this.onKeyDown, false);
    }, _this.onKeyDown = function (e) {
      e.preventDefault();

      var temp = _this.props.filterText;

      // Match Backspace
      if (e.key === 'Backspace') {
        _this.props.setFilterText(temp.slice(0, -1));
      }

      // Match Esc
      else if (e.key === 'Escape') {
          temp = '';
          _this.props.setFilterText(temp);
        }

        // Match exactly one alphanumeric char
        else if (/^[a-z0-9-_]$/i.test(e.key) && temp.length < 10) {
            temp += e.key;
            _this.props.setFilterText(temp);
          }
    }, _this.clearText = function () {
      _this.props.setFilterText('');
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Search, [{
    key: 'render',
    value: function render() {

      var text = this.props.filterText.length > 0 ? this.props.filterText : this.props.placeholder;
      var textStyle = this.props.filterText.length > 0 ? filterStyle : placeholderStyle;

      // Clear button
      var clearButton = void 0;
      if (this.props.filterText.length > 0) {
        clearButton = _react2.default.createElement('div', { className: 'fa fa-times', style: clearButtonStyle, onClick: this.clearText });
      }

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          'div',
          { style: { width: '100%', display: 'flex', justifyContent: 'center' } },
          _react2.default.createElement('div', { style: powerButtonDragStyle }),
          _react2.default.createElement('div', { style: dragBarStyle }),
          _react2.default.createElement('div', { style: clearButtonDragStyle })
        ),
        _react2.default.createElement(
          'div',
          { style: searchStyle },
          _react2.default.createElement(
            'div',
            { style: textStyle },
            text
          ),
          _react2.default.createElement('div', { className: 'fa fa-power-off', style: powerButtonStyle, onClick: this.props.powerOff }),
          clearButton,
          _react2.default.createElement('hr', { style: dividerStyle })
        )
      );
    }
  }]);

  return Search;
}(_react.Component);

exports.default = Search;


var searchStyle = {
  position: 'fixed',
  top: 0,
  width: '100%',
  backgroundColor: '#fff',
  zIndex: '1',
  display: 'flex',
  alignItems: 'center',
  justifyContent: 'center'
};

var filterStyle = {
  border: '0',
  boxShadow: 'none',
  textAlign: 'center',
  height: '64px',
  fontSize: '20px',
  paddingTop: '20px',
  color: '#666666',
  cursor: 'default'
};

var placeholderStyle = {
  border: '0',
  boxShadow: 'none',
  textAlign: 'center',
  height: '64px',
  fontSize: '20px',
  paddingTop: '20px',
  color: '#a1a1a1',
  cursor: 'default',
  opacity: '0.7'
};

var dividerStyle = {
  position: 'absolute',
  width: '100%',
  top: '100%',
  margin: '0',
  padding: '0'
};

var powerButtonStyle = {
  position: 'absolute',
  left: '32px',
  cursor: 'pointer',
  color: '#666666',
  WebkitAppRegion: 'no-drag'
};

var clearButtonStyle = {
  position: 'absolute',
  right: '32px',
  cursor: 'pointer',
  color: '#666666',
  WebkitAppRegion: 'no-drag'
};

var dragBarStyle = {
  flex: 1,
  width: '100%',
  height: '64px',
  WebkitAppRegion: 'drag'
};

var powerButtonDragStyle = {
  flex: '0 0 48px',
  height: '64px'
};

var clearButtonDragStyle = {
  flex: '0 0 48px',
  height: '64px'
};