import React, { useEffect, useState, ReactNode } from 'react';
import Modal, { ModalProps } from 'antd/lib/modal';
import { StarProps } from '../interface';

/**
 * 星级模态框组件，基于Antd Modal的增强组件
 * 提供国际化和组件生命周期管理功能
 */
const StarModal: React.FC<ModalProps & StarProps & { children: ReactNode }> = (props) => {
  const { open, destroyOnClose, children, format: formate, ...rest } = props;
  const [exist, setExist] = useState<boolean | undefined>(open);
  
  useEffect(() => {
    if (open) setExist(open);
  }, [open]);
  
  const elements = exist ? (
    <Modal
      destroyOnClose={destroyOnClose}
      open={open}
      afterClose={() => setExist(false)}
      okText={formate ? formate({ id: 'confirm' }) : '确定'}
      cancelText={formate ? formate({ id: 'cancel' }) : '取消'}
      {...rest}
    >
      {children}
    </Modal>
  ) : (
    <div />
  );

  return elements;
};

export default StarModal;
