"use client" import { useState } from "react" import { FileText } from "lucide-react" import { Button } from "@/shared/components/ui/button" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog" import { ScrollArea } from "@/shared/components/ui/scroll-area" import { InvoiceForm } from "@/modules/invoices/invoice-form" import { toRelation } from "@/shared/lib/utils" import { useEstimate } from "./estimate-context" /** * Maps an Estimate data object to an InvoiceFormValues shape so that * useResourceForm's shallow spread correctly pre-fills all relational fields. */ function mapEstimateToInvoiceInitialData(estimate: Record) { return { subject: estimate.title ?? "", notes: estimate.footer ?? "", // Relation fields — must be { value, label } objects for RhfAsyncSelectField customer: toRelation(estimate.customer_id, estimate.customer_name), vehicle: toRelation(estimate.vehicle_id, estimate.vehicle_name), department: toRelation(estimate.department_id, estimate.department_name), invoice_number: "", invoice_date: estimate.date ?? "", due_date: "", status: "draft" as const, } } export function CreateInvoiceFromEstimateButton() { const [open, setOpen] = useState(false) const estimateContext = useEstimate() if (!estimateContext) return null const initialData = estimateContext.data ? mapEstimateToInvoiceInitialData(estimateContext.data) : undefined return ( <> Generate Invoice from Estimate setOpen(false)} /> ) }