"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.promise.js");
var _react = _interopRequireWildcard(require("react"));
var _icons = require("@wii/icons");
var _useMergedState = _interopRequireDefault(require("rc-util/es/hooks/useMergedState"));
var _classnames = _interopRequireDefault(require("classnames"));
var _omit = _interopRequireDefault(require("omit.js"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _tabs = _interopRequireDefault(require("../tabs"));
var _grid = _interopRequireDefault(require("../grid"));
var _wauLabelIconTip = _interopRequireDefault(require("../wau-label-icon-tip"));
var _cardLoading = _interopRequireDefault(require("./card-loading"));
var _actions = _interopRequireDefault(require("./actions"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const {
  useBreakpoint
} = _grid.default;
// @ts-ignore
const ProCard = /*#__PURE__*/_react.default.forwardRef((props, ref) => {
  const {
    className,
    style,
    bodyStyle = {},
    headStyle = {},
    title,
    subTitle,
    extra,
    tip,
    layout,
    loading,
    colSpan,
    gutter = 0,
    tooltip,
    split,
    headerBordered = false,
    bordered = false,
    children,
    size,
    actions,
    ghost = false,
    hoverable = false,
    direction,
    collapsed: controlCollapsed,
    collapsible = false,
    defaultCollapsed = false,
    onCollapse,
    tabs,
    type,
    ...rest
  } = props;
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const screens = useBreakpoint();
  const [collapsed, setCollapsed] = (0, _useMergedState.default)(defaultCollapsed, {
    value: controlCollapsed,
    onChange: onCollapse
  });

  // 顺序决定如何进行响应式取值，按最大响应值依次取值，请勿修改。
  const responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];

  /**
   * 根据响应式获取 gutter, 参考 antd 实现
   *
   * @param gutter Gutter
   */
  const getNormalizedGutter = gut => {
    const results = [0, 0];
    const normalizedGutter = Array.isArray(gut) ? gut : [gut, 0];
    normalizedGutter.forEach((g, index) => {
      if (typeof g === 'object') {
        for (let i = 0; i < responsiveArray.length; i += 1) {
          const breakpoint = responsiveArray[i];
          if (screens[breakpoint] && g[breakpoint] !== undefined) {
            results[index] = g[breakpoint];
            break;
          }
        }
      } else {
        results[index] = g || 0;
      }
    });
    return results;
  };

  /**
   * 根据条件返回 style，负责返回空对象
   *
   * @param withStyle 是否符合条件
   * @param appendStyle 如果符合条件要返回的 style 属性
   */
  const getStyle = (withStyle, appendStyle) => {
    return withStyle ? appendStyle : {};
  };
  const prefixCls = getPrefixCls('pro-card');
  const normalizedGutter = getNormalizedGutter(gutter);

  // 判断是否套了卡片，如果套了的话将自身卡片内部内容的 padding 设置为0
  let containProCard;
  const childrenArray = _react.default.Children.toArray(children);
  const childrenModified = childrenArray.map((element, index) => {
    var _element$type;
    if (element !== null && element !== void 0 && (_element$type = element.type) !== null && _element$type !== void 0 && _element$type.isProCard) {
      containProCard = true;

      // 右侧空隙
      const gutterRightStyle = getStyle(normalizedGutter[0] > 0 && index !== childrenArray.length - 1, {
        marginRight: normalizedGutter[0]
      });

      // 下方空隙
      const gutterBottomStyle = getStyle(normalizedGutter[1] > 0, {
        marginBottom: normalizedGutter[1]
      });

      // 当 split 有值时，内部卡片 radius 设置为 0
      const splitStyle = getStyle(split === 'vertical' || split === 'horizontal', {
        borderRadius: 0
      });
      return /*#__PURE__*/_react.default.cloneElement(element, {
        className: (0, _classnames.default)(element.props.className, {
          // 横纵切割
          [`${prefixCls}-split-vertical`]: split === 'vertical' && index !== childrenArray.length - 1,
          [`${prefixCls}-split-horizontal`]: split === 'horizontal' && index !== childrenArray.length - 1
        }),
        style: {
          ...gutterRightStyle,
          ...gutterBottomStyle,
          ...splitStyle,
          ...element.props.style
        }
      });
    }
    return element;
  });
  let span = colSpan;

  // colSpan 响应式
  if (typeof colSpan === 'object') {
    for (let i = 0; i < responsiveArray.length; i += 1) {
      const breakpoint = responsiveArray[i];
      if (screens[breakpoint] && colSpan[breakpoint] !== undefined) {
        span = colSpan[breakpoint];
        break;
      }
    }
  }

  // 当 colSpan 为 30% 或 300px 时
  const colSpanStyle = getStyle(typeof span === 'string' && /\d%|\dpx/i.test(span), {
    width: span,
    flexShrink: 0
  });
  const cardStyle = {
    ...colSpanStyle,
    ...style
  };
  const cardCls = (0, _classnames.default)(`${prefixCls}`, className, {
    [`${prefixCls}-span-${span}`]: typeof span === 'number' && span >= 0 && span <= 24,
    [`${prefixCls}-border`]: bordered,
    [`${prefixCls}-contain-card`]: containProCard,
    [`${prefixCls}-loading`]: loading,
    [`${prefixCls}-split`]: split === 'vertical' || split === 'horizontal',
    [`${prefixCls}-ghost`]: ghost,
    [`${prefixCls}-hoverable`]: hoverable,
    [`${prefixCls}-size-${size}`]: size,
    [`${prefixCls}-type-${type}`]: type,
    [`${prefixCls}-collapse`]: collapsed
  });
  const headerCls = (0, _classnames.default)(`${prefixCls}-header`, {
    [`${prefixCls}-header-border`]: headerBordered || type === 'inner'
  });
  const bodyCls = (0, _classnames.default)(`${prefixCls}-body`, {
    [`${prefixCls}-body-center`]: layout === 'center',
    [`${prefixCls}-body-column`]: split === 'horizontal' || direction === 'column'
  });
  const loadingBlockStyle = bodyStyle.padding === 0 || bodyStyle.padding === '0px' ? {
    padding: 24
  } : undefined;
  const loadingDOM = /*#__PURE__*/_react.default.isValidElement(loading) ? loading : /*#__PURE__*/(0, _jsxRuntime.jsx)(_cardLoading.default, {
    prefix: prefixCls,
    style: loadingBlockStyle
  });

  // 非受控情况下展示
  const collapsibleButton = collapsible && controlCollapsed === undefined && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.RightOutlined, {
    rotate: !collapsed ? 90 : undefined,
    className: `${prefixCls}-collapsible-icon`
  });
  const titleCls = (0, _classnames.default)(`${prefixCls}-title`, {
    [`${prefixCls}-title-collapsible`]: collapsibleButton
  });

  /** 操作按钮 */
  const actionDom = /*#__PURE__*/(0, _jsxRuntime.jsx)(_actions.default, {
    actions: actions,
    prefixCls: prefixCls
  });
  const computedTabs = (0, _react.useMemo)(() => {
    if (tabs) {
      const {
        items = []
      } = tabs;
      return {
        ...tabs,
        items: items.map(item => {
          const {
            className,
            children,
            cardProps,
            ...rest
          } = item;
          const prefixCls = getPrefixCls('pro-card-tabpane');
          const tabPaneClassName = (0, _classnames.default)(prefixCls, className);
          return {
            ...rest,
            className: tabPaneClassName,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ProCard, {
              ...cardProps,
              children: children
            })
          };
        })
      };
    }
    return tabs;
  }, [getPrefixCls, tabs]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: cardCls,
    style: cardStyle,
    ref: ref,
    ...(0, _omit.default)(rest, ['id', 'prefixCls']),
    children: [(title || extra || collapsibleButton) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: headerCls,
      style: headStyle,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: titleCls,
        onClick: () => {
          if (collapsibleButton) setCollapsed(!collapsed);
        },
        children: [collapsibleButton, /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauLabelIconTip.default, {
          label: title,
          tooltip: tooltip || tip,
          subTitle: subTitle
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${prefixCls}-extra`,
        children: extra
      })]
    }), computedTabs ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${prefixCls}-tabs`,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_tabs.default, {
        onChange: computedTabs.onChange,
        ...computedTabs,
        children: loading ? loadingDOM : children
      })
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: bodyCls,
      style: bodyStyle,
      children: loading ? loadingDOM : childrenModified
    }), actionDom]
  });
});
var _default = ProCard;
exports.default = _default;