import { reactive } from "vue"

/** 维护其他系统过来的特殊字段  这里用来记录，以免其他系统传过来的参数混乱 不好维护 */
export type queryParamsType = {
  hideFullScreen: boolean
  fromType: "PLATFORM" | "TEACHER" | "CLASS"
  instrumentId: string
  lessonCoursewareKnowledgeId: string
  pptResourcesType: "PLATFORM" | "TEACHER" | "CLASS"
}
const queryParams = reactive<queryParamsType>({
  hideFullScreen: false, // 隐藏预览时候的全屏按钮
  fromType: "TEACHER", // 用于区分老师端 平台，或者学生端
  lessonCoursewareKnowledgeId: "", // 课件id 老师端带过来查相关课件
  instrumentId: "", // 传过来的乐器值，用于ppt里面云教练带什么乐器
  pptResourcesType: "TEACHER" // ppt查看资源类型 用来查询ppt数据  学生端有多种场景资源数据
})

export function initQueryParams() {
  const paramsFromUrl = getParamsFromUrl()
  if (paramsFromUrl) {
    const hideFullScreen = !!paramsFromUrl.get("hideFullScreen")
    const fromType = paramsFromUrl.get("fromType")
    const pptResourcesType = paramsFromUrl.get("pptResourcesType")
    const lessonCoursewareKnowledgeId = paramsFromUrl.get("lessonCoursewareKnowledgeId")
    const instrumentId = paramsFromUrl.get("instrumentId")
    queryParams.hideFullScreen = hideFullScreen
    fromType && (queryParams.fromType = fromType as any)
    pptResourcesType && (queryParams.pptResourcesType = pptResourcesType as any)
    lessonCoursewareKnowledgeId && (queryParams.lessonCoursewareKnowledgeId = lessonCoursewareKnowledgeId as any)
    instrumentId && (queryParams.instrumentId = instrumentId as any)
  }
  console.log(queryParams, "携带参数")
}

function getParamsFromUrl() {
  const fullUrl = window.location.href
  const queryIndex = fullUrl.indexOf("?")
  if (queryIndex === -1) return undefined
  const queryString = fullUrl.slice(queryIndex + 1)
  const params = new URLSearchParams(queryString)
  return params
}
export default queryParams
