'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 _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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _ReadOnlyInput = require('./ReadOnlyInput');

var _ReadOnlyInput2 = _interopRequireDefault(_ReadOnlyInput);

var _DropdownInput = require('./DropdownInput');

var _DropdownInput2 = _interopRequireDefault(_DropdownInput);

var _Menu = require('./Menu');

var _Menu2 = _interopRequireDefault(_Menu);

var _pathHelpers = require('../helpers/pathHelpers');

var _optionHelpers = require('../helpers/optionHelpers');

var _DropdownMenuInput = require('../../styles/cspace-input/DropdownMenuInput.css');

var _DropdownMenuInput2 = _interopRequireDefault(_DropdownMenuInput);

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 propTypes = _extends({}, _DropdownInput2.default.propTypes, {
  className: _react.PropTypes.string,
  menuHeader: _react.PropTypes.node,
  menuFooter: _react.PropTypes.node,
  open: _react.PropTypes.bool,
  options: _react.PropTypes.arrayOf(_react.PropTypes.shape({
    value: _react.PropTypes.string,
    label: _react.PropTypes.string
  })),
  readOnly: _react.PropTypes.bool,
  renderItemLabel: _react.PropTypes.func,
  valueLabel: _react.PropTypes.string,
  onClose: _react.PropTypes.func,
  onCommit: _react.PropTypes.func,
  onMount: _react.PropTypes.func,
  onOpen: _react.PropTypes.func,
  onUpdate: _react.PropTypes.func
});

var defaultProps = {
  blankable: true,
  options: []
};

var renderMenuHeader = function renderMenuHeader(content) {
  if (content) {
    return _react2.default.createElement(
      'header',
      null,
      content
    );
  }

  return null;
};

var renderMenuFooter = function renderMenuFooter(content) {
  if (content) {
    return _react2.default.createElement(
      'footer',
      null,
      content
    );
  }

  return null;
};

var DropdownMenuInput = function (_Component) {
  _inherits(DropdownMenuInput, _Component);

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

    var _this = _possibleConstructorReturn(this, (DropdownMenuInput.__proto__ || Object.getPrototypeOf(DropdownMenuInput)).call(this, props));

    _this.handleDropdownInputClose = _this.handleDropdownInputClose.bind(_this);
    _this.handleDropdownInputMount = _this.handleDropdownInputMount.bind(_this);
    _this.handleDropdownInputOpen = _this.handleDropdownInputOpen.bind(_this);
    _this.handleMenuRef = _this.handleMenuRef.bind(_this);
    _this.handleMenuSelect = _this.handleMenuSelect.bind(_this);
    _this.focusMenu = _this.focusMenu.bind(_this);

    var valueLabel = props.valueLabel === null || typeof props.valueLabel === 'undefined' ? (0, _optionHelpers.getLabelForValue)(props.options, props.value) : props.valueLabel;

    _this.state = {
      valueLabel: valueLabel,
      open: false,
      value: props.value
    };
    return _this;
  }

  _createClass(DropdownMenuInput, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var onMount = this.props.onMount;


      if (onMount) {
        onMount({ value: this.state.value });
      }
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var valueLabel = nextProps.valueLabel === null || typeof nextProps.valueLabel === 'undefined' ? (0, _optionHelpers.getLabelForValue)(nextProps.options, nextProps.value) : nextProps.valueLabel;

      this.setState({
        valueLabel: valueLabel,
        open: nextProps.open,
        value: nextProps.value
      });
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      var onUpdate = this.props.onUpdate;


      if (onUpdate) {
        onUpdate({ value: this.state.value });
      }
    }
  }, {
    key: 'commit',
    value: function commit(value) {
      var _props = this.props,
          initialValue = _props.value,
          onCommit = _props.onCommit;


      if (onCommit && (value || initialValue) && value !== initialValue) {
        onCommit((0, _pathHelpers.getPath)(this.props), value);
      }
    }
  }, {
    key: 'focusMenu',
    value: function focusMenu() {
      if (this.menu) {
        this.menu.focus();
      }
    }
  }, {
    key: 'handleDropdownInputClose',
    value: function handleDropdownInputClose() {
      this.setState({
        open: false
      });

      var onClose = this.props.onClose;


      if (onClose) {
        onClose();
      }
    }
  }, {
    key: 'handleDropdownInputMount',
    value: function handleDropdownInputMount(_ref) {
      var focusInput = _ref.focusInput;

      this.focusInput = focusInput;
    }
  }, {
    key: 'handleDropdownInputOpen',
    value: function handleDropdownInputOpen() {
      this.setState({
        open: true
      });

      var onOpen = this.props.onOpen;


      if (onOpen) {
        onOpen();
      }
    }
  }, {
    key: 'handleMenuRef',
    value: function handleMenuRef(ref) {
      this.menu = ref;
    }
  }, {
    key: 'handleMenuSelect',
    value: function handleMenuSelect(option) {
      var value = option.value,
          valueLabel = option.label;


      this.setState({
        value: value,
        valueLabel: valueLabel,
        open: false
      });

      this.commit(value);
      this.focusInput();
    }
  }, {
    key: 'render',
    value: function render() {
      var _classNames;

      var _state = this.state,
          open = _state.open,
          value = _state.value,
          valueLabel = _state.valueLabel;

      var _props2 = this.props,
          className = _props2.className,
          menuHeader = _props2.menuHeader,
          menuFooter = _props2.menuFooter,
          options = _props2.options,
          readOnly = _props2.readOnly,
          renderItemLabel = _props2.renderItemLabel,
          blankable = _props2.blankable,
          openProp = _props2.open,
          valueLabelProp = _props2.valueLabel,
          onClose = _props2.onClose,
          onCommit = _props2.onCommit,
          onMount = _props2.onMount,
          onOpen = _props2.onOpen,
          onUpdate = _props2.onUpdate,
          remainingProps = _objectWithoutProperties(_props2, ['className', 'menuHeader', 'menuFooter', 'options', 'readOnly', 'renderItemLabel', 'blankable', 'open', 'valueLabel', 'onClose', 'onCommit', 'onMount', 'onOpen', 'onUpdate']);

      var inputValue = valueLabel;

      if (readOnly) {
        return _react2.default.createElement(_ReadOnlyInput2.default, { value: inputValue });
      }

      var classes = (0, _classnames2.default)(className, (_classNames = {}, _defineProperty(_classNames, _DropdownMenuInput2.default.common, true), _defineProperty(_classNames, _DropdownMenuInput2.default.open, open), _classNames));

      return _react2.default.createElement(
        _DropdownInput2.default,
        _extends({}, remainingProps, {
          className: classes,
          focusPopup: this.focusMenu,
          open: open,
          spellCheck: false,
          value: inputValue,
          onClose: this.handleDropdownInputClose,
          onMount: this.handleDropdownInputMount,
          onOpen: this.handleDropdownInputOpen
        }),
        renderMenuHeader(menuHeader),
        _react2.default.createElement(_Menu2.default, {
          options: options,
          ref: this.handleMenuRef,
          tabIndex: '-1',
          renderItemLabel: renderItemLabel,
          value: value,
          onSelect: this.handleMenuSelect
        }),
        renderMenuFooter(menuFooter)
      );
    }
  }]);

  return DropdownMenuInput;
}(_react.Component);

exports.default = DropdownMenuInput;


DropdownMenuInput.propTypes = propTypes;
DropdownMenuInput.defaultProps = defaultProps;