import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "strict", "getOverflowContainer", "onHiddenBeforeChange", "title"];
import React, { Children, cloneElement, useCallback, useRef } from 'react';
import { findDOMNode } from 'react-dom';
import noop from 'lodash/noop';
import Tooltip from '../tooltip/Tooltip';
import utilIsOverflow from './util';

function renderTitle(props) {
  if (props) {
    var trigger = props.trigger;

    if (trigger) {
      return Children.map(trigger, function (item) {
        return /*#__PURE__*/cloneElement(item, {
          ref: null,
          className: null
        });
      });
    }
  }
}

var OverflowTip = function OverflowTip(props) {
  var ref = useRef(null);
  var defaultGetOverflowContainer = useCallback(function () {
    var current = ref.current;

    if (current) {
      return findDOMNode(current);
    }
  }, [ref]);

  var children = props.children,
      strict = props.strict,
      _props$getOverflowCon = props.getOverflowContainer,
      getOverflowContainer = _props$getOverflowCon === void 0 ? defaultGetOverflowContainer : _props$getOverflowCon,
      _props$onHiddenBefore = props.onHiddenBeforeChange,
      onHiddenBeforeChange = _props$onHiddenBefore === void 0 ? noop : _props$onHiddenBefore,
      _props$title = props.title,
      title = _props$title === void 0 ? renderTitle : _props$title,
      rest = _objectWithoutProperties(props, _excluded);

  var isOverFlow = useCallback(function () {
    var element = getOverflowContainer();

    if (element) {
      return utilIsOverflow(element);
    }

    return false;
  }, [getOverflowContainer]);
  var handleHiddenBeforeChange = useCallback(function (hidden) {
    if (onHiddenBeforeChange(hidden) === false) {
      return false;
    }

    if (hidden) {
      return true;
    }

    return isOverFlow();
  }, [isOverFlow, onHiddenBeforeChange]);
  return /*#__PURE__*/React.createElement(Tooltip, _extends({}, rest, {
    title: title,
    onHiddenBeforeChange: strict ? onHiddenBeforeChange : handleHiddenBeforeChange,
    ref: ref
  }), children);
};

OverflowTip.displayName = 'OverflowTip';
export default OverflowTip;
//# sourceMappingURL=index.js.map
