/**
 * cmdType 与前端路由映射（设备推送页面跳转）
 */
export const TSB_CMD_ROUTE_MAP = {
  'common:tax': '/app-common/tax',
  'common:opw': '/app-common/opw',
  'common:opw:query': '/app-common/opw',
  'common:opw:passthrough': '/app-common/opw'
}

/** 设备工作区固定路由（切换设备不改地址） */
export const TSB_WORKSPACE_ROUTE = '/tsb/workspace'

import { OPW_CMD_SUB_PAGE } from '@/utils/tsbOpwConfig'

/**
 * 根据 cmdType 获取路由路径
 * @param {string} cmdType
 * @param {number|string} [deviceSn] 有设备上下文时进入工作区
 */
export function getRouteByCmdType(cmdType, deviceSn) {
  const base = TSB_CMD_ROUTE_MAP[cmdType]
  if (!base) {
    return null
  }
  if (deviceSn == null || deviceSn === '') {
    return base
  }
  if (cmdType === 'common:tax' || cmdType === 'common:opw'
    || cmdType === 'common:opw:query' || cmdType === 'common:opw:passthrough') {
    return TSB_WORKSPACE_ROUTE
  }
  return base
}

/**
 * 是否处于某 cmdType 对应页面
 * @param {string} path 当前路由
 * @param {string} cmdType
 * @param {number|string} [deviceSn]
 * @param {string} [currentPanel] 工作区子页面
 */
export function isOnCmdRoute(path, cmdType, deviceSn, currentPanel) {
  const legacy = TSB_CMD_ROUTE_MAP[cmdType]
  if (path === legacy) {
    return true
  }
  if (cmdType === 'common:tax' && path === TSB_WORKSPACE_ROUTE) {
    return currentPanel === 'tax'
  }
  return false
}

/**
 * 液位仪子页是否匹配 cmdType
 */
export function isOnOpwSubPage(path, cmdType, currentPanel, opwSubPage) {
  if (path !== TSB_WORKSPACE_ROUTE || currentPanel !== 'opw') {
    return false
  }
  const expected = OPW_CMD_SUB_PAGE[cmdType]
  if (!expected) {
    return false
  }
  return opwSubPage === expected
}
