1import{da as ye,db as je,r as g,d as te,af as xe,ag as X,j as t,K as r,cv as be,ai as V,T as F,bP as pe,dc as re,M as ie,df as ee,N as oe,bR as Se,bd as me,dj as De,ds as ge,a as we,u as Ie,a8 as ce,bU as Ue,i as ve,d0 as Z,ey as ke,ez as ze,$ as z,n as K,p as G,e as Fe,L as le}from"./index-ChIlw_lC.js";import{a as Pe}from"./queries-Z-fRPt8k.js";import{g as de,L as ue,a as Ce,u as Re,D as Te}from"./useDocumentLoading-kKjygGRE.js";import{I as Le}from"./File-CcypC1YC.js";import"./Clock-zNhrn7Do.js";const Ae=({sourceType:m,onDelete:u,onDeleteDocument:n,index:d,documentUUID:S})=>{var M,Q,Y,_,q;const{control:i,setValue:w}=ye(),e=je({control:i,name:`files.${d}`}),[x,R]=g.useState(!1),[T,P]=g.useState(0),[C,A]=g.useState(!1),[D,H]=g.useState(!1),{t:a}=te("analyzer"),o=xe(),s=X(o.breakpoints.down("md")),c=X(o.breakpoints.down("sm")),I=()=>{const N=!e.isScanned;w(`files.${d}.isScanned`,N),H(!0)},L=N=>{w(`files.${d}.language`,N),H(!0)},$=async()=>{if(!C)try{A(!0),n?await n(d):u(d)}catch(N){console.error("Failed to delete failed document stat:",N)}finally{A(!1)}},W=(e==null?void 0:e.status)==="processing"||x,U=W||(e==null?void 0:e.status)==="success",B=()=>{switch(e==null?void 0:e.status){case"success":return t.jsx(me,{size:s?20:24,color:"#4CAF50",weight:"fill"});case"failed":return t.jsx(Se,{size:s?20:24,color:"#F44336",weight:"fill"});case"processing":return t.jsx(ee,{size:s?20:24,color:"#2196F3",className:"spinning-icon"});default:return t.jsx(t.Fragment,{})}},p=()=>(e==null?void 0:e.status)==="success"?t.jsx(V,{title:a("successfullyUploaded"),arrow:!0,children:t.jsx(r,{sx:{display:"flex",alignItems:"center"},children:B()})}):(e==null?void 0:e.status)==="processing"||x?t.jsx(V,{title:a("processingDots"),arrow:!0,children:t.jsx(r,{sx:{display:"flex",alignItems:"center"},children:B()})}):(e==null?void 0:e.status)==="failed"?t.jsx(r,{sx:{display:"flex",alignItems:"center",gap:1},children:t.jsx(V,{title:a("uploadFailed"),arrow:!0,children:B()})}):t.jsx(V,{title:a("readyToUpload"),arrow:!0,children:t.jsx(r,{sx:{display:"flex",alignItems:"center"},children:B()})}),f=D||(e==null?void 0:e.status)!=="failed"?null:a("failedDocumentProcess");if(!e||!e.file)return null;const O=(Q=(M=e==null?void 0:e.failedDocument)==null?void 0:M.attachment)!=null&&Q.contentType?de(e.failedDocument.attachment.contentType):de((Y=e==null?void 0:e.file)==null?void 0:Y.type);return t.jsx(t.Fragment,{children:t.jsxs(r,{sx:{marginTop:"10px",bgcolor:"background.paper",border:"1px solid",borderColor:(e==null?void 0:e.status)==="success"?"success.main":(e==null?void 0:e.status)==="failed"&&!D?"error.main":D?"success.main":"divider",borderRadius:"12px",overflow:"hidden",transition:"all 0.3s ease",opacity:W?.9:1,"&:hover":{boxShadow:o.shadows[2]}},children:[W&&t.jsx(be,{variant:T>0?"determinate":"indeterminate",value:T,sx:{height:3,bgcolor:"transparent","& .MuiLinearProgress-bar":{bgcolor:"primary.main"}}}),t.jsxs(r,{sx:{p:s?c?"12px":"14px":"16px",display:"flex",flexDirection:s?"column":"row",gap:s?"12px":"16px"},children:[t.jsxs(r,{sx:{display:"flex",alignItems:"center",gap:s?"10px":"12px",flex:1,minWidth:0},children:[t.jsx(r,{sx:{bgcolor:(e==null?void 0:e.status)==="success"?"success.50":(e==null?void 0:e.status)==="failed"&&!D?"error.50":D?"success.50":(e==null?void 0:e.status)==="processing"?"primary.50":"action.hover",borderRadius:"8px",width:s?"40px":"48px",height:s?"40px":"48px",display:"flex",alignItems:"center",justifyContent:"center",flexShrink:0,position:"relative"},children:O==="image"?t.jsx("img",{src:URL.createObjectURL(e==null?void 0:e.file),alt:"Fix this",style:{width:"40px",height:"40px"}}):t.jsx(Le,{size:s?16:20})}),t.jsxs(r,{sx:{minWidth:0,flex:1},children:[t.jsx(V,{title:e.file.name,arrow:!0,children:t.jsx(F,{variant:s?"body2":"subtitle1",sx:{fontWeight:600,maxWidth:"200px",fontSize:s?c?"13px":"14px":"16px",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",color:(e==null?void 0:e.status)==="success"?"success.main":(e==null?void 0:e.status)==="failed"&&!D?"error.main":"text.primary"},children:e.file.name})}),f&&t.jsxs(r,{sx:{display:"flex",alignItems:"center",gap:.5,mt:.5},children:[t.jsx(pe,{size:12,color:o.palette.error.main}),t.jsx(F,{variant:"caption",color:"error",sx:{fontSize:s?"10px":"11px",fontWeight:500},children:f})]}),D&&!f&&t.jsx(r,{sx:{display:"flex",alignItems:"center",gap:.5,mt:.5},children:t.jsx(F,{variant:"caption",color:"success.main",sx:{fontSize:s?"10px":"11px",fontWeight:500},children:a("settingsUpdated")})}),(e==null?void 0:e.status)==="success"&&t.jsx(r,{sx:{display:"flex",alignItems:"center",gap:.5,mt:.5},children:t.jsx(F,{variant:"caption",color:"success.main",sx:{fontSize:s?"10px":"11px",fontWeight:500},children:a("successfullyUploaded")})}),W&&t.jsx(F,{variant:"caption",color:"primary",sx:{fontSize:s?"10px":"11px",fontWeight:500,mt:.5,display:"block"},children:T>0?`${a("processingDots")} ${T}%`:a("processingDots")})]})]}),!s&&t.jsxs(r,{sx:{display:"flex",alignItems:"center",gap:"12px",flexShrink:0},children:[t.jsx(r,{sx:{minWidth:"200px"},children:t.jsx(ue,{lang:e.language||[],changeLanguage:L,disabled:U,index:d})}),(O==="pdf"||((_=e==null?void 0:e.file)==null?void 0:_.type)==="application/pdf")&&t.jsxs(r,{sx:{display:"flex",alignItems:"center",gap:.5,minWidth:"fit-content",whiteSpace:"nowrap"},children:[t.jsx(re,{checked:(e==null?void 0:e.isScanned)||!1,onChange:I,disabled:e.file.type!=="application/pdf"||U,size:"small",sx:{padding:"4px",color:"primary.main","&.Mui-checked":{color:"primary.main"}}}),t.jsx(F,{variant:"body2",sx:{fontSize:"13px",color:U?"text.disabled":"text.secondary",lineHeight:1.2},children:a("scannedPdfFile")})]}),(e==null?void 0:e.status)&&(e==null?void 0:e.status)!=="idle"&&t.jsx(p,{}),t.jsx(V,{title:a("deleteFile"),arrow:!0,children:t.jsx(ie,{size:"small",color:"error",onClick:$,disabled:C||W,sx:{bgcolor:"error.50","&:hover":{bgcolor:"error.100"}
1,"&:disabled":{bgcolor:"action.disabledBackground"}},children:C?t.jsx(ee,{size:16,className:"spinning-icon"}):t.jsx(oe,{size:16})})})]}),s&&t.jsxs(r,{sx:{display:"flex",flexDirection:"column",gap:"10px",width:"100%"},children:[t.jsxs(r,{sx:{display:"flex",alignItems:"center",gap:"8px",width:"100%"},children:[t.jsx(r,{sx:{flex:1,minWidth:0},children:t.jsx(ue,{lang:e.language||[],changeLanguage:L,disabled:U,index:d})}),(O==="pdf"||((q=e==null?void 0:e.file)==null?void 0:q.type)==="application/pdf")&&t.jsxs(r,{sx:{display:"flex",alignItems:"center",gap:.5,flexShrink:0},children:[t.jsx(re,{checked:(e==null?void 0:e.isScanned)||!1,onChange:I,disabled:e.file.type!=="application/pdf"||U,size:"small",sx:{padding:"2px",color:"primary.main","&.Mui-checked":{color:"primary.main"}}}),t.jsx(F,{variant:"caption",sx:{fontSize:"11px",color:U?"text.disabled":"text.secondary",lineHeight:1.2},children:a("scannedPdfFile")})]})]}),t.jsxs(r,{sx:{display:"flex",alignItems:"center",justifyContent:"space-between",gap:"8px"},children:[t.jsx(r,{sx:{flex:1},children:(e==null?void 0:e.status)&&(e==null?void 0:e.status)!=="idle"&&t.jsx(p,{})}),t.jsx(V,{title:a("deleteFile"),arrow:!0,children:t.jsx(ie,{size:"small",color:"error",onClick:$,disabled:C||W,sx:{bgcolor:"error.50","&:hover":{bgcolor:"error.100"},"&:disabled":{bgcolor:"action.disabledBackground"}},children:C?t.jsx(ee,{size:16,className:"spinning-icon"}):t.jsx(oe,{size:16})})})]})]})]})]})})};function J(m){var u,n;return((n=(u=m.failedDocument)==null?void 0:u.documentInfo)==null?void 0:n.uuid)||""}function $e(m=[],u=[]){if(m.length!==u.length)return!1;for(let n=0;n<m.length;n+=1)if(m[n]!==u[n])return!1;return!0}function We(m,u){if(m.length!==u.length)return!1;for(let n=0;n<m.length;n+=1){const d=m[n],S=u[n];if(d.status!==S.status||J(d)!==J(S)||!!d.isScanned!=!!S.isScanned||!$e(d.language||[],S.language||[]))return!1}return!0}const Ee=({failedDocuments:m,sourceType:u,methods:n,documentUUID:d,onDeleteDocument:S,onRetryEnd:i,onRetrySuccess:w,isDocumentRetrying:e})=>{const x=Ce(m),{handleSubmit:R,control:T}=n,{fields:P,remove:C}=De({control:T,name:"files"}),A=a=>{C(a)},D=async()=>{};return g.useEffect(()=>{const a=n.getValues("files")||[],o=new Map(a.map(c=>[J(c),c])),s=(x||[]).map(c=>{const I=J(c),L=o.get(I),$=!!(I&&(e!=null&&e(I)));return L?{...c,language:L.language||c.language,isScanned:!!L.isScanned,pageRotations:L.pageRotations,status:$?"processing":c.status,error:$?void 0:c.error}:$?{...c,status:"processing",error:void 0}:c});We(a,s)||n.reset({files:s})},[x,e,n]),x&&x.length>0||P&&P.length>0?t.jsx(ge,{methods:n,onSubmit:R(D),children:P==null?void 0:P.map((a,o)=>t.jsx(Ae,{sourceType:u,onDelete:A,onDeleteDocument:S,index:o,documentUUID:d},a.id))}):null};function He(){const[m]=we(),u=Ie(),{t:n}=te("analyzer"),{t:d}=te("common"),S=xe(),i=X(S.breakpoints.down("md")),w=X(S.breakpoints.down("sm")),e=ce(m.get("documentUUID")),x=ce(m.get("sourceType"))||"ANALYZER",[R,T]=g.useState(!1),[P,C]=g.useState([]),{data:A,isLoading:D,error:H,dataUpdatedAt:a}=Pe(e,{refetchInterval:R?3e3:!1}),o=Ue(),s=ve({defaultValues:{files:[]}}),c=g.useCallback(()=>{o.invalidateQueries({queryKey:["ASYNC_DOCUMENT_LOADING",e]}),Z(o)},[e,o]),I=g.useCallback(()=>{o.invalidateQueries({queryKey:["FAILED_DOCUMENT_STATS",e]}),c()},[e,c,o]),L=g.useCallback(()=>{c()},[c]),{mutateAsync:$}=ke(L),{mutateAsync:W}=ze(I),[U,B]=g.useState(!1),p=Re(!!(e&&R),e||""),se=p&&(p.pendingDocument??0)>0,f=s.watch("files")||[],O=f==null?void 0:f.some(l=>l.status==="success"),M=(f==null?void 0:f.some(l=>l.status==="processing"))||se||P.length>0,Q=f==null?void 0:f.some(l=>l.status==="idle");g.useEffect(()=>{if(!R)return;const l=(p.pendingDocument??0)>0,v=(p.failedDocument??0)>0;!l&&(p.completed||v)&&(T(!1),C([]),I())},[p.completed,p.failedDocument,p.pendingDocument,I,R]),g.useEffect(()=>{R&&Z(o)},[a,o,R]);const Y=g.useCallback(l=>P.includes(l),[P]),_=()=>{u(x==="ANALYZER"?"/analyzer":"/drafter/create")},q=async l=>{if(O&&!Q){Z(o);const y=new URLSearchParams({is_ghost_mode:"false"}),j=new URLSearchParams({documentUUID:e||""}),h=`${G.analyzer}/${e}?${y.toString()}`,b=`${G.drafter.documentPath(e)}?${j.toString()}`;if(x==="ANALYZER"){u(h);return}else{window.location.href=b;return}}B(!0);let v=!1;const E=[];try{const y=[...l.files];for(let j=0;j<l.files.length;j++){const h=l.files[j];if(h.status!=="success"&&h.status==="failed"&&h.failedDocument){y[j]={...h,status:"processing"},s.setValue("files",[...y]);try{const b=h.failedDocument.documentInfo,k=h.failedDocument.attachment,ne=h.failedDocument.attachmentId??(k==null?void 0:k.id),ae=b==null?void 0:b.uuid;if(!(b!=null&&b.documentUUID)||!ae||ne==null||!(k!=null&&k.key)||!(k!=null&&k.bucketN
1ame))throw new Error("Missing failed document attachment metadata for retry");const fe={documentUUID:b.documentUUID,sourceType:x,isScanned:h.isScanned||!1,attachmentId:String(ne),attachmentKey:k.key,bucketName:k.bucketName,language:h.language||["AUTO"]};await $(fe),v=!0,E.push(ae),y[j]={...h,status:"processing",error:void 0},s.setValue("files",[...y])}catch(b){y[j]={...h,status:"failed",error:b},s.setValue("files",[...y])}}}v&&(C(j=>Array.from(new Set([...j,...E]))),T(!0))}finally{B(!1),v?await c():await I()}},N=async l=>{var y,j;const v=s.getValues(`files.${l}`),E=(j=(y=v==null?void 0:v.failedDocument)==null?void 0:y.documentInfo)==null?void 0:j.uuid;E&&(await W(E),s.setValue("files",s.getValues("files").filter((h,b)=>b!==l)))};if(H||!e)return t.jsxs(z,{sx:{display:"flex",flexDirection:"column",justifyContent:"center",alignItems:"center",height:"50vh",gap:2},children:[t.jsx(pe,{size:48,color:S.palette.error.main}),t.jsx(F,{variant:"h6",color:"error",children:n("failedToLoadFailedDocuments")}),t.jsx(K,{onClick:_,variant:"outlined",children:d("goBack")})]});const he=g.useCallback(()=>{Z(o);const l=new URLSearchParams({is_ghost_mode:"false"}),v=new URLSearchParams({documentUUID:e||""}),E=`${G.analyzer}/${e}?${l.toString()}`,y=`${G.drafter.documentPath(e)}?${v.toString()}`;x==="ANALYZER"?u(E):window.location.href=y},[e,u,o,x]);return t.jsxs(z,{sx:{maxHeight:"100vh",display:"flex",flexDirection:"column",bgcolor:"background.default"},children:[t.jsx(z,{sx:{display:"flex",alignItems:"center",justifyContent:"space-between",py:i?"12px":"16px",px:i?"12px":"16px",pb:i?"12px":"15.7px",minHeight:i?"64px":"77px",borderBottom:"1px solid var(--mui-palette-divider)",gap:"8px"},children:t.jsx(z,{sx:{display:"flex",alignItems:"center",gap:"8px",flex:1,minWidth:0},children:t.jsx(F,{variant:i?w?"h6":"h5":"h4",sx:{fontSize:i?w?"0.9rem":"1.1rem":"1.3rem",fontWeight:600,whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis"},children:n("failedDocuments")})})}),t.jsxs(z,{sx:{paddingX:i?w?2:3:4.5,pb:i?w?2:3:4,pt:i?2:3,flex:1,overflowY:"auto",WebkitOverflowScrolling:"touch",overflowX:"hidden"},children:[!D&&p&&((p.pendingDocument??0)>0||(p.failedDocument??0)>0||p.completed)&&t.jsx(z,{sx:{mb:3},children:t.jsx(Te,{documentPendingState:p,documentUUID:e,sourceType:x})}),D?t.jsx(z,{sx:{display:"flex",justifyContent:"center",alignItems:"center",minHeight:"300px"},children:t.jsx(Fe,{size:40})}):t.jsx(t.Fragment,{children:A&&A.length>0?t.jsxs(ge,{methods:s,onSubmit:s.handleSubmit(q),children:[t.jsx(Ee,{failedDocuments:A,sourceType:x,methods:s,documentUUID:e,onDeleteDocument:N,isDocumentRetrying:Y}),t.jsx(z,{sx:{mt:i?"24px":"32px",display:"flex",alignItems:"center",justifyContent:"flex-end",flexDirection:i?"column":"row",gap:i?"12px":"16px",width:"100%",px:i?2:0,py:i?2:0,...i&&{position:"fixed",bottom:"70px",bgcolor:"background.paper",borderTop:"1px solid",borderColor:"divider",boxShadow:"0 -4px 12px rgba(0, 0, 0, 0.1)",zIndex:10,left:0,mt:"auto"}},children:i?t.jsx(z,{sx:{display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",width:"100%",gap:"12px"},children:t.jsx(le,{variant:"contained",color:"primary",type:"submit",disabled:U||M,loading:U||M,size:w?"medium":"large",sx:{width:"100%",minHeight:w?"42px":"46px",fontSize:w?"13px":"15px",fontWeight:600,borderRadius:"8px",gap:"20px"},children:n("ready")})}):t.jsxs(t.Fragment,{children:[t.jsx(K,{variant:"outlined",onClick:_,sx:{minWidth:"120px",fontWeight:600,borderRadius:"8px"},children:d("goBack")}),t.jsx(le,{variant:"contained",color:"primary",type:"submit",disabled:U||M,loading:U||M,sx:{fontWeight:600,borderRadius:"8px"},children:n("ready")})]})})]}):t.jsxs(z,{sx:{display:"flex",flexDirection:"column",justifyContent:"center",alignItems:"center",height:"50vh",gap:3},children:[t.jsx(me,{size:64,color:S.palette.success.main,weight:"fill"}),t.jsx(F,{variant:"h5",color:"success.main",fontWeight:600,textAlign:"center",children:n("allDocumentsCompleted")}),t.jsx(F,{variant:"body1",color:"text.secondary",textAlign:"center",sx:{maxWidth:"400px"},children:n("allDocumentsProcessedSuccessfully")}),t.jsxs(z,{sx:{display:"flex",gap:2,mt:2},children:[t.jsx(K,{onClick:_,variant:"outlined",size:"large",children:d("goBack")}),t.jsx(K,{onClick:()=>he(),variant:"contained",size:"large",color:"primary",children:n(x==="ANALYZER"?"continueToAnalyzer":"continueToDrafter")})]})]})})]})]})}export{He as FailedDocumentsPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.