import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useForm, FormProvider } from "react-hook-form";
import { Button, Collection, DateInput, Dialog, Input, Loader, } from "../../components";
import { useCrud } from "../../contexts";
export const ListCrudDocsEditDialog = ({ isOpen, onClose, persona, }) => {
    const methods = useForm({
        defaultValues: persona,
    });
    const { createItem, updateItem, fetchItems } = useCrud();
    const { register, handleSubmit, formState: { errors }, } = methods;
    const onSubmit = async (persona) => {
        if (persona.id) {
            await updateItem.execute(persona);
        }
        else {
            await createItem.execute(persona);
        }
        onClose();
        fetchItems.execute();
    };
    const isLoading = createItem.isLoading || updateItem.isLoading;
    return (_jsx(FormProvider, { ...methods, children: _jsx(Dialog, { isOpen: isOpen, onClose: onClose, title: "Editar Persona", footer: _jsxs(_Fragment, { children: [_jsx(Button, { variant: "outline", onClick: onClose, children: "Cancelar" }), _jsx(Button, { onClick: handleSubmit(onSubmit), children: "Guardar" })] }), children: _jsx("form", { onSubmit: handleSubmit(onSubmit), onKeyDown: (e) => {
                    if (e.key === "Enter") {
                        e.preventDefault();
                        handleSubmit(onSubmit)();
                    }
                }, children: _jsxs(Collection, { children: [_jsx(Input, { label: "Nombre", ...register("nombre", { required: "Campo obligatorio" }), error: errors.nombre?.message }), _jsx(Input, { label: "Email", ...register("email", { required: "Campo obligatorio" }), error: errors.email?.message }), _jsx(DateInput, { label: "Fecha de Nacimiento", placeholder: "dd/mm/yyyy", icon: "fa-calendar-alt", ...register("fechaNacimiento", {
                                required: "Campo obligatorio",
                            }), error: errors.fechaNacimiento?.message }), _jsx(Loader, { isLoading: isLoading })] }) }) }) }));
};
