import Utils from '@/utils/util'
import FormOptions from '../constants/formOptions'

const formFieldUtil = {
  /**
   * 获取所有字段
   * @param {*} columns
   */
  getColumns(columns) {
    const formColumns = []
    const traverse = (fields) => {
      fields.forEach((field) => {
        if (FormOptions.t.NESTED_FIELD_TYPES.includes(field.field_type)) {
          const childColumns = field.field_options.columns
          childColumns.forEach(child => {
            traverse(child.fields)
          })
        } else {
          formColumns.push(field)
        }
      })
    }
    traverse(columns)
    return formColumns
  },
  /**
   * 获取子表显示所有字段
   * @param {*} columns
   */
  getSubDisplayColumns(columns) {
    const formColumns = []
    const traverse = (fields) => {
      fields.forEach((field) => {
        if (FormOptions.t.NESTED_FIELD_TYPES.includes(field.field_type)) {
          const childColumns = field.field_options.columns
          childColumns.forEach(child => {
            traverse(child.fields)
          })
        } else {
          if (Utils.toBoolean(field.field_options.display, true)) {
            formColumns.push(field)
          }
        }
      })
    }
    traverse(columns)
    return formColumns
  }
}

export default formFieldUtil
