var _excluded = ["children"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React, { useState, useRef, useEffect } from 'react';
import cls from 'classnames';
import { usePrefixCls, pickDataProps, QuestionCircleOutlinedIcon, CloseCircleOutlinedIcon, CheckCircleOutlinedIcon, ExclamationCircleOutlinedIcon } from '../__builtins__';
import { isVoidField } from '@formily/core';
import { connect, mapProps } from '@formily/react';
import { useFormLayout, FormLayoutShallowContext } from '../form-layout';
import { Balloon } from '@alicloudfe/components';
var useFormItemLayout = function useFormItemLayout(props) {
  var _ref, _props$layout, _props$colon, _props$labelAlign, _ref2, _props$labelAlign2, _props$labelWrap, _props$labelWidth, _props$wrapperWidth, _props$labelCol, _props$wrapperCol, _props$wrapperAlign, _props$wrapperWrap, _props$fullness, _props$size, _props$inset, _props$bordered, _ref3, _props$feedbackLayout, _ref4, _props$tooltipLayout, _ref5, _props$tooltipIcon;
  var layout = useFormLayout();
  var layoutType = (_ref = (_props$layout = props.layout) !== null && _props$layout !== void 0 ? _props$layout : layout.layout) !== null && _ref !== void 0 ? _ref : 'horizontal';
  return _objectSpread(_objectSpread({}, props), {}, {
    layout: layoutType,
    colon: (_props$colon = props.colon) !== null && _props$colon !== void 0 ? _props$colon : layout.colon,
    labelAlign: layoutType === 'vertical' ? (_props$labelAlign = props.labelAlign) !== null && _props$labelAlign !== void 0 ? _props$labelAlign : 'left' : (_ref2 = (_props$labelAlign2 = props.labelAlign) !== null && _props$labelAlign2 !== void 0 ? _props$labelAlign2 : layout.labelAlign) !== null && _ref2 !== void 0 ? _ref2 : 'right',
    labelWrap: (_props$labelWrap = props.labelWrap) !== null && _props$labelWrap !== void 0 ? _props$labelWrap : layout.labelWrap,
    labelWidth: (_props$labelWidth = props.labelWidth) !== null && _props$labelWidth !== void 0 ? _props$labelWidth : layout.labelWidth,
    wrapperWidth: (_props$wrapperWidth = props.wrapperWidth) !== null && _props$wrapperWidth !== void 0 ? _props$wrapperWidth : layout.wrapperWidth,
    labelCol: (_props$labelCol = props.labelCol) !== null && _props$labelCol !== void 0 ? _props$labelCol : layout.labelCol,
    wrapperCol: (_props$wrapperCol = props.wrapperCol) !== null && _props$wrapperCol !== void 0 ? _props$wrapperCol : layout.wrapperCol,
    wrapperAlign: (_props$wrapperAlign = props.wrapperAlign) !== null && _props$wrapperAlign !== void 0 ? _props$wrapperAlign : layout.wrapperAlign,
    wrapperWrap: (_props$wrapperWrap = props.wrapperWrap) !== null && _props$wrapperWrap !== void 0 ? _props$wrapperWrap : layout.wrapperWrap,
    fullness: (_props$fullness = props.fullness) !== null && _props$fullness !== void 0 ? _props$fullness : layout.fullness,
    size: (_props$size = props.size) !== null && _props$size !== void 0 ? _props$size : layout.size,
    inset: (_props$inset = props.inset) !== null && _props$inset !== void 0 ? _props$inset : layout.inset,
    asterisk: props.asterisk,
    bordered: (_props$bordered = props.bordered) !== null && _props$bordered !== void 0 ? _props$bordered : layout.bordered,
    feedbackIcon: props.feedbackIcon,
    feedbackLayout: (_ref3 = (_props$feedbackLayout = props.feedbackLayout) !== null && _props$feedbackLayout !== void 0 ? _props$feedbackLayout : layout.feedbackLayout) !== null && _ref3 !== void 0 ? _ref3 : 'loose',
    tooltipLayout: (_ref4 = (_props$tooltipLayout = props.tooltipLayout) !== null && _props$tooltipLayout !== void 0 ? _props$tooltipLayout : layout.tooltipLayout) !== null && _ref4 !== void 0 ? _ref4 : 'icon',
    tooltipIcon: (_ref5 = (_props$tooltipIcon = props.tooltipIcon) !== null && _props$tooltipIcon !== void 0 ? _props$tooltipIcon : layout.tooltipIcon) !== null && _ref5 !== void 0 ? _ref5 : /*#__PURE__*/React.createElement(QuestionCircleOutlinedIcon, null)
  });
};
function useOverflow() {
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    overflow = _useState2[0],
    setOverflow = _useState2[1];
  var containerRef = useRef();
  var contentRef = useRef();
  var layout = useFormLayout();
  var labelCol = JSON.stringify(layout.labelCol);
  useEffect(function () {
    requestAnimationFrame(function () {
      if (containerRef.current && contentRef.current) {
        var contentWidth = contentRef.current.getBoundingClientRect().width;
        var containerWidth = containerRef.current.getBoundingClientRect().width;
        if (contentWidth && containerWidth && containerWidth < contentWidth) {
          if (!overflow) setOverflow(true);
        } else {
          if (overflow) setOverflow(false);
        }
      }
    });
  }, [labelCol]);
  return {
    overflow: overflow,
    containerRef: containerRef,
    contentRef: contentRef
  };
}
var ICON_MAP = {
  error: /*#__PURE__*/React.createElement(CloseCircleOutlinedIcon, null),
  success: /*#__PURE__*/React.createElement(CheckCircleOutlinedIcon, null),
  warning: /*#__PURE__*/React.createElement(ExclamationCircleOutlinedIcon, null)
};
export var BaseItem = function BaseItem(props) {
  var _cls, _cls3, _cls4, _cls5, _cls6;
  var children = props.children,
    others = _objectWithoutProperties(props, _excluded);
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    active = _useState4[0],
    setActive = _useState4[1];
  var formLayout = useFormItemLayout(others);
  var _useOverflow = useOverflow(),
    containerRef = _useOverflow.containerRef,
    contentRef = _useOverflow.contentRef,
    overflow = _useOverflow.overflow;
  var label = formLayout.label,
    style = formLayout.style,
    layout = formLayout.layout,
    _formLayout$colon = formLayout.colon,
    colon = _formLayout$colon === void 0 ? true : _formLayout$colon,
    addonBefore = formLayout.addonBefore,
    addonAfter = formLayout.addonAfter,
    asterisk = formLayout.asterisk,
    feedbackStatus = formLayout.feedbackStatus,
    extra = formLayout.extra,
    feedbackText = formLayout.feedbackText,
    fullness = formLayout.fullness,
    feedbackLayout = formLayout.feedbackLayout,
    feedbackIcon = formLayout.feedbackIcon,
    inset = formLayout.inset,
    _formLayout$bordered = formLayout.bordered,
    bordered = _formLayout$bordered === void 0 ? true : _formLayout$bordered,
    labelWidth = formLayout.labelWidth,
    wrapperWidth = formLayout.wrapperWidth,
    labelCol = formLayout.labelCol,
    wrapperCol = formLayout.wrapperCol,
    labelAlign = formLayout.labelAlign,
    _formLayout$wrapperAl = formLayout.wrapperAlign,
    wrapperAlign = _formLayout$wrapperAl === void 0 ? 'left' : _formLayout$wrapperAl,
    size = formLayout.size,
    labelWrap = formLayout.labelWrap,
    wrapperWrap = formLayout.wrapperWrap,
    tooltip = formLayout.tooltip,
    tooltipLayout = formLayout.tooltipLayout,
    tooltipIcon = formLayout.tooltipIcon;
  var labelStyle = _objectSpread({}, formLayout.labelStyle);
  var wrapperStyle = _objectSpread({}, formLayout.wrapperStyle);
  // 固定宽度
  var enableCol = false;
  if (labelWidth || wrapperWidth) {
    if (labelWidth) {
      labelStyle.width = labelWidth === 'auto' ? undefined : labelWidth;
      labelStyle.maxWidth = labelWidth === 'auto' ? undefined : labelWidth;
    }
    if (wrapperWidth) {
      wrapperStyle.width = wrapperWidth === 'auto' ? undefined : wrapperWidth;
      wrapperStyle.maxWidth = wrapperWidth === 'auto' ? undefined : wrapperWidth;
    }
    // 栅格模式
  }

  if (labelCol || wrapperCol) {
    if (!labelStyle.width && !wrapperStyle.width && layout !== 'vertical') {
      enableCol = true;
    }
  }
  var prefixCls = usePrefixCls('formily-item', props);
  var prefix = usePrefixCls();
  var formatChildren = (feedbackLayout === 'popover' || feedbackLayout === 'hover-popover') && !!feedbackText ? /*#__PURE__*/React.createElement(Balloon, _objectSpread({
    needAdjust: true,
    align: "t",
    closable: false,
    trigger: children
  }, feedbackLayout === 'popover' ? {
    visible: !!feedbackText
  } : {}), /*#__PURE__*/React.createElement("div", {
    className: cls((_cls = {}, _defineProperty(_cls, "".concat(prefixCls, "-").concat(feedbackStatus, "-help"), !!feedbackStatus), _defineProperty(_cls, "".concat(prefixCls, "-help"), true), _cls))
  }, ICON_MAP[feedbackStatus !== null && feedbackStatus !== void 0 ? feedbackStatus : ''], " ", feedbackText)) : children;
  var gridStyles = {};
  var getOverflowTooltip = function getOverflowTooltip() {
    if (overflow) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", null, label), /*#__PURE__*/React.createElement("div", null, tooltip));
    }
    return tooltip;
  };
  var renderLabelText = function renderLabelText() {
    var labelChildren = /*#__PURE__*/React.createElement("div", {
      className: cls("".concat(prefixCls, "-label-content")),
      ref: containerRef
    }, /*#__PURE__*/React.createElement("span", {
      ref: contentRef
    }, asterisk && /*#__PURE__*/React.createElement("span", {
      className: cls("".concat(prefixCls, "-asterisk"))
    }, '*'), /*#__PURE__*/React.createElement("label", null, label)));
    if (tooltipLayout === 'text' && tooltip || overflow) {
      return /*#__PURE__*/React.createElement(Balloon.Tooltip, {
        align: "t",
        trigger: labelChildren
      }, getOverflowTooltip());
    }
    return labelChildren;
  };
  var renderTooltipIcon = function renderTooltipIcon() {
    if (tooltip && tooltipLayout === 'icon' && !overflow) {
      return /*#__PURE__*/React.createElement("span", {
        className: cls("".concat(prefixCls, "-label-tooltip-icon"))
      }, /*#__PURE__*/React.createElement(Balloon.Tooltip, {
        align: "t",
        trigger: tooltipIcon
      }, tooltip));
    }
  };
  var renderLabel = function renderLabel() {
    var _cls2;
    if (!label) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: cls((_cls2 = {}, _defineProperty(_cls2, "".concat(prefixCls, "-label"), true), _defineProperty(_cls2, "".concat(prefixCls, "-label-tooltip"), tooltip && tooltipLayout === 'text' || overflow), _defineProperty(_cls2, "".concat(prefixCls, "-item-col-").concat(labelCol), enableCol && !!labelCol), _cls2)),
      style: labelStyle
    }, renderLabelText(), renderTooltipIcon(), asterisk && /*#__PURE__*/React.createElement("span", {
      className: cls("".concat(prefixCls, "-asterisk-pro"))
    }), label !== ' ' && /*#__PURE__*/React.createElement("span", {
      className: cls("".concat(prefixCls, "-colon"))
    }, colon ? ':' : ''));
  };
  return /*#__PURE__*/React.createElement("div", _objectSpread(_objectSpread({}, pickDataProps(props)), {}, {
    style: _objectSpread(_objectSpread({}, style), gridStyles),
    "data-grid-span": props.gridSpan,
    className: cls((_cls3 = {}, _defineProperty(_cls3, "".concat(prefixCls), true), _defineProperty(_cls3, "".concat(prefixCls, "-layout-").concat(layout), true), _defineProperty(_cls3, "".concat(prefixCls, "-").concat(feedbackStatus), !!feedbackStatus), _defineProperty(_cls3, "".concat(prefixCls, "-feedback-has-text"), !!feedbackText), _defineProperty(_cls3, "".concat(prefixCls, "-size-").concat(size), !!size), _defineProperty(_cls3, "".concat(prefixCls, "-feedback-layout-").concat(feedbackLayout), !!feedbackLayout), _defineProperty(_cls3, "".concat(prefixCls, "-fullness"), !!fullness || !!inset || !!feedbackIcon), _defineProperty(_cls3, "".concat(prefixCls, "-inset"), !!inset), _defineProperty(_cls3, "".concat(prefix, "input"), !!inset), _defineProperty(_cls3, "".concat(prefixCls, "-active"), active), _defineProperty(_cls3, "".concat(prefix, "focus"), active), _defineProperty(_cls3, "".concat(prefixCls, "-extra-wrap"), extra), _defineProperty(_cls3, "".concat(prefixCls, "-inset-active"), !!inset && active), _defineProperty(_cls3, "".concat(prefixCls, "-label-align-").concat(labelAlign), true), _defineProperty(_cls3, "".concat(prefixCls, "-control-align-").concat(wrapperAlign), true), _defineProperty(_cls3, "".concat(prefixCls, "-label-wrap"), !!labelWrap), _defineProperty(_cls3, "".concat(prefixCls, "-control-wrap"), !!wrapperWrap), _defineProperty(_cls3, "".concat(prefixCls, "-bordered-none"), bordered === false), _defineProperty(_cls3, props === null || props === void 0 ? void 0 : props.className, !!props.className), _cls3)),
    onFocus: function onFocus() {
      if (feedbackIcon || inset) {
        setActive(true);
      }
    },
    onBlur: function onBlur() {
      if (feedbackIcon || inset) {
        setActive(false);
      }
    }
  }), renderLabel(), /*#__PURE__*/React.createElement("div", {
    className: cls((_cls4 = {}, _defineProperty(_cls4, "".concat(prefixCls, "-control"), true), _defineProperty(_cls4, "".concat(prefixCls, "-item-col-").concat(wrapperCol), enableCol && !!wrapperCol && label), _cls4))
  }, /*#__PURE__*/React.createElement("div", {
    className: cls("".concat(prefixCls, "-control-content"))
  }, addonBefore && /*#__PURE__*/React.createElement("div", {
    className: cls("".concat(prefixCls, "-addon-before"))
  }, addonBefore), /*#__PURE__*/React.createElement("div", {
    style: wrapperStyle,
    className: cls((_cls5 = {}, _defineProperty(_cls5, "".concat(prefixCls, "-control-content-component"), true), _defineProperty(_cls5, "".concat(prefixCls, "-control-content-component-has-feedback-icon"), !!feedbackIcon), _defineProperty(_cls5, "".concat(prefix, "input"), !!feedbackIcon), _defineProperty(_cls5, "".concat(prefixCls, "-active"), active), _defineProperty(_cls5, "".concat(prefix, "focus"), active), _cls5))
  }, /*#__PURE__*/React.createElement(FormLayoutShallowContext.Provider, {
    value: {
      size: size
    }
  }, formatChildren), feedbackIcon && /*#__PURE__*/React.createElement("div", {
    className: cls("".concat(prefixCls, "-feedback-icon"))
  }, feedbackIcon)), addonAfter && /*#__PURE__*/React.createElement("div", {
    className: cls("".concat(prefixCls, "-addon-after"))
  }, addonAfter)), !!feedbackText && feedbackLayout !== 'popover' && feedbackLayout !== 'hover-popover' && feedbackLayout !== 'none' && /*#__PURE__*/React.createElement("div", {
    className: cls((_cls6 = {}, _defineProperty(_cls6, "".concat(prefixCls, "-").concat(feedbackStatus, "-help"), !!feedbackStatus), _defineProperty(_cls6, "".concat(prefixCls, "-help"), true), _defineProperty(_cls6, "".concat(prefixCls, "-help-enter"), true), _defineProperty(_cls6, "".concat(prefixCls, "-help-enter-active"), true), _cls6))
  }, feedbackText), extra && /*#__PURE__*/React.createElement("div", {
    className: cls("".concat(prefixCls, "-extra"))
  }, extra)));
};
// 适配
export var FormItem2 = connect(BaseItem, mapProps(function (props, field) {
  if (isVoidField(field)) return {
    label: field.title || props.label,
    asterisk: props.asterisk,
    extra: props.extra || field.description
  };
  if (!field) return props;
  var takeFeedbackStatus = function takeFeedbackStatus() {
    if (field.validating) return 'pending';
    return field.decoratorProps.feedbackStatus || field.validateStatus;
  };
  var takeMessage = function takeMessage() {
    var split = function split(messages) {
      return messages.reduce(function (buf, text, index) {
        if (!text) return buf;
        return index < messages.length - 1 ? buf.concat([text, ', ']) : buf.concat([text]);
      }, []);
    };
    if (field.validating) return;
    if (props.feedbackText) return props.feedbackText;
    if (field.selfErrors.length) return split(field.selfErrors);
    if (field.selfWarnings.length) return split(field.selfWarnings);
    if (field.selfSuccesses.length) return split(field.selfSuccesses);
  };
  var takeAsterisk = function takeAsterisk() {
    if (field.required && field.pattern !== 'readPretty') {
      return true;
    }
    if ('asterisk' in props) {
      return props.asterisk;
    }
    return false;
  };
  return {
    label: props.label || field.title,
    feedbackStatus: takeFeedbackStatus(),
    feedbackText: takeMessage(),
    asterisk: takeAsterisk(),
    extra: props.extra || field.description
  };
}));
FormItem2.defaultProps = {
  fullness: true
};
FormItem2.BaseItem = BaseItem;
export default FormItem2;