"use strict";

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

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

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

var _mobx = require("mobx");

var _classnames = _interopRequireDefault(require("classnames"));

var _mobxReactLite = require("mobx-react-lite");

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

var _FormContext = _interopRequireDefault(require("./FormContext"));

var _utils = require("./utils");

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

var _row = _interopRequireDefault(require("../row"));

var _col = _interopRequireDefault(require("../col"));

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

var _singleton = require("../tooltip/singleton");

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

var _excluded = ["children", "useColon", "requiredMarkAlign"],
    _excluded2 = ["className", "fieldClassName"];

var Label = function Label(props) {
  var children = props.children,
      className = props.className,
      tooltip = props.tooltip,
      width = props.width;

  var _useContext = (0, _react.useContext)(_ConfigContext["default"]),
      getTooltipTheme = _useContext.getTooltipTheme,
      getTooltipPlacement = _useContext.getTooltipPlacement;

  var tooltipRef = (0, _react.useRef)(false);
  var style = (0, _react.useMemo)(function () {
    return width ? {
      width: width
    } : undefined;
  }, [width]);
  var handleMouseEnter = (0, _react.useCallback)(function (e) {
    var currentTarget = e.currentTarget;

    if (tooltip === _enum2.Tooltip.always || tooltip === _enum2.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 === _enum2.Tooltip.always || tooltipType === _enum2.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;
      }
    }
  }, [children, tooltip, 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("label", {
    className: className,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    style: style
  }, children);
};

Label.displayName = 'Label';
var Item = (0, _mobxReactLite.observer)(function (props) {
  var _useContext2 = (0, _react.useContext)(_FormContext["default"]),
      getConfig = _useContext2.getConfig,
      dataSet = _useContext2.dataSet,
      record = _useContext2.record,
      _useContext2$labelLay = _useContext2.labelLayout,
      labelLayout = _useContext2$labelLay === void 0 ? getConfig('labelLayout') : _useContext2$labelLay,
      labelAlign = _useContext2.labelAlign,
      _useContext2$labelWid = _useContext2.labelWidth,
      contextLabelWidth = _useContext2$labelWid === void 0 ? _utils.defaultLabelWidth : _useContext2$labelWid,
      labelTooltip = _useContext2.labelTooltip,
      useColon = _useContext2.useColon,
      requiredMarkAlign = _useContext2.requiredMarkAlign,
      getProPrefixCls = _useContext2.getProPrefixCls;

  var children = props.children,
      _props$useColon = props.useColon,
      fieldUseColon = _props$useColon === void 0 ? useColon : _props$useColon,
      _props$requiredMarkAl = props.requiredMarkAlign,
      fieldRequiredMarkAlign = _props$requiredMarkAl === void 0 ? requiredMarkAlign : _props$requiredMarkAl,
      rest = (0, _objectWithoutProperties2["default"])(props, _excluded);

  var child = _react.Children.only(children);

  if ( /*#__PURE__*/(0, _react.isValidElement)(child)) {
    var _classNames;

    var prefixCls = getProPrefixCls(_utils.FIELD_SUFFIX);
    var childrenProps = child.props;
    var className = childrenProps.className,
        fieldClassName = childrenProps.fieldClassName,
        otherProps = (0, _objectWithoutProperties2["default"])(childrenProps, _excluded2);
    var fieldProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, rest), otherProps);
    var fieldElementProps = (0, _objectSpread2["default"])({
      className: (0, _classnames["default"])(props.className, className, prefixCls)
    }, fieldProps);
    var intlFieldOutput = child.type.displayName === 'IntlField' && fieldProps.displayOutput;

    if ([_enum.LabelLayout.none, _enum.LabelLayout["float"], _enum.LabelLayout.placeholder].includes(labelLayout)) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-wrapper")
      }, /*#__PURE__*/(0, _react.cloneElement)(child, fieldElementProps));
    }

    var label = (0, _utils.getProperty)(fieldProps, 'label', dataSet, record);
    var required = (0, _utils.getPropertyDSFirst)(fieldProps, 'required', dataSet, record);
    var readOnly = (0, _utils.getProperty)(fieldProps, 'readOnly', dataSet, record);
    var isOutput = labelLayout === _enum.LabelLayout.horizontal && (child.type.displayName === 'Output' || intlFieldOutput);
    var labelClassName = (0, _classnames["default"])("".concat(prefixCls, "-label"), "".concat(prefixCls, "-label-").concat(labelAlign), fieldClassName, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-required"), required && !(child.type.displayName === 'Output' || intlFieldOutput)), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-readonly"), readOnly), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-label-vertical"), labelLayout === _enum.LabelLayout.vertical), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-label-output"), isOutput), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-label-useColon"), label && fieldUseColon), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-label-required-mark-").concat((0, _utils.getRequiredMarkAlign)(fieldRequiredMarkAlign)), labelLayout === _enum.LabelLayout.horizontal && required && !(child.type.displayName === 'Output' || intlFieldOutput) && (0, _utils.getRequiredMarkAlign)(fieldRequiredMarkAlign)), _classNames));
    var wrapperClassName = (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-output"), isOutput));
    var tooltip = props.labelTooltip || labelTooltip;

    if (labelLayout === _enum.LabelLayout.vertical) {
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(Label, {
        className: labelClassName,
        tooltip: tooltip
      }, label), /*#__PURE__*/_react["default"].createElement("div", {
        className: wrapperClassName
      }, /*#__PURE__*/(0, _react.cloneElement)(child, fieldElementProps)));
    }

    var fieldLabelWidth = (0, _utils.getProperty)(fieldProps, 'labelWidth', dataSet, record);
    var columnLabelWidth = (0, _utils.normalizeLabelWidth)(contextLabelWidth, 1)[0];
    var labelWidth = columnLabelWidth === 'auto' ? undefined : Math.max(columnLabelWidth, isNaN(fieldLabelWidth) ? 0 : fieldLabelWidth);
    return /*#__PURE__*/_react["default"].createElement(_row["default"], {
      className: "".concat(prefixCls, "-row")
    }, /*#__PURE__*/_react["default"].createElement(_col["default"], {
      className: "".concat(prefixCls, "-col")
    }, /*#__PURE__*/_react["default"].createElement(Label, {
      className: labelClassName,
      width: labelWidth,
      tooltip: tooltip
    }, /*#__PURE__*/_react["default"].createElement("span", null, label))), /*#__PURE__*/_react["default"].createElement(_col["default"], {
      className: "".concat(prefixCls, "-col ").concat(prefixCls, "-col-control")
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: wrapperClassName
    }, /*#__PURE__*/(0, _react.cloneElement)(child, fieldElementProps))));
  }

  if (child) {
    return child;
  }

  return null;
});
Item.displayName = 'FormItem';
Item.__PRO_FORM_ITEM = true;
var _default = Item;
exports["default"] = _default;
//# sourceMappingURL=Item.js.map
