import {
  CaretDownOutlined,
  CaretRightOutlined,
  MinusSquareOutlined,
  PlusSquareOutlined,
} from '@ant-design/icons';
import Link from 'antd/lib/typography/Link';
import type { CSSProperties } from 'react';

export interface ExpandIconProps<T = any> {
  expanded: boolean;
  onExpand: (record: T, e: React.MouseEvent<HTMLElement>) => void;
  record: T;
  style?: CSSProperties;
  /** 使用高级图标 */
  useSenior?: boolean;
  /** 图标是否受数据控制 */
  dataControl?: boolean;
}

const ExpandIcon = (props: ExpandIconProps) => {
  const { expanded, onExpand, record, style, useSenior = false, dataControl = true } = props;
  if (dataControl && (!record.children || record.children.length === 0)) {
    return null; // 如果没有数据，返回 null 不渲染展开图标
  }

  const expandIcon = useSenior ? <MinusSquareOutlined /> : <CaretDownOutlined />;
  const foldIcon = useSenior ? <PlusSquareOutlined /> : <CaretRightOutlined />;

  return (
    <Link style={style} onClick={(e) => onExpand(record, e)}>
      {expanded ? expandIcon : foldIcon}
    </Link>
  );
};

export default ExpandIcon;
