import type { Renderable, Signal } from '@tempots/dom';
import type { Controller, ControllerValidation } from '../../form';
import type { SchemaContext, JSONSchema } from '../schema-context';
import { type ResolvedWidget } from './utils';
import type { ValidationMode } from '../../form/controller/union-controller';
/**
 * Widget factory function signature
 */
export type WidgetFactory<T = unknown> = (props: {
    controller: Controller<T>;
    ctx: SchemaContext;
    options?: Record<string, unknown>;
    /**
     * Function to set the form's validation status.
     * Use this for advanced widgets that need custom validation.
     * Also available via `ctx.setStatus`.
     */
    setStatus?: (status: ControllerValidation) => void;
    /**
     * Signal containing the entire form's current value.
     * Useful for cross-field validation or conditional rendering.
     * Also available via `ctx.formValue`.
     */
    formValue?: Signal<unknown>;
    /**
     * Current validation mode ('eager', 'onTouched', 'onSubmit').
     * Also available via `ctx.validationMode`.
     */
    validationMode?: ValidationMode;
    /**
     * Signal indicating whether the form is currently submitting.
     * Also available via `ctx.submitting`.
     */
    submitting?: Signal<boolean>;
}) => Renderable;
/**
 * Widget registration entry
 */
export interface WidgetRegistration<T = unknown> {
    /** Widget factory function */
    factory: WidgetFactory<T>;
    /** Widget display name */
    displayName: string;
    /** Widget description */
    description?: string;
    /** Supported JSON Schema types */
    supportedTypes?: string[];
    /** Default configuration */
    defaultConfig?: Record<string, unknown>;
    /** Configuration schema for the widget */
    configSchema?: JSONSchema;
    /** Widget priority (higher = preferred when multiple widgets match) */
    priority?: number;
    /** Whether this widget can be used as a fallback */
    canFallback?: boolean;
    /** Custom matcher function for widget selection */
    matcher?: (ctx: SchemaContext) => boolean;
}
/**
 * Widget customization configuration from x:ui
 */
export interface WidgetCustomization {
    /** Custom widget type override */
    widget?: string;
    /** Widget-specific configuration */
    config?: Record<string, unknown>;
    /** Custom CSS classes */
    className?: string;
    /** Custom styles */
    style?: Record<string, string>;
    /** Custom attributes */
    attributes?: Record<string, string>;
    /** Widget wrapper configuration */
    wrapper?: {
        tag?: string;
        className?: string;
        style?: Record<string, string>;
        attributes?: Record<string, string>;
    };
    /** Conditional widget selection */
    conditional?: {
        condition: string | ((value: unknown, formData: unknown) => boolean);
        then?: WidgetCustomization;
        else?: WidgetCustomization;
    };
}
/**
 * Custom widget registration for form-scoped widgets
 */
export interface CustomWidgetRegistration {
    /** Widget factory function */
    factory: WidgetFactory;
    /** Custom matcher function for widget selection (required) */
    matcher: (ctx: SchemaContext) => boolean;
    /** Widget priority (higher = preferred, default: 50) */
    priority?: number;
    /** Widget display name (for debugging) */
    displayName?: string;
    /** Widget description */
    description?: string;
    /** Supported JSON Schema types */
    supportedTypes?: string[];
}
/**
 * Array of custom widget registrations for the customWidgets option
 */
export type CustomWidgets = CustomWidgetRegistration[];
/**
 * Widget registry for managing custom widgets
 */
export declare class WidgetRegistry {
    private widgets;
    private typeMapping;
    /**
     * Register a custom widget
     */
    register<T = unknown>(name: string, registration: WidgetRegistration<T>): void;
    /**
     * Unregister a widget
     */
    unregister(name: string): void;
    /**
     * Get a registered widget
     */
    get(name: string): WidgetRegistration<unknown> | undefined;
    /**
     * Get all registered widgets
     */
    getAll(): Map<string, WidgetRegistration<unknown>>;
    /**
     * Get widgets that support a specific JSON Schema type
     */
    getForType(type: string): WidgetRegistration<unknown>[];
    /**
     * Find the best widget for a schema context
     */
    findBestWidget(ctx: SchemaContext): {
        name: string;
        registration: WidgetRegistration<unknown>;
        resolved: ResolvedWidget;
    } | null;
}
/**
 * Global widget registry instance
 */
