'use strict';

exports.__esModule = true;

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

/**
 * IsVisible is component for determining if single child is visible on screen
 * @param props
 * @returns {XML}
 * @constructor
 */

var IsVisible = function (_React$Component) {
  (0, _inherits3.default)(IsVisible, _React$Component);

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

    (0, _classCallCheck3.default)(this, IsVisible);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = { visibleClass: '' }, _this.getChildContext = function () {
      return {
        visibleClass: _this.state.visibleClass
      };
    }, _this.isVisible = function (element) {
      var tempParentArr = [];
      var elementBoundingRect = element.getBoundingClientRect();
      var elementParent = element.parentElement;
      var windowBottom = window.pageXOffset + window.innerHeight;
      var elementTop = elementBoundingRect.top - 36 - elementBoundingRect.height;
      var elementBottom = elementBoundingRect.bottom;

      var findParents = function findParents(elem) {
        return !elem.parentElement ? tempParentArr : findParents(elem.parentElement, tempParentArr.push(elem));
      };

      var elementParents = findParents(elementParent);

      var findOverflow = function findOverflow(node) {
        var searchProps = ['overflow', 'overflow-y'];

        return searchProps.reduce(function (agg, prop) {
          var overflowElement = _reactDom2.default.findDOMNode(node).style[prop];

          return !overflowElement || agg.includes(overflowElement) ? agg : agg += overflowElement;
        }, '');
      };

      var findScrollParent = function findScrollParent() {
        var overflowElement = null;
        var idx = 0;

        while (!overflowElement && elementParents[idx]) {
          if (/(auto|scroll)/.test(findOverflow(elementParents[idx]))) {
            return overflowElement = elementParents[idx];
          }
          idx++;
        }

        return overflowElement ? overflowElement : window;
      };

      var scrollParent = findScrollParent(element);
      var parentBottom = !!scrollParent.getBoundingClientRect && scrollParent.getBoundingClientRect().bottom || windowBottom;
      var parentTop = !!scrollParent.getBoundingClientRect && scrollParent.getBoundingClientRect().top || 0;

      return elementBottom < windowBottom && elementBottom < parentBottom || elementTop < 0 && elementTop < parentTop;
    }, _this.chooseClass = function (isOpen) {
      return isOpen && _this.isVisible(_reactDom2.default.findDOMNode(_this.dropdown)) ? _this.setState({
        visibleClass: 'bottom'
      }) : _this.setState({ visibleClass: 'top' });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  IsVisible.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
    prevProps !== this.props && this.chooseClass(this.props.isOpen);
  };

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

    var children = this.props.children;


    return _react2.default.cloneElement(children, {
      ref: function ref(_ref) {
        return _this2.dropdown = _ref;
      }
    });
  };

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

IsVisible.displayName = 'IsVisible';
exports.default = IsVisible;


IsVisible.childContextTypes = {
  /** Class to be applied to child element. */
  visibleClass: _propTypes2.default.string
};

module.exports = exports['default'];