"use client"; import { useState } from "react"; import { useForm, type FieldValues, type DefaultValues, type Resolver } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { Plus, Pencil, Trash2, RotateCcw } from "lucide-react"; import type { z } from "zod"; import { Button, Dialog, Drawer, Input, EmptyState, FormField, ConfirmDialog, Textarea, Select, } from "@/components/ds"; import { LoyaltyTablePage } from "@/modules/loyalty/design-system"; import { LoyaltyBreadcrumbs } from "@/modules/loyalty/components/LoyaltyBreadcrumbs"; import { LoyaltyPageLoader, LoyaltyPageError, exportToCsv } from "@/modules/loyalty/pages/shared"; import { useTenantId } from "@/hooks/useTenantId"; export type LoyaltyFieldConfig = { name: string; label: string; type?: "text" | "email" | "tel" | "number" | "textarea" | "select"; options?: { value: string; label: string }[]; required?: boolean; editOnly?: boolean; createOnly?: boolean; }; export type LoyaltyListCrudConfig = { title: string; description?: string; breadcrumb?: string; resourceKey: string; createSchema: z.ZodTypeAny; editSchema: z.ZodTypeAny; createDefaults: DefaultValues; fields: LoyaltyFieldConfig[]; columns: { key: string; header: string; render?: (row: T) => React.ReactNode; }[]; list: (tenantId: string) => Promise; create: (tenantId: string, data: FieldValues) => Promise; update?: (tenantId: string, id: string, data: FieldValues) => Promise; remove?: (tenantId: string, id: string) => Promise; restore?: (tenantId: string, id: string) => Promise; exportColumns?: string[]; filterDeleted?: boolean; extraActions?: (row: T, helpers: { refresh: () => void }) => React.ReactNode; }; function renderField( field: LoyaltyFieldConfig, register: ReturnType["register"], mode: "create" | "edit" ) { if (mode === "create" && field.editOnly) return null; if (mode === "edit" && field.createOnly) return null; if (field.type === "textarea") { return (