// ==================== 校验规则类型定义 ====================

// 基础校验规则
type BaseRule = {
  /** 错误提示信息 */
  message?: string;
};

// 必填规则
type RequiredRule = BaseRule & {
  required: true;
};

// 最小值规则（数字）
type MinRule = BaseRule & {
  min: number;
};

// 最大值规则（数字）
type MaxRule = BaseRule & {
  max: number;
};

// 最小长度规则（字符串/数组）
type MinLengthRule = BaseRule & {
  minLength: number;
};

// 最大长度规则（字符串/数组）
type MaxLengthRule = BaseRule & {
  maxLength: number;
};

// 自定义校验函数规则
type ValidatorRule<TValue = any> = BaseRule & {
  /** 自定义校验函数，返回错误信息或 Promise<错误信息> */
  validator: (
    value: TValue,
    formValues: Record<string, any>
  ) => string | Promise<string> | void | Promise<void>;
};

// 所有规则类型的联合
export type Rule<TValue = any> =
  | RequiredRule
  | MinRule
  | MaxRule
  | MinLengthRule
  | MaxLengthRule
  | ValidatorRule<TValue>;

// 规则数组类型
export type Rules<TValue = any> = Rule<TValue>[];

import type React from 'react';

// ==================== Form Context 类型 ====================

export type FormContextValue<TValues extends Record<string, any>> = {
  // 表单值 ref（不触发重新渲染）
  valuesRef: React.MutableRefObject<TValues>;
  // 表单错误 ref（不触发重新渲染）
  errorsRef: React.MutableRefObject<Record<string, string>>;
  // 设置字段值
  setFieldValue: (name: string, value: any) => void;
  // 获取字段值（从 ref 读取）
  getFieldValue: (name: string) => any;
  // 设置字段错误
  setFieldError: (name: string, error: string | undefined) => void;
  // 获取字段错误
  getFieldError: (name: string) => string | undefined;
  // 校验字段
  validateField: (name: string) => Promise<boolean>;
  // 校验所有字段
  validateAll: () => Promise<boolean>;
  // 获取所有表单值
  getValues: () => TValues;
  // 重置表单
  reset: (values?: Partial<TValues>) => void;
  // 表单禁用状态
  disabled: boolean;
  // 注册字段规则（内部使用）
  registerFieldRules: (name: string, rules: Rules) => void;
  // 订阅字段变化（用于性能优化）
  subscribe: (name: string, callback: () => void) => () => void;
};

// ==================== Form 组件类型 ====================

export type CombineValidateResult = {
  key: string;
  message: string;
} | null;

export type FormProps<
  TValues extends Record<string, any> = Record<string, any>,
> = {
  initialValues?: Partial<TValues>;
  onSubmit?: (data: TValues) => void | Promise<void>;
  onChange?: (data: Partial<TValues>) => void;
  /** 组合校验函数，用于校验多个字段的组合条件 */
  combineValidate?: (
    values: TValues
  ) => CombineValidateResult | Promise<CombineValidateResult>;
  loading?: boolean;
  disabled?: boolean;
  children: React.ReactNode;
};

export type FormAction<
  TValues extends Record<string, any> = Record<string, any>,
> = {
  validateFields: () => Promise<TValues | null>;
  getValues: () => TValues;
  reset: (values?: Partial<TValues>) => void;
  setFieldValue: (name: string, value: any) => void;
  setFieldError: (name: string, error: string | undefined) => void;
};

// ==================== FormItem 组件类型 ====================

export type FormItemProps<
  TValues extends Record<string, any> = Record<string, any>,
> = {
  /** 校验触发时机 */
  trigger?: 'onChange' | 'onBlur';
  /** 字段名，支持嵌套路径如 'user.name' */
  name: string;
  /** 校验规则数组 */
  rules?: Rules;
  /** 渲染函数 */
  render: (
    props: {
      value: any;
      onChange: (value: any) => void;
      onBlur: () => void;
    },
    error: string | undefined
  ) => React.ReactNode;
  /** 是否禁用该字段 */
  disabled?: boolean;
};

// ==================== FormDependency 组件类型 ====================

export type FormDependencyProps<
  TValues extends Record<string, any> = Record<string, any>,
> = {
  /** 依赖的字段名数组 */
  dependencies: string[];
  /** 渲染函数 */
  render: (props: { values: Record<string, any> }) => React.ReactNode;
};

// ==================== FormError 组件类型 ====================

export type FormErrorProps<
  TValues extends Record<string, any> = Record<string, any>,
> = {
  /** 要监听的字段名数组 */
  name: string[];
  /** 渲染函数 */
  render: (props: { errors: Record<string, string> }) => React.ReactNode;
};
