"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _react = _interopRequireWildcard(require("react"));

var _ConfigContext = _interopRequireDefault(require("../../../lib/config-provider/ConfigContext"));

var _mobx = require("mobx");

var _enum = require("../core/enum");

var _util = _interopRequireDefault(require("../overflow-tip/util"));

var _singleton = require("../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 = (0, _react.useContext)(_ConfigContext["default"]),
      getTooltipTheme = _useContext.getTooltipTheme,
      getTooltipPlacement = _useContext.getTooltipPlacement;

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

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

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

FormItemLabel.displayName = 'FormItemLabel';

var _default = /*#__PURE__*/(0, _react.memo)(FormItemLabel);

exports["default"] = _default;
//# sourceMappingURL=FormItemLabel.js.map
