import type { DynamicFormItem, DynamicFormConfig } from './types'

/**
 * 创建数字输入表单项
 */
export const createNumberItem = (
  key: string,
  label: string,
  options: {
    min?: number
    max?: number
    precision?: number
    step?: number
    placeholder?: string
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'number',
    min: options.min,
    max: options.max,
    precision: options.precision || 0,
    step: options.step || 1,
    placeholder: options.placeholder || `请输入${label}`,
    required: options.required,
    disabled: options.disabled,
    span: options.span,
    fullWidth: options.fullWidth
  }
}

/**
 * 创建文本输入表单项
 */
export const createTextItem = (
  key: string,
  label: string,
  options: {
    inputType?: string
    rows?: number
    placeholder?: string
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: options.rows && options.rows > 1 ? 'textarea' : 'text',
    inputType: options.inputType || 'text',
    rows: options.rows,
    placeholder: options.placeholder || `请输入${label}`,
    required: options.required,
    disabled: options.disabled,
    span: options.span,
    fullWidth: options.fullWidth
  }
}

/**
 * 创建选择器表单项
 */
export const createSelectItem = (
  key: string,
  label: string,
  options: Array<{ label: string; value: any; disabled?: boolean }>,
  config: {
    placeholder?: string
    required?: boolean
    disabled?: boolean
    filterable?: boolean
    multiple?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'select',
    options,
    placeholder: config.placeholder || `请选择${label}`,
    required: config.required,
    disabled: config.disabled,
    filterable: config.filterable,
    multiple: config.multiple,
    span: config.span,
    fullWidth: config.fullWidth
  }
}

/**
 * 创建日期选择表单项
 */
export const createDateItem = (
  key: string,
  label: string,
  options: {
    dateType?: 'date' | 'datetime' | 'daterange' | 'datetimerange'
    format?: string
    valueFormat?: string
    placeholder?: string
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'date',
    dateType: options.dateType || 'date',
    format: options.format,
    valueFormat: options.valueFormat,
    placeholder: options.placeholder || `请选择${label}`,
    required: options.required,
    disabled: options.disabled,
    span: options.span,
    fullWidth: options.fullWidth
  }
}

/**
 * 创建开关表单项
 */
export const createSwitchItem = (
  key: string,
  label: string,
  options: {
    activeText?: string
    inactiveText?: string
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'switch',
    activeText: options.activeText,
    inactiveText: options.inactiveText,
    required: options.required,
    disabled: options.disabled,
    span: options.span,
    fullWidth: options.fullWidth
  }
}

/**
 * 创建单选框组表单项
 */
export const createRadioItem = (
  key: string,
  label: string,
  options: Array<{ label: string; value: any; disabled?: boolean }>,
  config: {
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'radio',
    options,
    required: config.required,
    disabled: config.disabled,
    span: config.span,
    fullWidth: config.fullWidth
  }
}

/**
 * 创建复选框组表单项
 */
export const createCheckboxItem = (
  key: string,
  label: string,
  options: Array<{ label: string; value: any; disabled?: boolean }>,
  config: {
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'checkbox',
    options,
    required: config.required,
    disabled: config.disabled,
    span: config.span,
    fullWidth: config.fullWidth
  }
}

/**
 * 创建滑块表单项
 */
export const createSliderItem = (
  key: string,
  label: string,
  options: {
    min?: number
    max?: number
    step?: number
    showInput?: boolean
    showStops?: boolean
    showTooltip?: boolean
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'slider',
    min: options.min || 0,
    max: options.max || 100,
    step: options.step || 1,
    showInput: options.showInput,
    showStops: options.showStops,
    showTooltip: options.showTooltip,
    required: options.required,
    disabled: options.disabled,
    span: options.span,
    fullWidth: options.fullWidth
  }
}

/**
 * 创建自定义插槽表单项
 */
export const createCustomItem = (
  key: string,
  label: string,
  slotName: string,
  options: {
    required?: boolean
    disabled?: boolean
    span?: number
    fullWidth?: boolean
  } = {}
): DynamicFormItem => {
  return {
    key,
    label,
    type: 'custom',
    slotName,
    required: options.required,
    disabled: options.disabled,
    span: options.span,
    fullWidth: options.fullWidth
  }
}

/**
 * 创建动态表单配置
 */
export const createDynamicFormConfig = (
  items: DynamicFormItem[],
  config: {
    title?: string
    gutter?: number
    columnSpan?: number
    formProp?: string
  } = {}
): DynamicFormConfig => {
  return {
    title: config.title,
    gutter: config.gutter || 16,
    columnSpan: config.columnSpan || 12,
    formProp: config.formProp,
    items
  }
}

/**
 * 验证表单项配置
 */
export const validateFormItem = (item: DynamicFormItem): boolean => {
  if (!item.key || !item.label || !item.type) {
    console.error('表单项配置不完整:', item)
    return false
  }

  // 验证选择器类型必须有选项
  if (['select', 'radio', 'checkbox'].includes(item.type) && (!item.options || item.options.length === 0)) {
    console.error('选择器类型表单项必须有选项:', item)
    return false
  }

  return true
}

/**
 * 验证表单配置
 */
export const validateFormConfig = (config: DynamicFormConfig): boolean => {
  if (!config.items || config.items.length === 0) {
    console.error('表单配置必须有表单项')
    return false
  }

  return config.items.every(validateFormItem)
}
