import {useEffect} from "react";
import {Form} from "antd";


const useLensFormUI = (lensTypes, visible, onCancel, onSubmit, lens) => {
    const [form] = Form.useForm();

    useEffect(() => {
        if (visible) {
            form.resetFields();
            if (lens) {
                form.setFieldsValue({
                    ...lens,
                });
            }
        }
    }, [visible, lens, form]);

    const modalStyle = {
        marginTop: 20,
    };
    const formItemStyle = { width: "100%" };

    const handleOk = async () => {
        try {
            const values = await form.validateFields();
            onSubmit(values);
            form.resetFields();
        } catch (error) {
            console.log("Validation Failed:", error);
        }
    };

    const handleCancel = () => {
        form.resetFields();
        onCancel();
    };
    
    return {
        form,
        modalStyle,
        formItemStyle,
        handleOk,
        handleCancel,
    };
};

export default useLensFormUI;