
import { appconfig } from '@/config/bigScreenConfig'
const getDefaultState = () => {
  return {
    view: null,
    //地图配置信息
    tdttoken: '75293e320524bb2be0a5f14fa887e46f',
    mapConfig: null,
    isInitViewer: false,//三维场景是否初始化
    mapContainerId: '',//地图容器Id
    //集团、分公司信息
    groupProject: [],
    groupName: null,
    groupCodeValue: null,
    //项目级信息
    currentActive: null,//项目当前激活模块
    customToolList: [],//自定义工具激活集合
    currentTool: '',//当前激活工具
    propertiesViewValue: null,//属性查看选择值
    isShowPipeSpeedInfoCheck: false,//显示进度查看弹窗
    monitorTreeCheckHistory: null,//设备监测树历史记录
    currentDeviceShow: null,//当前查看设备
    isShowExcavationAnalysis: false,//显示开挖分析窗体
    //-->统计分析模块
    currentStatisticalActive: null
  }
}
const state = getDefaultState()
//检查是否存在已有数据方法
function some(arr, str) {
  const result = arr.some((item) => {
    if (item.name === str) {
      return true
    }
  })
  return result
}
function delOne(arr, str) {
  return arr.filter(item => item.name !== str)
}
const mutations = {
  SET_GISSOURCE: (state, payload) => {
    state.mapConfig = payload
  },
  SET_TOOL: (state, data) => {
    state.currentTool = data.name
    if (!some(state.customToolList, data.name)) {
      state.customToolList.push(data)
    } else {
      state.customToolList = state.customToolList.slice()
    }
  },
  DEL_TOOL: (state, data) => {
    state.customToolList = delOne(state.customToolList, data)
    if (state.customToolList.length > 0) {
      state.currentTool = state.customToolList[state.customToolList.length - 1].name
    }
  },
  DEL_ALLTOOL: (state) => {
    state.customToolList = []
  },
  RESET_ALL: (state) => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  initGISResource({ commit }) {
    return new Promise((resolve, reject) => {
      commit('SET_GISSOURCE', appconfig.bigScreenMapService);
      resolve(appconfig.bigScreenMapService);
    })
  },
  // 添加工具集
  changeToolList({ commit, state }, value) {
    commit('SET_TOOL', value)
  },
  //移除工具集
  removeSingleTool({ commit, state }, value) {
    commit('DEL_TOOL', value)
  },
  //移除所有工具
  removeAllTool({ commit, state }, value) {
    commit('DEL_ALLTOOL', value)
  },
  //重置state数据
  resetState({ commit, state }, value) {
    commit('RESET_ALL', value)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
