import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import React, { memo, useCallback, useContext, useEffect, useRef } from 'react';
import ConfigContext from '../../../es/config-provider/ConfigContext';
import { isArrayLike } from 'mobx';
import { Tooltip as LabelTooltip } from '../core/enum';
import isOverflow from '../overflow-tip/util';
import { hide, show } from '../tooltip/singleton';

var FormItemLabel = function FormItemLabel(props) {
  var className = props.className,
      rowSpan = props.rowSpan,
      style = props.style,
      tooltip = props.tooltip,
      children = props.children,
      help = props.help;

  var _useContext = useContext(ConfigContext),
      getTooltipTheme = _useContext.getTooltipTheme,
      getTooltipPlacement = _useContext.getTooltipPlacement;

  var tooltipRef = useRef(false);
  var handleMouseEnter = useCallback(function (e) {
    var currentTarget = e.currentTarget;

    if (tooltip === LabelTooltip.always || tooltip === LabelTooltip.overflow && isOverflow(currentTarget)) {
      show(currentTarget, {
        title: children,
        theme: getTooltipTheme('label'),
        placement: getTooltipPlacement('label')
      });
      tooltipRef.current = true;
    } else if (isArrayLike(tooltip)) {
      var tooltipType = tooltip[0];
      var labelTooltipProps = tooltip[1] || {};
      var duration = (labelTooltipProps.mouseEnterDelay || 0.1) * 1000;

      if (tooltipType === LabelTooltip.always || tooltipType === LabelTooltip.overflow && isOverflow(currentTarget)) {
        show(currentTarget, _objectSpread({
          theme: getTooltipTheme('label'),
          placement: getTooltipPlacement('label'),
          title: labelTooltipProps.title ? labelTooltipProps.title : children
        }, labelTooltipProps), duration);
        tooltipRef.current = true;
      }
    }
  }, [tooltip, children, tooltipRef]);
  var handleMouseLeave = useCallback(function () {
    if (tooltipRef.current) {
      hide();
      tooltipRef.current = false;
    }
  }, [tooltipRef]);
  useEffect(function () {
    return function () {
      if (tooltipRef.current) {
        hide();
        tooltipRef.current = false;
      }
    };
  }, [tooltipRef]);
  return /*#__PURE__*/React.createElement("td", {
    className: className,
    rowSpan: rowSpan,
    style: style
  }, /*#__PURE__*/React.createElement("label", null, /*#__PURE__*/React.createElement("span", {
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave
  }, children), help));
};

FormItemLabel.displayName = 'FormItemLabel';
export default /*#__PURE__*/memo(FormItemLabel);
//# sourceMappingURL=FormItemLabel.js.map
