import * as appService from '../services/search'

export default {
  namespace: 'search',
  state: {
    btnEntities: [],
    // 共享以及记忆查询
    shareData: {
      // 存储每个 shareKey 对应的查询条件
      // 数据结构如下
      // 'shareKey': [
      //   { field: '', value: '', defaultValue: '' }
      // ]
    },
    shareIns: {
      // 存储每个 Search 组件实例
      // 数据结构如下：
      // 'shareKey': [
      //   {
      //     insRef: '', // 每个 Search 组件的名字
      //     instance: '', // Search 组件的实例
      //   }
      // ]
    },

    // 导出功能，用于导出的 excel 插入标题
    fieldConfigs: {
      // 存储每个 formCode 对应的查询条件
      // 数据结构如下：
      // PF001: [
      //   { field: '', value: '', defaultValue: '' }
      // ]
    },
    relatedFieldConfigs: {
      // 存储每个 formCode 对应相关联的其他 formCode 的查询条件
      // 数据结构如下：
      // PF001: {
      //   relatedTo: 'PF002',
      //   extraRows: ['插入行']
      // }
    }
  },
  effects: {
    *getSysFields ({ payload, onlyData, ignoreThrottle = true }, { call }) {
      return yield call(appService.getSysFields, payload, onlyData, ignoreThrottle)
    },
    *getUserFields ({ payload, onlyData, ignoreThrottle = true }, { call }) {
      return yield call(appService.getUserFields, payload, onlyData, ignoreThrottle)
    },
    *getUserConfig ({ payload }, { call }) {
      return yield call(appService.getUserConfig, payload)
    },
    *getQueryFields ({ payload }, { call }) {
      return yield call(appService.getQueryFields, payload)
    },
    *deleteUserConfig ({ payload }, { call }) {
      return yield call(appService.deleteUserConfig, payload)
    },
    *saveUserConfig ({ payload }, { call }) {
      return yield call(appService.saveUserConfig, payload)
    },
    *updateUserConfigIsDefault ({ payload }, { call }) {
      return yield call(appService.updateUserConfigIsDefault, payload)
    },
    *saveUserConfigFeildDefaultValue ({ payload }, { call }) {
      return yield call(appService.saveUserConfigFeildDefaultValue, payload)
    },
  },
  reducers: {
    save (state, action) {
      return { ...state, ...action.payload }
    },
    setShareData (state, { payload }) {
      const { shareData } = state
      const { key, data } = payload
      shareData[key] = data
      return { ...state }
    },
    setShareIns (state, { payload }) {
      const { shareIns } = state
      const { key, insRef, instance } = payload
      if (shareIns[key]) {
        const target = shareIns[key].find(item => item.insRef === insRef)
        !target && shareIns[key].push({ insRef, instance })
      } else {
        shareIns[key] = [{ insRef, instance }]
      }
      return { ...state }
    },
    cleanShareIns (state, { payload }) {
      const { shareIns } = state
      const { key, insRef } = payload
      if (shareIns[key]) {
        const targetIndex = shareIns[key].findIndex(item => item.insRef === insRef)
        targetIndex !== -1 && shareIns[key].splice(targetIndex, 1)
      }
      return { ...state }
    },
    setFieldConfigs (state, { payload }) {
      const { fieldConfigs } = state
      const { key, data } = payload
      fieldConfigs[key] = data
      return { ...state }
    },
    setRelatedFieldConfigs (state, { payload }) {
      const { relatedFieldConfigs } = state
      const { code, relatedTo, extraRows } = payload
      relatedFieldConfigs[code] = {
        relatedTo,
        extraRows
      }
      return { ...state }
    }
  }
}
