{"version":3,"file":"usePlayerLogicProgress-F048w32h.mjs","names":[],"sources":["../src/components/composable/player/usePlayerLogicProgress.ts"],"sourcesContent":["import { usePlayerStore } from \"../../../stores/PlayerStore\";\nimport { useAuthStore } from \"../../../stores/AuthStore\";\nimport { computed, onMounted, Ref, ref, watch} from 'vue';\nimport classicApi from \"../../../api/classicApi\";\n\nexport const usePlayerLogicProgress = ()=>{\n  const listenTime= ref(0);\n  const notListenTime= ref(0);\n  const lastSend= ref(0);\n  const downloadId: Ref<string | null>= ref(null);\n  const urlLiveSent= ref(false);\n\n  const playerStore = usePlayerStore();\n  const authStore = useAuthStore();\n\n  const intervalToSend = computed(() => { \n    if(lastSend.value < 180){\n      return 10;\n    }\n    if(lastSend.value < 1800){\n      return 30;\n    }\n    return 60;\n  });\n\n  watch(listenTime, async (newVal) => {\n    if (\n      (playerStore.playerRadio && !playerStore.playerPodcast && !playerStore.playerLive) ||\n      !downloadId.value ||\n      newVal - lastSend.value < intervalToSend.value\n    ) {\n      return;\n    }\n    lastSend.value = newVal;\n    await sendListeningProgress(newVal);\n  });\n\n  watch(()=>playerStore.playerSeekTime, async () => {\n    if (undefined===playerStore.playerSeekTime) {\n      return;\n    }\n    if (playerStore.playerPodcast || playerStore.playerLive) {\n      notListenTime.value = playerStore.playerSeekTime - listenTime.value;\n    }\n    if(playerStore.playerVideo){\n      return;\n    }\n    const audioPlayer: HTMLAudioElement | null = document.querySelector(\"#audio-player\");\n    if (!audioPlayer) {\n      return;\n    }\n    audioPlayer.currentTime = playerStore.playerSeekTime;\n  });\n\n\n\n  onMounted(() => {\n      window.addEventListener(\"beforeunload\", endListeningProgress);\n  })\n\n  async function initLiveDownloadId() {\n    if (!playerStore.playerLive || downloadId.value) {\n      return;\n    }\n    try {\n      const mediaType = playerStore.playerVideo ? \"VIDEO\":\"AUDIO\";\n      const downloadIdFetched = await classicApi.fetchData<string | null>({\n        api:0,\n        path: \"podcast/download/live2/\" + playerStore.playerLive.podcastId,\n        parameters:{\n          mediaType: mediaType,\n          origin: \"octopus\",\n          distributorId: authStore.authOrgaId,\n        },\n      });\n      setDownloadId(downloadIdFetched);\n    } catch(e) {\n      downloadId.value = null;\n      console.error(\"ERROR downloadId\", e);\n    }\n  }\n\n  /**\n   * @param currentTime: Temps en seconds\n   */\n  function onTimeUpdateProgress(currentTime: number): void {\n    if (!downloadId.value) {\n      return;\n    }\n    if (\n      playerStore.playerLive &&\n      0 === listenTime.value &&\n      0 !== currentTime\n    ) {\n      notListenTime.value = currentTime;\n      listenTime.value = 1;\n    } else {\n      const newListenTime = currentTime - notListenTime.value;\n      const diffTime = newListenTime - listenTime.value;\n      if(diffTime > 0 && diffTime<1) {\n        listenTime.value = newListenTime;\n      }\n    }\n  }\n\n  function setDownloadId(newValue: string | null): void {\n    endListeningProgress();\n    downloadId.value = newValue;\n  }\n\n  async function endListeningProgress(): Promise<void> {\n    if (!downloadId.value) {\n      return;\n    }\n    await sendListeningProgress(listenTime.value);\n    downloadId.value = null;\n    notListenTime.value = 0;\n    lastSend.value = 0;\n    listenTime.value = 0;\n    urlLiveSent.value = false;\n    playerStore.playerUpdatePlayerHlsUrl(undefined);\n  }\n\n  async function sendListeningProgress(listenTime:number){\n    let paramUrlLive= \"\";\n    if(!urlLiveSent.value && playerStore.playerHlsUrl){\n      paramUrlLive=\"&url=\"+encodeURI(playerStore.playerHlsUrl);\n      urlLiveSent.value = true;\n    }\n\n    // Send listening updates if listening lasted more than 30 seconds\n    try {\n      await classicApi.putData<string | null>({\n        api: 0,\n        path:\"podcast/listen/\" +downloadId.value +\"?seconds=\" +Math.round(listenTime)+paramUrlLive,\n        isNotAuth:true\n      });\n    } catch(e) {\n      //Do nothing\n      console.error(e);\n    }\n  }\n\n  return {\n    listenTime,\n    downloadId,\n    initLiveDownloadId,\n    setDownloadId,\n    onTimeUpdateProgress,\n    endListeningProgress\n  }\n}\n"],"mappings":";;;;AAKA,IAAa,+BAA6B;CACxC,MAAM,aAAY,IAAI,CAAC;CACvB,MAAM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