import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.promise.js";
import React, { useMemo, useContext } from 'react';
import { RightOutlined } from '@wii/icons';
import useMergedState from 'rc-util/es/hooks/useMergedState';
import classNames from 'classnames';
import omit from 'omit.js';
import ConfigProvider from '../config-provider';
import Tabs from '../tabs';
import Grid from '../grid';
import LabelIconTip from '../wau-label-icon-tip';
import CardLoading from './card-loading';
import Actions from './actions';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const {
  useBreakpoint
} = Grid;
// @ts-ignore
const ProCard = /*#__PURE__*/React.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
  } = useContext(ConfigProvider.ConfigContext);
  const screens = useBreakpoint();
  const [collapsed, setCollapsed] = useMergedState(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.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.cloneElement(element, {
        className: classNames(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 = classNames(`${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 = classNames(`${prefixCls}-header`, {
    [`${prefixCls}-header-border`]: headerBordered || type === 'inner'
  });
  const bodyCls = classNames(`${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.isValidElement(loading) ? loading : /*#__PURE__*/_jsx(CardLoading, {
    prefix: prefixCls,
    style: loadingBlockStyle
  });

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

  /** 操作按钮 */
  const actionDom = /*#__PURE__*/_jsx(Actions, {
    actions: actions,
    prefixCls: prefixCls
  });
  const computedTabs = useMemo(() => {
    if (tabs) {
      const {
        items = []
      } = tabs;
      return {
        ...tabs,
        items: items.length ? items.map(item => {
          const {
            className,
            children,
            cardProps,
            ...rest
          } = item;
          const prefixCls = getPrefixCls('pro-card-tabpane');
          const tabPaneClassName = classNames(prefixCls, className);
          return {
            ...rest,
            className: tabPaneClassName,
            children: /*#__PURE__*/_jsx(ProCard, {
              ...cardProps,
              children: children
            })
          };
        }) : undefined
      };
    }
    return tabs;
  }, [getPrefixCls, tabs]);
  return /*#__PURE__*/_jsxs("div", {
    className: cardCls,
    style: cardStyle,
    ref: ref,
    ...omit(rest, ['id', 'prefixCls']),
    children: [(title || extra || collapsibleButton) && /*#__PURE__*/_jsxs("div", {
      className: headerCls,
      style: headStyle,
      children: [/*#__PURE__*/_jsxs("div", {
        className: titleCls,
        onClick: () => {
          if (collapsibleButton) setCollapsed(!collapsed);
        },
        children: [collapsibleButton, /*#__PURE__*/_jsx(LabelIconTip, {
          label: title,
          tooltip: tooltip || tip,
          subTitle: subTitle
        })]
      }), /*#__PURE__*/_jsx("div", {
        className: `${prefixCls}-extra`,
        children: extra
      })]
    }), computedTabs ? /*#__PURE__*/_jsx("div", {
      className: `${prefixCls}-tabs`,
      children: /*#__PURE__*/_jsx(Tabs, {
        onChange: computedTabs.onChange,
        ...computedTabs,
        children: loading ? loadingDOM : children
      })
    }) : /*#__PURE__*/_jsx("div", {
      className: bodyCls,
      style: bodyStyle,
      children: loading ? loadingDOM : childrenModified
    }), actionDom]
  });
});
export default ProCard;