import { isExecute, isHide } from './set-form-control'
/**
 * 参数 params 结构 [{key:field,value: ref}]
 * 数组的每一项是一个对象, key 为字段名 value 为组件的 ref
 * @param {Array<object>} params
 */

export const initAbles = params => {
  const submitList = []
  if (params && params.length) {
    params.forEach(item => {
      if (!isHide(item.key)) {
        submitList.push(item.value)
      }
    })
  }
  return submitList
}

/**
 * 初始化数据 Refs
 * @param {Array<object>} fields
 * @param {Object} params
 */
export function initRefAbles(fields, params) {
  const refs = initAbles(fields)
  for (let ref of refs) {
    this.$nextTick(() => {
      this.$refs[ref].getInit(params)
    })
  }
}

export const submitAbles = params => {
  const submitList = []
  if (params && params.length) {
    params.forEach(item => {
      if (!isHide(item.key) || isExecute(item.key)) {
        submitList.push(item.value)
      }
    })
  }
  return submitList
}

/**
 * 提交 Refs
 * @param {Array<object>} fields
 * @returns Array
 */
export function submitRefAbles(fields) {
  const refs = submitAbles(fields)
  const requestQueue = []
  for (let ref of refs) {
    requestQueue.push(this.$refs[ref].handleSubmit())
  }
  return requestQueue
}

/**
 * 移除所有校验 (使 Modal 组件的 destroyOnClose 属性生效)
 * @author Matrix
 * @since 2022/09/09
 */
export function removeRefValidate(fields) {
  const refs = submitAbles(fields)
  for (let ref of refs) {
    this.$nextTick(() => {
      this.$refs[ref].removeValidate()
    })
  }
}
