'use client'
import React, { useEffect } from "react";
import { useRouter } from "next/navigation";
import { ViewFormProps } from "@/validations/mutual/forms/type";
import { renderInputsBySchemaView } from "@/lib/renderInputs";

const ViewForm: React.FC<ViewFormProps> = ({ schemas, selectedRow, rollbackTo, labels }) => {
    const router = useRouter()
    useEffect(() => {
        if (!selectedRow) {
            router.push(rollbackTo, { scroll: false })
        }
    }, [selectedRow])
    const viewSchema = schemas.view
    const findLabels = Object.entries(viewSchema?.shape || {}).reduce((acc: any, [key, value]: any) => {
        acc[key] = {
            label: labels[key] || key,
            description: value.description,
        }
        return acc
    }, {})
    return (
        <div>
            <div>View Form</div>
            {selectedRow && renderInputsBySchemaView(findLabels, selectedRow)}
        </div>
    )
}

export default ViewForm
