import type { StoreApi } from 'zustand'; import type { UseBoundStoreWithEqualityFn } from 'zustand/traditional'; export interface DefaultValues { contractOutputsToken: string; fromAmount: string; fromChain?: number; fromToken?: string; toAddress: string; toAmount: string; toChain?: number; toContractAddress: string; toContractCallData: string; toContractGasLimit: string; toToken?: string; tokenSearchFilter: string; } export type GenericFormValue = string | number | undefined; export interface FormValueControl { isTouched: boolean; isDirty: boolean; value: T; } export interface FormValues { contractOutputsToken: FormValueControl; fromAmount: FormValueControl; fromChain?: FormValueControl; fromToken?: FormValueControl; toAddress: FormValueControl; toAmount: FormValueControl; toChain?: FormValueControl; toContractAddress: FormValueControl; toContractCallData: FormValueControl; toContractGasLimit: FormValueControl; toToken?: FormValueControl; tokenSearchFilter: FormValueControl; } export type FormFieldNames = keyof FormValues; export type ExtractValueType = T extends FormValueControl ? U : never; export type FormFieldArray = { [K in keyof T]: ExtractValueType; }; export type TouchedFields = { [key in FormFieldNames]?: boolean }; type ValidationFn = (value: any) => Promise; export interface ValidationProps { isValid: boolean; isValidating: boolean; errors: { [key in FormFieldNames]?: string; }; validation: { [key in FormFieldNames]?: ValidationFn; }; } export interface ValidationActions { addFieldValidation: ( name: FormFieldNames, validationFn: ValidationFn, ) => void; triggerFieldValidation: (name: FormFieldNames) => Promise; clearErrors: (name: FormFieldNames) => void; } export interface FormProps { defaultValues: FormValues; userValues: FormValues; touchedFields: { [key in FormFieldNames]?: boolean }; } interface ResetOptions { defaultValue?: GenericFormValue; } export interface FormActions { setDefaultValues: (formValues: DefaultValues) => void; isTouched: (fieldName: FormFieldNames) => boolean; setAsTouched: (fieldName: FormFieldNames) => void; resetField: (fieldName: FormFieldNames, resetOptions?: ResetOptions) => void; setFieldValue: ( fieldName: FormFieldNames, value: GenericFormValue, options?: SetOptions, ) => void; getFieldValues: ( ...names: T ) => FormFieldArray; } export type FormValuesState = FormProps & FormActions & ValidationProps & ValidationActions; export type FormStoreStore = UseBoundStoreWithEqualityFn< StoreApi >; interface SetOptions { isDirty?: boolean; isTouched?: boolean; } export type FormType = 'from' | 'to'; export interface FormTypeProps { formType: FormType; } export const FormKeyHelper = { getChainKey: (formType: FormType): 'fromChain' | 'toChain' => `${formType}Chain`, getTokenKey: (formType: FormType): 'fromToken' | 'toToken' => `${formType}Token`, getAmountKey: (formType: FormType): 'fromAmount' | 'toAmount' => `${formType}Amount`, };