import { FieldType, type FieldConfig, type EditTableConfig, type SelectOption, type PopupSelectConfig } from './types'

/**
 * 创建输入框字段配置
 */
export function createInputField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.INPUT,
    clearable: true,
    ...options
  }
}

/**
 * 创建数字输入框字段配置
 */
export function createNumberField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.NUMBER,
    precision: 2,
    clearable: true,
    ...options
  }
}

/**
 * 创建日期选择器字段配置
 */
export function createDateField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.DATE,
    clearable: true,
    valueFormat: 'x',
    ...options
  }
}

/**
 * 创建日期范围选择器字段配置
 */
export function createDateRangeField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.DATE_RANGE,
    clearable: true,
    valueFormat: 'x',
    ...options
  }
}

/**
 * 创建选择器字段配置
 */
export function createSelectField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.SELECT,
    clearable: true,
    filterable: true,
    ...options
  }
}

/**
 * 创建多选选择器字段配置
 */
export function createMultiSelectField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.MULTI_SELECT,
    multiple: true,
    clearable: true,
    filterable: true,
    ...options
  }
}

/**
 * 创建远程搜索选择器字段配置
 */
export function createRemoteSelectField(prop: string, label: string, remoteMethod: (query: string) => Promise<SelectOption[]>, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.REMOTE_SELECT,
    remote: true,
    filterable: true,
    remoteMethod,
    clearable: true,
    ...options
  }
}

/**
 * 创建树形选择器字段配置
 */
export function createTreeSelectField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.TREE_SELECT,
    clearable: true,
    filterable: true,
    checkStrictly: true,
    props: {
      label: 'label',
      children: 'children',
      disabled: 'disabled'
    },
    ...options
  }
}

/**
 * 创建树形多选选择器字段配置
 */
export function createTreeMultiSelectField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.TREE_MULTI_SELECT,
    multiple: true,
    showCheckbox: true,
    clearable: true,
    filterable: true,
    props: {
      label: 'label',
      children: 'children',
      disabled: 'disabled'
    },
    ...options
  }
}

/**
 * 创建必填字段配置
 */
export function setRequired(field: FieldConfig, message?: string): FieldConfig {
  field.required = true
  if (message) {
    field.rules = field.rules || []
    field.rules.push({
      required: true,
      message,
      trigger: field.type === FieldType.INPUT ? 'blur' : 'change'
    })
  }
  return field
}

/**
 * 创建禁用字段配置
 */
export function setDisabled(field: FieldConfig, disabled: boolean = true): FieldConfig {
  field.disabled = disabled
  return field
}

/**
 * 创建只读字段配置
 */
export function setReadonly(field: FieldConfig, readonly: boolean = true): FieldConfig {
  field.readonly = readonly
  return field
}

/**
 * 批量设置字段为只读模式
 */
export function setFieldsReadonly(fields: FieldConfig[], readonly: boolean = true): FieldConfig[] {
  return fields.map(field => setReadonly(field, readonly))
}

/**
 * 创建只读表格配置
 */
export function createReadonlyConfig(config: EditTableConfig): EditTableConfig {
  return {
    ...config,
    fields: config.fields.map(field => ({
      ...field,
      disabled: true,
      readonly: true
    }))
  }
}

/**
 * 创建自定义渲染字段配置
 */
export function createCustomField(prop: string, label: string, render: (row: any, column: FieldConfig, index: number) => any, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.INPUT,
    render,
    ...options
  }
}

/**
 * 常用验证规则
 */
export const ValidationRules = {
  required: (message: string) => ({
    required: true,
    message,
    trigger: 'blur'
  }),
  
  email: {
    type: 'email',
    message: '请输入正确的邮箱地址',
    trigger: 'blur'
  },
  
  phone: {
    pattern: /^1[3-9]\d{9}$/,
    message: '请输入正确的手机号码',
    trigger: 'blur'
  },
  
  number: {
    type: 'number',
    message: '请输入数字',
    trigger: 'blur'
  },
  
  integer: {
    type: 'integer',
    message: '请输入整数',
    trigger: 'blur'
  },
  
  min: (min: number, message?: string) => ({
    type: 'number',
    min,
    message: message || `数值不能小于${min}`,
    trigger: 'blur'
  }),
  
  max: (max: number, message?: string) => ({
    type: 'number',
    max,
    message: message || `数值不能大于${max}`,
    trigger: 'blur'
  }),
  
  minLength: (min: number, message?: string) => ({
    min,
    message: message || `长度不能少于${min}个字符`,
    trigger: 'blur'
  }),
  
  maxLength: (max: number, message?: string) => ({
    max,
    message: message || `长度不能超过${max}个字符`,
    trigger: 'blur'
  })
}

/**
 * 创建级联选择器字段配置
 */
export function createCascaderField(prop: string, label: string, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.CASCADER,
    clearable: true,
    filterable: true,
    cascaderProps: {
      expandTrigger: 'click',
      checkStrictly: false,
      emitPath: true
    },
    ...options
  }
}

/**
 * 创建弹窗选择器字段配置
 */
export function createPopupSelectField(prop: string, label: string, popupSelect: PopupSelectConfig, options: Partial<FieldConfig> = {}): FieldConfig {
  return {
    prop,
    label,
    type: FieldType.POPUP_SELECT,
    clearable: true,
    readonly: true, // 默认只读，通过弹窗选择
    popupSelect: {
      title: `选择${label}`,
      width: '800px',
      height: '600px',
      multiple: false,
      selection: {
        labelKey: 'label',
        valueKey: 'value'
      },
      pagination: {
        pageSize: 10,
        showSizeChanger: true,
        pageSizeOptions: [10, 20, 50, 100]
      },
      ...popupSelect
    },
    ...options
  }
}