{"version":3,"file":"submissionReportDownload.mjs","names":["ConfigService","apiMutate","SUBMITTED_STATUS","tryParseJsonObject","value","parsed","JSON","parse","hasNonWhitespaceText","v","trim","length","formatDateForFilename","dateString","normalized","includes","replace","toUpperCase","endsWith","date","Date","Number","isNaN","getTime","toLocaleDateString","undefined","year","month","day","sanitizeFilenamePart","String","getSubmissionDisplayFormName","row","dataObj","formSubmission_Data","overrideName","FormTemplateOverrides","FormName","baseName","form_Name","getBrowserTimeZone","Intl","DateTimeFormat","resolvedOptions","timeZone","buildReportFilename","reportName","submissionOwner","formSubmission_UserDisplayName","isSubmitted","formSubmission_Status","submittedLabel","formSubmission_SubmittedOn","downloadSubmissionReport","submissionId","formSubmission_Id","Error","browserTimeZone","response","productV1ApiUrl","encodeURIComponent","method","responseType","blob","data","filename","blobUrl","window","URL","createObjectURL","link","document","createElement","href","download","body","appendChild","click","removeChild","revokeObjectURL"],"sources":["../../../../src/udp/pages/UdpQuestionFormOverviewPage/submissionReportDownload.js"],"sourcesContent":["import { ConfigService } from '../../../configService';\nimport { apiMutate } from '../../../utilities/useAxiosMutate';\n\n// Self-contained submission-PDF download helper for the Overview page's\n// Submissions tab. Mirrors the legacy UdpFormsSubmissionGrid \"Download Report\"\n// action but lives entirely here so the legacy grid stays untouched. The\n// backend endpoint (GET /UdpFormSubmission/{id}/report) renders the PDF\n// server-side via Siberix and already handles lookups, repeatable sections,\n// and file/signature rehydration.\n\nconst SUBMITTED_STATUS = 2;\n\nconst tryParseJsonObject = (value) => {\n  if (!value || typeof value !== 'string') return null;\n  try {\n    const parsed = JSON.parse(value);\n    return parsed && typeof parsed === 'object' ? parsed : null;\n  } catch {\n    return null;\n  }\n};\n\nconst hasNonWhitespaceText = (v) => typeof v === 'string' && v.trim().length > 0;\n\nconst formatDateForFilename = (dateString) => {\n  if (!dateString) return '';\n  let normalized = dateString;\n  if (typeof normalized === 'string' && normalized.includes(' ')) {\n    normalized = normalized.replace(' ', 'T');\n  }\n  if (typeof normalized === 'string' && !normalized.toUpperCase().endsWith('Z')) {\n    normalized = normalized + 'Z';\n  }\n  const date = new Date(normalized);\n  if (Number.isNaN(date.getTime())) return '';\n  return date.toLocaleDateString(undefined, {\n    year: 'numeric',\n    month: 'long',\n    day: '2-digit',\n  });\n};\n\nconst sanitizeFilenamePart = (value) =>\n  String(value ?? '')\n    // Windows file name chars: \\ / : * ? \" < > |\n    .replace(/[\\\\/:*?\"<>|]/g, '')\n    .replace(/\\s+/g, ' ')\n    .trim();\n\nconst getSubmissionDisplayFormName = (row) => {\n  const dataObj = tryParseJsonObject(row?.formSubmission_Data);\n  const overrideName = dataObj?.FormTemplateOverrides?.FormName;\n  const baseName = row?.form_Name;\n  if (hasNonWhitespaceText(overrideName)) return overrideName.trim();\n  if (hasNonWhitespaceText(baseName)) return baseName.trim();\n  return 'Untitled Form';\n};\n\nconst getBrowserTimeZone = () => {\n  try {\n    return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC';\n  } catch {\n    return 'UTC';\n  }\n};\n\nconst buildReportFilename = (row) => {\n  const reportName = getSubmissionDisplayFormName(row);\n  const submissionOwner = row?.formSubmission_UserDisplayName || 'Unknown User';\n  const isSubmitted = Number(row?.formSubmission_Status) === SUBMITTED_STATUS;\n  const submittedLabel = isSubmitted\n    ? formatDateForFilename(row?.formSubmission_SubmittedOn) || 'Submitted'\n    : 'Not Submitted';\n  return `${sanitizeFilenamePart(reportName)} - ${sanitizeFilenamePart(\n    submissionOwner\n  )} - ${sanitizeFilenamePart(submittedLabel)}.pdf`;\n};\n\n// Fetches the server-rendered PDF for a submission row and triggers a browser\n// download. Throws on failure so the caller can surface an error.\nexport const downloadSubmissionReport = async (row) => {\n  const submissionId = row?.formSubmission_Id;\n  if (!submissionId) throw new Error('Missing submission id');\n\n  const browserTimeZone = getBrowserTimeZone();\n  const response = await apiMutate(\n    ConfigService.productV1ApiUrl,\n    `udpFormSubmission/${submissionId}/report?timeZoneId=${encodeURIComponent(\n      browserTimeZone\n    )}&templateName=modern`,\n    { method: 'GET', responseType: 'blob' },\n    {}\n  );\n\n  const blob = response.data;\n  const filename = buildReportFilename(row);\n\n  const blobUrl = window.URL.createObjectURL(blob);\n  const link = document.createElement('a');\n  link.href = blobUrl;\n  link.download = filename;\n  document.body.appendChild(link);\n  link.click();\n  document.body.removeChild(link);\n  window.URL.revokeObjectURL(blobUrl);\n};\n"],"mappings":";;;AAUA,MAAME,mBAAmB;AAEzB,MAAMC,sBAAsBC,UAAU;AACpC,KAAI,CAACA,SAAS,OAAOA,UAAU,SAAU,QAAO;AAChD,KAAI;EACF,MAAMC,SAASC,KAAKC,MAAMH,MAAM;AAChC,SAAOC,UAAU,OAAOA,WAAW,WAAWA,SAAS;SACjD;AACN,SAAO;;;AAIX,MAAMG,wBAAwBC,MAAM,OAAOA,MAAM,YAAYA,EAAEC,MAAM,CAACC,SAAS;AAE/E,MAAMC,yBAAyBC,eAAe;AAC5C,KAAI,CAACA,WAAY,QAAO;CACxB,IAAIC,aAAaD;AACjB,KAAI,OAAOC,eAAe,YAAYA,WAAWC,SAAS,IAAI,CAC5DD,cAAaA,WAAWE,QAAQ,KAAK,IAAI;AAE3C,KAAI,OAAOF,eAAe,YAAY,CAACA,WAAWG,aAAa,CAACC,SAAS,IAAI,CAC3EJ,cAAaA,aAAa;CAE5B,MAAMK,OAAO,IAAIC,KAAKN,WAAW;AACjC,KAAIO,OAAOC,MAAMH,KAAKI,SAAS,CAAC,CAAE,QAAO;AACzC,QAAOJ,KAAKK,mBAAmBC,KAAAA,GAAW;EACxCC,MAAM;EACNC,OAAO;EACPC,KAAK;EACN,CAAC;;AAGJ,MAAMC,wBAAwBzB,UAC5B0B,OAAO1B,SAAS,GAAE,CAEfY,QAAQ,iBAAiB,GAAG,CAC5BA,QAAQ,QAAQ,IAAI,CACpBN,MAAM;AAEX,MAAMqB,gCAAgCC,QAAQ;CAE5C,MAAMG,eADUhC,mBAAmB6B,KAAKE,oBAAoB,EAC9BE,uBAAuBC;CACrD,MAAMC,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