'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PhoneDropdown = 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 _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);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _dropdown = require('kitten/components/dropdowns/dropdown');

var _dropdownButton = require('kitten/components/dropdowns/dropdown-button');

var _elementHelper = require('kitten/helpers/dom/element-helper');

var _elementHelper2 = _interopRequireDefault(_elementHelper);

var _assign = require('core-js/library/fn/object/assign');

var _assign2 = _interopRequireDefault(_assign);

var _emitter = require('kitten/helpers/utils/emitter');

var _emitter2 = _interopRequireDefault(_emitter);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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 PhoneDropdown = exports.PhoneDropdown = function (_React$Component) {
  _inherits(PhoneDropdown, _React$Component);

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

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

    _this.state = {
      isExpanded: false,
      contentPosition: { top: 0, left: 0 },
      arrowPosition: { top: 0, left: 0 },
      positionOn: 'right'
    };

    _this.handleButtonClick = _this.handleButtonClick.bind(_this);
    _this.handlePositionUpdate = _this.handlePositionUpdate.bind(_this);
    _this.handleOtherDropdownsOpening = _this.handleOtherDropdownsOpening.bind(_this);
    return _this;
  }

  _createClass(PhoneDropdown, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      this.handlePositionUpdate();

      _emitter2.default.on('dropdown:opening:trigger', this.handleOtherDropdownsOpening);
      _emitter2.default.on('element:update', this.handlePositionUpdate);

      if (this.props.closeEvents) {
        this.props.closeEvents.forEach(function (ev) {
          window.addEventListener(ev, _this2.handleOtherDropdownsOpening);
        });
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      var _this3 = this;

      if (this.props.closeEvents) {
        this.props.closeEvents.forEach(function (ev) {
          window.removeEventListener(ev, _this3.handleOtherDropdownsOpening);
        });
      }

      _emitter2.default.off('dropdown:opening:trigger', this.handleOtherDropdownsOpening);
      _emitter2.default.off('element:update', this.handlePositionUpdate);
    }

    // Component methods.

  }, {
    key: 'getButtonId',
    value: function getButtonId() {
      return 'k-PhoneDropdown';
    }
  }, {
    key: 'canComputeSize',
    value: function canComputeSize() {
      return typeof this.refs.dropdown != 'undefined';
    }
  }, {
    key: 'getDropdownContentElement',
    value: function getDropdownContentElement() {
      return this.refs.dropdown.refs.dropdownContent;
    }
  }, {
    key: 'getDropdownButtonElement',
    value: function getDropdownButtonElement() {
      return this.refs.dropdownButton.refs.dropdownButton;
    }
  }, {
    key: 'getDropdownButtonHalfWidth',
    value: function getDropdownButtonHalfWidth() {
      return _elementHelper2.default.getComputedWidth(this.getDropdownButtonElement()) / 2;
    }
  }, {
    key: 'getDropdownButtonPositionLeft',
    value: function getDropdownButtonPositionLeft() {
      return this.getDropdownButtonElement().offsetLeft + this.getDropdownButtonHalfWidth();
    }
  }, {
    key: 'getDropdownContentElementWidth',
    value: function getDropdownContentElementWidth() {
      return _elementHelper2.default.getComputedWidth(this.getDropdownContentElement());
    }
  }, {
    key: 'getDropdownContentElementHalfWidth',
    value: function getDropdownContentElementHalfWidth() {
      return this.getDropdownContentElementWidth() / 2;
    }
  }, {
    key: 'getContentHorizontalPosition',
    value: function getContentHorizontalPosition() {
      // Compute dropdown position.
      var space = this.getDropdownButtonPositionLeft() - this.getDropdownContentElementHalfWidth();

      // If the interval is negative, just stick the dropdown to the reference
      // element border.

      if (window.innerWidth < space + this.getDropdownContentElementWidth()) {
        return {
          left: window.innerWidth - this.getDropdownContentElementWidth() + 'px'
        };
      } else {
        return { left: space + 'px' };
      }
    }
  }, {
    key: 'getArrowHorizontalPosition',
    value: function getArrowHorizontalPosition() {
      var contentHorizontalPositionValue = parseInt(this.getContentHorizontalPosition().left, 10);

      var space = this.getDropdownButtonPositionLeft() - contentHorizontalPositionValue;

      return { left: space + 'px' };
    }
  }, {
    key: 'getDropdownButton',
    value: function getDropdownButton() {
      var buttonClassName = (0, _classnames2.default)('k-ButtonIcon', 'k-ButtonIcon--hydrogen', 'k-ButtonIcon--phone', this.props.buttonClassName);

      return _react2.default.createElement(
        _dropdownButton.DropdownButton,
        {
          ref: 'dropdownButton',
          className: buttonClassName,
          id: this.props.buttonId,
          isExpanded: this.state.isExpanded,
          onClick: this.handleButtonClick
        },
        _react2.default.createElement(
          'svg',
          {
            className: 'k-ButtonIcon__svg',
            viewBox: '0 0 16 18',
            xmlns: 'http://www.w3.org/2000/svg'
          },
          _react2.default.createElement('path', {
            d: 'M3.354 0C2.238 0 .496.498.207 2.268-.2 4.764 1.533 9.09 4.047 12.202c2.49 3.084 6.305 5.63 8.86 5.792.06.004.12.006.177.006 2.39 0 3.002-3.158 2.694-3.548l-4.335-1.94c-.26-.15-.532-.225-.79-.225-.263 0-.51.08-.713.24l-.866.63c-.093.034-.194.05-.302.05-1.807 0-5.535-4.758-4.636-6.166.006-.008.79-.724.79-.724.387-.313.534-.882.347-1.444L4.275.206C4.18.088 3.823 0 3.355 0'
          })
        )
      );
    }
  }, {
    key: 'getDropdownArrow',
    value: function getDropdownArrow() {
      return _react2.default.createElement('span', { className: 'k-PhoneDropdown__arrow' });
    }
  }, {
    key: 'updateDropdownContentPosition',
    value: function updateDropdownContentPosition() {
      this.setState({
        contentHorizontalPosition: this.getContentHorizontalPosition()
      });
    }
  }, {
    key: 'updateDropdownArrowPosition',
    value: function updateDropdownArrowPosition() {
      this.setState({
        arrowHorizontalPosition: this.getArrowHorizontalPosition()
      });
    }

    // Component listener callbacks.

  }, {
    key: 'handlePositionUpdate',
    value: function handlePositionUpdate() {
      if (this.canComputeSize()) {
        this.updateDropdownContentPosition();
        this.updateDropdownArrowPosition();
      }
    }
  }, {
    key: 'handleButtonClick',
    value: function handleButtonClick(event) {
      event.stopPropagation();
      event.preventDefault();

      this.setState({ isExpanded: !this.state.isExpanded });
    }
  }, {
    key: 'handleOtherDropdownsOpening',
    value: function handleOtherDropdownsOpening(openedDropdown) {
      if (openedDropdown != this.refs.dropdown) {
        this.setState({ isExpanded: false });
      }
    }

    // Rendering.

  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          dropdownList = _props.dropdownList,
          otherProps = _objectWithoutProperties(_props, ['dropdownList']);

      return _react2.default.createElement(_dropdown.Dropdown, _extends({
        ref: 'dropdown',
        button: this.getDropdownButton(),
        dropdownContent: this.props.dropdownContent,
        dropdownListArrow: this.getDropdownArrow(),
        contentHorizontalPosition: this.state.contentHorizontalPosition,
        arrowHorizontalPosition: this.state.arrowHorizontalPosition,
        isExpanded: this.state.isExpanded,
        buttonId: this.getButtonId(),
        onPositionUpdate: this.handlePositionUpdate
      }, otherProps));
    }
  }]);

  return PhoneDropdown;
}(_react2.default.Component);

PhoneDropdown.propTypes = {
  buttonClassName: _propTypes2.default.string,
  positionedWith: _propTypes2.default.func,
  positionedWithBorder: _propTypes2.default.bool,
  positionedOn: _propTypes2.default.string,
  refreshEvents: _propTypes2.default.array,
  closeEvents: _propTypes2.default.array,
  onPositionUpdate: _propTypes2.default.func
};

PhoneDropdown.defaultProps = {
  // Position.
  positionedWithBorder: false,

  // Button classes.
  buttonClassName: '',

  // Add custom value to compute horizontal position.
  spaceAroundGrid: 0,

  // Notifications count on badge.
  buttonNotifications: 0

  // DEPRECATED: do not use default export.
};exports.default = PhoneDropdown;