'use strict';

exports.__esModule = true;
exports.DropInput = undefined;

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 _reactDom = require('react-dom');

var _styledComponents = require('styled-components');

var _grommetIcons = require('grommet-icons');

var _grommet = require('grommet');

var _StyledDropInput = require('./StyledDropInput');

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

  function DropInput() {
    var _temp, _this, _ret;

    _classCallCheck(this, DropInput);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = { open: false }, _this.onUpdateValue = function (value) {
      var onChange = _this.props.onChange;

      _this.inputControlRef.value = value;
      _this.setState({ open: false });
      if (onChange) {
        onChange({ target: _this.inputControlRef });
      }
    }, _this.onOpen = function (e) {
      var _this$props = _this.props,
          onOpen = _this$props.onOpen,
          disabled = _this$props.disabled;

      _this.setState({ open: true });
      if (onOpen && !disabled) {
        onOpen(e);
      }
    }, _this.onClose = function (e) {
      var _this$props2 = _this.props,
          onClose = _this$props2.onClose,
          disabled = _this$props2.disabled;

      _this.setState({ open: false });
      if (onClose && !disabled) {
        onClose(e);
      }
    }, _this.onKeyDown = function (e) {
      var onKeyDown = _this.props.onKeyDown;

      _this.setState({ open: true });
      if (onKeyDown) {
        onKeyDown(e);
      }
    }, _this.onKeyUp = function (e) {
      var onKeyUp = _this.props.onKeyUp;

      _this.setState({ open: false });
      if (onKeyUp) {
        onKeyUp(e);
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  DropInput.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var value = nextProps.value,
        defaultValue = nextProps.defaultValue;

    if (value !== this.props.value || defaultValue !== this.props.defaultValue) {
      this.onClose();
    }
  };

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

    var _props = this.props,
        a11yTitle = _props.a11yTitle,
        a11yDropTitle = _props.a11yDropTitle,
        dropAlign = _props.dropAlign,
        dropTarget = _props.dropTarget,
        update = _props.update,
        widgets = _props.widgets,
        onOpen = _props.onOpen,
        onClose = _props.onClose,
        onKeyDown = _props.onKeyDown,
        onKeyUp = _props.onKeyUp,
        dropContent = _props.dropContent,
        dropIcon = _props.dropIcon,
        disabled = _props.disabled,
        rest = _objectWithoutProperties(_props, ['a11yTitle', 'a11yDropTitle', 'dropAlign', 'dropTarget', 'update', 'widgets', 'onOpen', 'onClose', 'onKeyDown', 'onKeyUp', 'dropContent', 'dropIcon', 'disabled']);

    var open = this.state.open;

    if (typeof update === 'function') {
      update(this.onUpdateValue);
    }
    var numWidgets = (dropContent ? 1 : 0) + (widgets ? widgets.length : 0);
    var decorations = void 0;
    if (numWidgets > 0) {
      var drop = void 0;
      if (dropContent) {
        drop = _react2.default.createElement(_grommet.DropButton, {
          a11yTitle: a11yDropTitle,
          disabled: disabled,
          dropAlign: dropAlign,
          dropTarget: dropTarget,
          open: open,
          tabIndex: '-1',
          focusIndicator: false,
          onOpen: this.onOpen,
          onClose: this.onClose,
          dropContent: dropContent,
          icon: dropIcon
        });
      }
      decorations = _react2.default.createElement(
        _StyledDropInput.StyledWidgetsContainer,
        {
          align: 'center',
          direction: 'row'
        },
        widgets.map(function (widget, index) {
          return _react2.default.createElement(_grommet.Button, _extends({ tabIndex: '-1', key: 'widget_' + index }, widget));
        }),
        drop
      );
    }
    return _react2.default.createElement(
      _grommet.Keyboard,
      { onDown: this.onKeyDown, onUp: this.onKeyUp },
      _react2.default.createElement(
        _StyledDropInput.StyledDropInputContainer,
        null,
        _react2.default.createElement(
          _styledComponents.ThemeContext.Consumer,
          null,
          function (theme) {
            return _react2.default.createElement(_StyledDropInput.StyledDropInput, _extends({
              ref: function ref(_ref) {
                _this2.inputControlRef = _ref && (0, _reactDom.findDOMNode)(_ref);
              },
              theme: theme,
              disabled: disabled,
              numWidgets: numWidgets,
              'aria-label': a11yTitle
            }, rest));
          }
        ),
        decorations
      )
    );
  };

  return DropInput;
}(_react.Component);

DropInput.defaultProps = {
  dropAlign: { top: 'bottom', right: 'left' },
  dropIcon: _react2.default.createElement(_grommetIcons.FormDown, null),
  type: 'text',
  widgets: []
};


var DropInputDoc = void 0;
if (process.env.NODE_ENV !== 'production') {
  DropInputDoc = require('./doc').default(DropInput); // eslint-disable-line global-require
}

var DropInputWrapper = DropInputDoc || DropInput;

exports.DropInput = DropInputWrapper;