export declare const globalWidgetRegistry: WidgetRegistry;
/**
 * Extract widget customization from schema x:ui
 */
export declare function getWidgetCustomization(schema: JSONSchema): WidgetCustomization | undefined;
/**
 * Apply widget customization to a rendered widget
 */
export declare function applyWidgetCustomization(widget: Renderable, customization: WidgetCustomization, value?: unknown, formData?: unknown): Renderable;
/**
 * Create a customized widget factory
 */
export declare function createCustomizedWidget<T = unknown>(baseFactory: WidgetFactory<T>, customization: WidgetCustomization): WidgetFactory<T>;
/**
 * Register built-in enhanced widgets
 */
export declare function registerEnhancedWidgets(registry?: WidgetRegistry): void;
/**
 * Helper to register a simple widget
 */
export declare function registerWidget<T = unknown>(name: string, factory: WidgetFactory<T>, options?: Partial<WidgetRegistration<T>>, registry?: WidgetRegistry): void;
/**
 * Create a custom widget registration that matches by explicit x:ui widget name
 *
 * @example
 * ```typescript
 * forXUI('fancy-email', myEmailWidget)
 * // Matches schema: { type: 'string', 'x:ui': 'fancy-email' }
 * ```
 */
export declare function forXUI(widgetName: string, factory: WidgetFactory, options?: Partial<Omit<CustomWidgetRegistration, 'factory' | 'matcher'>>): CustomWidgetRegistration;
/**
 * Create a custom widget registration that matches by schema format
 *
 * @example
 * ```typescript
 * forFormat('email', myEmailWidget)
 * // Matches ANY schema with: { format: 'email' }
 * ```
 */
export declare function forFormat(format: string, factory: WidgetFactory, options?: Partial<Omit<CustomWidgetRegistration, 'factory' | 'matcher'>>): CustomWidgetRegistration;
/**
 * Create a custom widget registration that matches by type + format combination
 *
 * @example
 * ```typescript
 * forTypeAndFormat('string', 'uuid', myUuidWidget)
 * // Matches schema: { type: 'string', format: 'uuid' }
 * ```
 */
export declare function forTypeAndFormat(type: string, format: string, factory: WidgetFactory, options?: Partial<Omit<CustomWidgetRegistration, 'factory' | 'matcher'>>): CustomWidgetRegistration;
/**
 * Create a diagnostic custom widget registration for debugging custom widget matching.
 *
 * This utility helps diagnose issues where custom widgets aren't being matched as expected.
 * It logs detailed information about each property that's being processed, including:
 * - Property name and path
 * - Schema type
 * - Whether the widgetRegistry is available
 * - Custom matcher results
 *
 * @example
 * ```typescript
 * const customWidgets = [
 *   // Add the diagnostic widget first to trace all properties
 *   createDiagnosticWidget({
 *     logPrefix: 'CUSTOM_WIDGET_DEBUG',
 *     filterFn: (ctx) => true, // Log all properties
 *   }),
 *   // Then add your actual custom widgets
 *   myCustomWidget,
 * ]
 * ```
 */
export declare function createDiagnosticWidget(options?: {
    /** Prefix for log messages (default: 'WIDGET_DIAG') */
    logPrefix?: string;
    /** Filter function to control which properties are logged */
    filterFn?: (ctx: SchemaContext) => boolean;
    /** Callback function called for each processed property */
    onProcess?: (info: {
        name: string | undefined;
        path: string[];
        type: string | undefined;
        hasRegistry: boolean;
        definition: unknown;
    }) => void;
}): CustomWidgetRegistration;
