import * as z from 'zod' import { createValidationSchema } from '@/app/lib/validation-config-parser'; import validationConfig from '@/app/lib/validation-config.json'; export type FormState = | { errors?: { name?: string[] email?: string[] password?: string[] } message?: string } | undefined export type FormAction = ( state: FormState, formData: FormData ) => Promise interface FieldConfig { maxSize?: { value: number; message: string }; minSize?: { value: number; message: string }; allowedSymbols?: { value: string; message: string }; optional?: { value: boolean }; requiredDigit?: { value: boolean; message: string }; requiredLetter?: { value: boolean; message: string }; caseType?: { value: 'LOWER' | 'UPPER' | 'ANY'; message: string }; emailValidation?: { value: boolean; message: string }; } interface ValidationConfig { [key: string]: FieldConfig; } const defaultConfig = validationConfig as ValidationConfig; let cachedSignupSchema: any = null; let schemaPromise: Promise | null = null; async function loadConfigFromBackend(): Promise { try { const response = await fetch('/api/validation-config'); if (!response.ok) throw new Error('Failed to load config'); return await response.json(); } catch (error) { console.error('Using default validation config:', error); return defaultConfig; } } export async function getSignupFormSchema() { console.log('get scheme'); if (cachedSignupSchema) { return cachedSignupSchema; } if (!schemaPromise) { schemaPromise = (async () => { const config = await loadConfigFromBackend(); cachedSignupSchema = createValidationSchema(config); return cachedSignupSchema; })(); } return await schemaPromise; } export const SignupFormSchema = createValidationSchema(defaultConfig); export const loginFormSchema = z .object({ email: z .string() .min(1, { error: 'login-error-email' }) .trim(), password: z .string() .min(1, { error: 'login-error-password' }) .trim(), }) export const localDevelopmentUrl = process.env.DEV_URL ? process.env.DEV_URL : 'http://localhost'; export const API_BASE_URL = process.env.NODE_ENV === 'development' ? localDevelopmentUrl : 'http://app:8080'; export const testUserData = { fullName: 'test', email: 'test@mail.com', subscriptionType: 'base' }