const tsb = {
  namespaced: true,
  state: {
    /** 当前操作的调试宝设备 */
    currentDevice: null,
    /** 本页签已打开过的设备卡片 */
    openedDevices: [],
    /** 是否已完成页签会话检查 */
    sessionReady: false,
    /** 新页签且未在本页签选择过设备 */
    isNewTabWithoutDevice: false,
    /** 各 cmdType 最新推送数据 */
    pageData: {},
    /** WebSocket 跳转时标记，页面初始化后清除 */
    initFromWs: {},
    /** 各设备当前子页面：home / tax 等 */
    devicePanelMap: {},
    /** 各设备页面表单缓存：key = deviceSn::panel */
    devicePageFormCache: {},
    /** 数据版本号，供页面 watch 实时更新 */
    pageDataVersion: 0,
    /** 液位仪各设备当前子页：main / query / passthrough */
    opwSubPageMap: {},
    /** 设备页签切换版本号，供子页面在切回时刷新 WS 数据 */
    deviceSwitchVersion: 0
  },
  mutations: {
    SET_CURRENT_DEVICE(state, device) {
      state.currentDevice = device || null
    },
    ADD_OPENED_DEVICE(state, device) {
      if (!device || device.deviceSn == null) {
        return
      }
      if (!state.openedDevices.some(d => d.deviceSn === device.deviceSn)) {
        state.openedDevices = [...state.openedDevices, device]
      }
    },
    REMOVE_OPENED_DEVICE(state, deviceSn) {
      if (deviceSn == null) {
        return
      }
      state.openedDevices = state.openedDevices.filter(d => d.deviceSn !== deviceSn)
      if (state.currentDevice && state.currentDevice.deviceSn === deviceSn) {
        state.currentDevice = state.openedDevices.length
          ? state.openedDevices[state.openedDevices.length - 1]
          : null
      }
    },
    SET_DEVICE_PANEL(state, { deviceSn, panel }) {
      if (deviceSn == null || !panel) {
        return
      }
      state.devicePanelMap = { ...state.devicePanelMap, [String(deviceSn)]: panel }
    },
    SET_DEVICE_PANEL_MAP(state, map) {
      state.devicePanelMap = map || {}
    },
    RESTORE_SESSION(state, session) {
      if (!session) {
        return
      }
      state.currentDevice = session.currentDevice || null
      state.openedDevices = session.openedDevices || []
      state.devicePanelMap = session.devicePanelMap || {}
      state.sessionReady = true
      state.isNewTabWithoutDevice = false
    },
    SET_SESSION_CHECKED(state, hasSession) {
      state.sessionReady = true
      state.isNewTabWithoutDevice = !hasSession
    },
    SET_DEVICE_PAGE_FORM(state, { key, data }) {
      if (!key) {
        return
      }
      state.devicePageFormCache = { ...state.devicePageFormCache, [key]: data }
    },
    CLEAR_DEVICE_PAGE_FORM(state, keyPrefix) {
      if (!keyPrefix) {
        return
      }
      const prefix = `${keyPrefix}::`
      const next = { ...state.devicePageFormCache }
      Object.keys(next).forEach((k) => {
        if (k.startsWith(prefix)) {
          delete next[k]
        }
      })
      state.devicePageFormCache = next
    },
    SET_WS_PAGE_DATA(state, { cmdType, data, navigate, deviceSn }) {
      const sn = deviceSn != null ? String(deviceSn) : null
      const key = sn != null ? `${sn}::${cmdType}` : cmdType
      state.pageData = { ...state.pageData, [key]: data }
      state.pageDataVersion += 1
      if (navigate) {
        state.initFromWs = { ...state.initFromWs, [key]: true }
      }
    },
    SET_OPW_SUB_PAGE(state, { deviceSn, subPage }) {
      if (deviceSn == null || !subPage) {
        return
      }
      state.opwSubPageMap = { ...state.opwSubPageMap, [String(deviceSn)]: subPage }
    },
    NOTIFY_DEVICE_SWITCHED(state) {
      state.deviceSwitchVersion += 1
    },
    CLEAR_WS_INIT(state, payload) {
      const cmdType = payload && payload.cmdType ? payload.cmdType : payload
      const deviceSn = payload && payload.deviceSn != null ? String(payload.deviceSn) : null
      const key = deviceSn != null ? `${deviceSn}::${cmdType}` : cmdType
      const initFromWs = { ...state.initFromWs }
      delete initFromWs[key]
      state.initFromWs = initFromWs
    },
    RESET_TSB(state) {
      state.currentDevice = null
      state.openedDevices = []
      state.sessionReady = false
      state.isNewTabWithoutDevice = false
      state.pageData = {}
      state.initFromWs = {}
      state.devicePanelMap = {}
      state.devicePageFormCache = {}
      state.pageDataVersion = 0
      state.deviceSwitchVersion = 0
      state.opwSubPageMap = {}
    }
  }
}

export default tsb
