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

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

import React, { Component } from 'react';
import { findDOMNode } from 'react-dom';
import { FormDown } from 'grommet-icons/es6/icons/FormDown';

import { compose } from 'recompose';
import { DropButton } from 'grommet/es6/components/DropButton';
import { Keyboard } from 'grommet/es6/components/Keyboard';
import { Button } from 'grommet/es6/components/Button';
import { withTheme } from 'grommet/es6/components/hocs';

import { StyledDropInput, StyledDropInputContainer, StyledWidgetsContainer } from './StyledDropInput';

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,
        theme = _props.theme,
        disabled = _props.disabled,
        rest = _objectWithoutProperties(_props, ['a11yTitle', 'a11yDropTitle', 'dropAlign', 'dropTarget', 'update', 'widgets', 'onOpen', 'onClose', 'onKeyDown', 'onKeyUp', 'dropContent', 'dropIcon', 'theme', '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 = React.createElement(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 = React.createElement(
        StyledWidgetsContainer,
        {
          align: 'center',
          direction: 'row'
        },
        widgets.map(function (widget, index) {
          return React.createElement(Button, _extends({ tabIndex: '-1', key: 'widget_' + index }, widget));
        }),
        drop
      );
    }
    return React.createElement(
      Keyboard,
      { onDown: this.onKeyDown, onUp: this.onKeyUp },
      React.createElement(
        StyledDropInputContainer,
        null,
        React.createElement(StyledDropInput, _extends({
          ref: function ref(_ref) {
            _this2.inputControlRef = _ref && findDOMNode(_ref);
          },
          theme: theme,
          disabled: disabled,
          numWidgets: numWidgets,
          'aria-label': a11yTitle
        }, rest)),
        decorations
      )
    );
  };

  return DropInput;
}(Component);

DropInput.defaultProps = {
  dropAlign: { top: 'bottom', right: 'left' },
  dropIcon: React.createElement(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 = compose(withTheme)(DropInputDoc || DropInput);

export { DropInputWrapper as DropInput };