import React, { forwardRef } from 'react';
import { CnBox, CnCard as UICnCard } from '@cainiaofe/cn-ui-m';
import { isArrayNotEmpty, isDesignMode, makeButtons } from '@/common/util/util';
import { ButtonPosition } from '@/type/button-position';

const CnCard = forwardRef((props, ref) => {
  if (!UICnCard) {
    return null;
  }
  const { _context, buttons, action, ...rest } = props;
  const isDesign = isDesignMode(props);
  const state = _context?.state;

  const extraProps = {
    ...rest,
  };

  const Action = makeButtons({
    buttons: action?.map((item) => {
      let realChildren = item?.children;
      return {
        ...item,
        // size:'small',
        type: 'primary',
        text: true,
        position: ButtonPosition.normalCardAction,
        children: realChildren,
      };
    }),
    _context,
    state,
    recordDataSource: {},
  });

  if (isArrayNotEmpty(Action)) {
    extraProps.action = (
      <CnBox spacing={8} direction='row'>
        {Action}
      </CnBox>
    );
  }

  const subAction = makeButtons({
    buttons: buttons?.map((item) => {
      let realChildren = item?.children;
      return {
        ...item,
        // size:'small',
        type: 'primary',
        text: true,
        position: ButtonPosition.normalCardSubAction,
        children: realChildren,
      };
    }),
    _context,
    state,
    recordDataSource: {},
  });

  if (isArrayNotEmpty(subAction)) {
    extraProps.subAction = (
      <CnBox spacing={8} direction='row'>
        {subAction}
      </CnBox>
    );
  }

  return <UICnCard {...extraProps} />;
});
CnCard.displayName = 'CnCard';
export default CnCard;
