/**
 * FormBackend - Unified backend interface for form operations
 *
 * This module provides types for a unified interface that works with either:
 * - React Hook Form (RHF) for settings forms
 * - Zustand Store for content editing with autosave
 *
 * Validation Flow:
 * - RHF backends: Validation handled by resolver passed to useForm (zodResolver, etc.)
 * - Store backends: Validation handled by validateFn option
 *   Example: createStoreBackend({ validateFn: (values) => schema.safeParse(values).error?.issues ?? [] })
 */
import type { ZodIssue } from 'zod';
import type { FieldValues, UseFormReturn } from 'react-hook-form';
import type { EditorBlockId } from '../../../core/src/index';
/**
 * Zustand-like store interface for store backend.
 * We use a minimal contract here to avoid circular dependencies with content-editor.
 * The actual EditorUiStoreInstance has more methods, but we only require
 * the core zustand contract at the type level.
 */
export interface ZustandLikeStore<TState = unknown> {
    getState: () => TState;
    subscribe: (callback: () => void) => () => void;
}
export type SetValueOptions = {
    shouldDirty?: boolean;
    shouldTouch?: boolean;
    shouldValidate?: boolean;
};
export type FieldError = {
    message: string;
    type?: string;
};
export type FieldState = {
    error?: FieldError;
    isDirty?: boolean;
    isTouched?: boolean;
    invalid?: boolean;
};
export type FormState = {
    isDirty: boolean;
    isValid: boolean;
    isSubmitting?: boolean;
    errors: Record<string, FieldError>;
};
/**
 * Unified form backend interface.
 * Factory functions (createRHFBackend, createStoreBackend) return this interface.
 *
 * Internal properties (form, store, scope, blockId) are available for hooks that need
 * direct access to underlying implementations (e.g., for reactivity).
 */
export interface FormBackend {
    /** Backend type identifier */
    type: 'rhf' | 'store';
    /** RHF form instance (RHF backend only) */
    form?: UseFormReturn<FieldValues>;
    /** Zustand store instance (Store backend only) */
    store?: ZustandLikeStore;
    /** Store scope (Store backend only) */
    scope?: 'blocks' | 'metadata' | 'content';
    /** Block ID for blocks scope (Store backend only) */
    blockId?: EditorBlockId;
    /** Get value at field path */
    getValue: (path: string) => unknown;
    /** Set value at field path with options */
    setValue: (path: string, value: unknown, options?: SetValueOptions) => void;
    /** Get all form values */
    getValues: () => Record<string, unknown>;
    /** Get field-level state (error, dirty, touched) */
    getFieldState: (path: string) => FieldState;
    /** Set custom error on field (for async/server validation) */
    setError: (path: string, error: FieldError) => void;
    /** Clear errors (specific field or all) */
    clearErrors: (path?: string) => void;
    /** Get all form errors */
    getErrors: () => Record<string, FieldError>;
    /** Manually mark field as dirty */
    setDirty: (path: string, isDirty: boolean) => void;
    /** Manually mark field as touched */
    setTouched: (path: string, isTouched: boolean) => void;
    /** Reset field to default value */
    resetField: (path: string) => void;
    /** Request focus on field */
    setFocus: (path: string) => void;
    /** Array field operations for repeater widgets */
    arrayField: {
        /** Append item to array */
        append: (path: string, value: unknown) => void;
        /** Remove item at index */
        remove: (path: string, index: number) => void;
        /** Move item from one index to another */
        move: (path: string, from: number, to: number) => void;
        /** Insert item at index */
        insert: (path: string, index: number, value: unknown) => void;
    };
    /** Get form-level state */
    formState: FormState;
    /** Reset entire form (optionally with new values) */
    reset: (values?: unknown) => void;
    /** Trigger validation */
    validate: () => Promise<boolean>;
    /**
     * Subscribe to form changes. Callback fires whenever any value changes.
     * Returns unsubscribe function.
     *
     * This is the unified subscription API - consumers should never branch on backend.type.
     */
    subscribe: (callback: () => void) => () => void;
}
/**
 * Options for creating a store backend
 */
export type StoreBackendOptions<TState = unknown> = {
    scope: 'blocks' | 'metadata' | 'content';
    store: ZustandLikeStore<TState>;
    blockId?: EditorBlockId;
    validateFn?: (values: Record<string, unknown>) => ZodIssue[];
};
/**
 * Type guards for discriminating backend types
 */
export declare function isRhfBackend(backend: FormBackend): backend is FormBackend & {
    type: 'rhf';
    form: UseFormReturn<FieldValues>;
};
export declare function isStoreBackend(backend: FormBackend): backend is FormBackend & {
    type: 'store';
    store: ZustandLikeStore;
    scope: 'blocks' | 'metadata' | 'content';
    blockId?: EditorBlockId;
};
export type { FieldDefinition } from '../../../blocks/src/index';
export { createRHFBackend } from './rhf-backend';
export { createStoreBackend } from './store-backend';
export { useBackendWatch } from './useBackendWatch';
