"use client"; import { useEffect, type ReactNode } from "react"; import * as z from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { HiOutlineChevronDown, HiXMark } from "react-icons/hi2"; import Image from "next/image"; import { FieldErrors, useForm, FieldValues, DefaultValues, Path, PathValue, } from "react-hook-form"; import { cn } from "@/lib/utils"; import Template from "./Template"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Textarea } from "@/components/ui/textarea"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; import Spinner from "../spinner"; import useImageUpload from "@/lib/hooks/useImageUpload"; type FieldOptions = | "toggle" | "horizontal-tabs" | "input" | "number" | "text-area" | "upload" | "date-time" | "custom"; type DefaultFieldType = { label: string; slug: keyof z.infer> & string; placeholder?: string; subtitle?: string; description?: string; lines?: number; styles?: string; value?: string | number | boolean; custom?: ReactNode; condition?: keyof z.infer> & string; options?: { label: string; value: string; icon?: ReactNode; description?: string; }[]; }; type FieldType = DefaultFieldType & ( | { type: "select-search"; options: { label: string; description?: string; value: string; icon?: ReactNode; }[]; } | { type: "select"; description?: string; options: { label: string; value: string; icon?: ReactNode; description?: string; }[]; } | { type: FieldOptions; } ); type FormModalProps = { title: string; fields: FieldType[]; errors?: FieldErrors; isSubmitting?: boolean; cta: { text: string; }; defaultValues?: Partial>>; onSubmit: (props: z.infer>) => any; formSchema: z.Schema; }; export default function FormModal({ title, fields, cta, errors, isSubmitting, formSchema, defaultValues, onSubmit, }: FormModalProps) { type FormDataType = z.infer; const form = useForm({ resolver: zodResolver(formSchema), defaultValues: defaultValues as DefaultValues, mode: "onChange", }); const { watch, setValue } = form; return (