import store from '@/store'
import { cloneDeep } from 'lodash'

/**
 * 设置 审批流程 模块信息
 * @param {Object} info 审批信息
 * @author Matrix
 * @since 2022/10/08
 */
export const SET_APPROVAL_PROCESS_INFO = info => {
  const APPROVAL_PROCESS_INFO = cloneDeep(info)
  store.commit('__setApprovalModuleInfo', APPROVAL_PROCESS_INFO)
}

/**
 * 设置 审批流程 规则
 * @param {Object} rules 审批规则
 * @author Matrix
 * @since 2022/10/08
 */
export const SET_APPROVAL_PROCESS_RULES = rules => {
  const APPROVAL_PROCESS_RULES = cloneDeep(rules)
  store.commit('__setApprovalModuleRules', APPROVAL_PROCESS_RULES)
}

/**
 * 移除 审批流程 规则
 * @param {Object} rules 审批规则
 * @author Matrix
 * @since 2022/10/08
 */
export const REMOVE_APPROVAL_PROCESS_RULES = rules => {
  const APPROVAL_PROCESS_RULES = cloneDeep(rules)
  if (APPROVAL_PROCESS_RULES && APPROVAL_PROCESS_RULES.length) {
    APPROVAL_PROCESS_RULES.forEach(rule => {
      if (rule?.components?.length) {
        rule.components.forEach(component => {
          if (component?.fields?.length) {
            component.fields = component.fields.filter(field => !field.switch)
          }
          if (rule.executeAble && rule.executeAble.length) {
            rule.executeAble = rule.executeAble.filter(field => {
              if (field.switch) {
                field.switch = false
              }
              return true
            })
          }
        })
      }
      // if (rule.fields && rule.fields.length) {
      //   rule.fields = rule.fields.filter(field => !field.switch)
      // }
    })
  }
  store.commit('__setApprovalModuleRules', APPROVAL_PROCESS_RULES)
}

/**
 * 重置 审批流程 信息和规则
 * @author Matrix
 * @since 2022/10/08
 */
export const RESET_APPROVAL_PROCESS_INFO_AND_RULES = () => {
  store.commit('__removeApprovalModuleInfoAndRules')
}
