import { FormInstance } from 'antd';
import { useEffect, useRef } from 'react';

/**
 * 用于在 modal 关闭时重置 Form 表单的 hook。
 *
 * @param form - Ant Design Form 表单实例
 * @param open - modal 是否打开
 */
export const useResetForm = (form: FormInstance, open: boolean) => {
  const prevOpenRef = useRef<boolean>();

  useEffect(() => {
    prevOpenRef.current = open;
  }, [open]);

  const prevOpen = prevOpenRef.current;

  useEffect(() => {
    if (!open && prevOpen) {
      form.resetFields();
    }
  }, [form, prevOpen, open]);
};
