import { computed, ref } from 'vue'

const uploadedPdfUrl = ref('')
const historyPdfUrl = ref('')
const activePdfSource = ref('upload')

export function usePdfPreviewState() {
  const pdfUrl = computed(() => {
    if (activePdfSource.value === 'history' && historyPdfUrl.value) {
      return historyPdfUrl.value
    }
    return uploadedPdfUrl.value || historyPdfUrl.value || ''
  })

  function setUploadedPdfUrl(url) {
    uploadedPdfUrl.value = url || ''
    if (url) activePdfSource.value = 'upload'
  }

  function setHistoryPdfUrl(url) {
    historyPdfUrl.value = url || ''
    if (url && !uploadedPdfUrl.value) activePdfSource.value = 'history'
  }

  function setActivePdfSource(source) {
    if (source === 'upload' || source === 'history') {
      activePdfSource.value = source
    }
  }

  return {
    pdfUrl,
    activePdfSource,
    setUploadedPdfUrl,
    setHistoryPdfUrl,
    setActivePdfSource
  }
}
