import { openOcrTextInNewTab } from '../utils/ocr'; const asyncFalse = async () => false; const resolveDocumentDownloadHref = (document, resolveApiPath) => { if (!document || typeof resolveApiPath !== 'function') { return null; } const downloadPath = document.current_version?.download_path; if (!downloadPath) { return null; } return resolveApiPath(downloadPath); }; const hasDocumentOcrAsset = (document, getDocumentAsset) => { if (!document || typeof getDocumentAsset !== 'function') { return false; } return Boolean(getDocumentAsset(document, 'ocr-text')); }; export const createDocumentActionState = ({ document, resolveApiPath, ensurePreviewData, ensureAssetUrl, getDocumentAsset, notifyApiError, ocrErrorMessage = 'Unable to open OCR text.', }) => { if (!document) { return { downloadHref: null, hasOcr: false, openOcr: asyncFalse, }; } const downloadHref = resolveDocumentDownloadHref(document, resolveApiPath); const hasOcr = hasDocumentOcrAsset(document, getDocumentAsset); const openOcr = hasOcr ? async () => { try { const success = await openOcrTextInNewTab({ document, ensurePreviewData, getDocumentAsset, ensureAssetUrl, }); if (!success && typeof notifyApiError === 'function') { notifyApiError(new Error('OCR text URL unavailable.'), ocrErrorMessage); } return success; } catch (error) { if (typeof notifyApiError === 'function') { notifyApiError(error, ocrErrorMessage); } throw error; } } : asyncFalse; return { downloadHref, hasOcr, openOcr, }; }; export const documentActionsTestExports = { resolveDocumentDownloadHref, hasDocumentOcrAsset, };