1import{j as t,r as f}from"./vendor-motion-BHKvfdub.js";import{v as y,as as S,Y as R,aF as q,p as H,B as E,u as F,bB as Y,bC as Q,bD as Z,bE as X,bF as J,bG as ee,bH as te,bI as ae}from"./index-BDt3UX3X.js";import{e as se,f as ie}from"./useCrmAttachments-tOhG1apK.js";import{g as ne,A as re}from"./AttachmentPreviewDialog-C6jjNpRn.js";import{B as oe}from"./badge-D5ubbIAJ.js";import{f as B}from"./date-BUUsho9b.js";import{c as I}from"./current-lang-DxDpOKRM.js";import{u as ce}from"./useTranslator-Trz3OFF4.js";import"./user-realtime-context-C8241BXR.js";import{a as P,B as le,d as me}from"./attachment-utils-BHmtrEVN.js";import{I as de}from"./image--Uyq06Ge.js";import{F as ue}from"./file-text-PtNy_3tm.js";import{E as fe}from"./eye-DWzH4NQZ.js";import{D as he}from"./download-B4HT65Hc.js";import{T as L}from"./trash-2-Ehlv2s2v.js";import{u as pe}from"./useConfirm-Be-6S_Kx.js";import{u as ge}from"./useAttachmentDropZone-qQqGwbp8.js";import{u as _e}from"./useEntityQuery-ButkGhoz.js";import{u as xe}from"./useEntityMutation-C2P9JrCh.js";import{U as O}from"./upload-DootJoDL.js";import"./vendor-recharts-DrQ74lLO.js";import"./vendor-supabase-DXfLReh3.js";import"./sanitize-key-Dukf4eDf.js";import"./dialog-B50exQqv.js";import"./resumable-upload-D4EkyPJ4.js";import"./index-DPk8kkfe.js";import"./useMutation-CBeKYIhq.js";function ve(e){return _e(P(e??""),async()=>{if(!e)return[];const{data:r,error:i}=await y.from("v_deal_documents_unified").select("row_id, attachment_id, deal_id, source_type, source_id, source_parent_id, file_name, file_path, file_size, mime_type, created_at, uploaded_by, storage_bucket, version, is_current").eq("deal_id",e).order("created_at",{ascending:!1});if(i)throw i;const s=r??[],m=s.filter(o=>o.storage_bucket==="crm-attachments").map(o=>o.attachment_id);if(m.length>0){const{data:o}=await y.from("crm_attachments").select("id, display_name, ai_description, ai_status, ai_lang, email_message_id, email_subject, email_from, email_from_name, email_received_at").in("id",m),_=new Map((o??[]).map(l=>[l.id,l]));for(const l of s){const d=_.get(l.attachment_id);if(d){const u=d;l.display_name=d.display_name,l.ai_description=d.ai_description,l.ai_status=d.ai_status,l.ai_lang=d.ai_lang,l.email_message_id=u.email_message_id??null,l.email_subject=u.email_subject??null,l.email_from=u.email_from??null,l.email_from_name=u.email_from_name??null,l.email_received_at=u.email_received_at??null}}}return s},{enabled:!!e})}function be(e){return xe(async r=>{await y.storage.from(le).remove([r.filePath]);const{error:i}=await y.from("crm_attachments").delete().eq("id",r.attachmentId);if(i)throw i},{invalidate:[P(e)],successMessage:"Datei gelöscht",errorMessage:"Datei konnte nicht gelöscht werden"})}function je(e){return e?e<1024?`${e} B`:e<1048576?`${(e/1024).toFixed(0)} KB`:`${(e/1048576).toFixed(1)} MB`:""}function we(e){return e.display_name&&e.display_name.trim()||e.file_name}function ye(e){return!!e&&e.startsWith("image/")}function M({active:e,label:r,count:i,onClick:s}){return t.jsxs("button",{type:"button",onClick:s,className:`text-xs px-2.5 py-1 rounded-full border transition-colors ${e?"bg-primary text-primary-foreground border-primary":"bg-background hover:bg-muted border-border text-foreground"}`,children:[r," · ",i]})}function Ne({doc:e,signedUrl:r,onPreview:i,onDelete:s,deleteDisabledReason:m}){const{t:o}=S(),_=we(e),l=_!==e.file_name,d=ye(e.mime_type),N=e.source_type!=="deal"?o(`attachments.originBadge.${e.source_type}`,{date:B(e.created_at)}):null,h=I(),x=!!e.ai_description&&e.ai_lang!=null&&e.ai_lang!==h,A=ce(),[C,v]=f.useState(null);f.useEffect(()=>{if(v(null),!x||!e.ai_description)return;let p=!1;return A.mutateAsync({text:e.ai_description,target_lang:h,source_lang:e.ai_lang??"auto",tone:"concise",context:"internal",use_glossary:!0,log_history:!1}).then(D=>{p||v(D.translated)}).catch(()=>{}),()=>{p=!0}},[e.ai_description,e.ai_lang,h]);const $=async()=>{try{e.storage_bucket==="crm-attachments"?await me(e.file_path,e.file_name):F.info(o("attachments.download"))}catch{F.error(o("common.errorDownloading","Download fehlgeschlagen"))}},b=!!i,z=p=>
1{i&&(p.key==="Enter"||p.key===" ")&&(p.preventDefault(),i())},j=p=>D=>{D.stopPropagation(),p()};return t.jsxs("div",{className:`group flex items-start gap-2 rounded-md border bg-background p-2 transition-colors ${b?"cursor-pointer hover:bg-muted/50":"hover:bg-muted/30"}`,role:b?"button":void 0,tabIndex:b?0:void 0,onClick:b?i:void 0,onKeyDown:z,title:b?o("attachments.preview","Vorschau"):void 0,children:[t.jsx("div",{className:`shrink-0 mt-0.5 rounded flex items-center justify-center bg-muted overflow-hidden ${d&&r?"h-16 w-16":"h-9 w-9"}`,children:d&&r?t.jsx("img",{src:r,alt:_,className:"h-full w-full object-cover"}):d?t.jsx(de,{className:"h-4 w-4 text-info"}):t.jsx(ue,{className:"h-4 w-4 text-muted-foreground"})}),t.jsxs("div",{className:"min-w-0 flex-1",children:[t.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[t.jsx("p",{className:"text-sm font-medium truncate",children:_}),N&&t.jsx(oe,{variant:"secondary",className:"text-[10px] font-normal",children:N})]}),t.jsxs("div",{className:"flex flex-wrap items-center gap-x-2 gap-y-0 text-[10px] text-muted-foreground leading-tight",children:[l&&t.jsx("span",{className:"truncate max-w-[40ch]",title:e.file_name,children:e.file_name}),t.jsx("span",{children:je(e.file_size)}),t.jsx("span",{children:"·"}),t.jsx("span",{children:B(e.created_at)}),e.ai_status==="pending"&&t.jsxs("span",{className:"inline-flex items-center gap-1 text-primary",children:[t.jsx(R,{className:"h-2.5 w-2.5 animate-spin"})," ",o("attachments.aiAnalyzing")]})]}),e.ai_status==="done"&&e.ai_description&&t.jsxs("p",{className:"text-xs text-muted-foreground leading-snug line-clamp-2 mt-0.5 flex items-start gap-1",children:[t.jsx(q,{className:"h-2.5 w-2.5 mt-0.5 text-primary shrink-0"}),t.jsx("span",{children:e.ai_description})]}),(e.email_subject||e.email_from)&&t.jsxs("p",{className:"mt-0.5 flex items-start gap-1 text-[10px] leading-snug text-muted-foreground",children:[t.jsx(H,{className:"mt-0.5 h-2.5 w-2.5 shrink-0 text-primary"}),t.jsxs("span",{className:"truncate",children:[o("attachments.fromEmail",{defaultValue:"Aus E-Mail"}),":"," ",e.email_subject||o("attachments.noSubject",{defaultValue:"Ohne Betreff"}),e.email_from_name||e.email_from?` · ${e.email_from_name||e.email_from}`:"",e.email_received_at?` · ${B(e.email_received_at)}`:""]})]})]}),t.jsxs("div",{className:"flex items-center gap-0.5",children:[i&&t.jsx(E,{variant:"ghost",size:"icon",className:"h-7 w-7",onClick:j(i),title:o("attachments.preview","Vorschau"),children:t.jsx(fe,{className:"h-3.5 w-3.5"})}),t.jsx(E,{variant:"ghost",size:"icon",className:"h-7 w-7",onClick:j($),title:o("attachments.download"),children:t.jsx(he,{className:"h-3.5 w-3.5"})}),s&&t.jsx(E,{variant:"ghost",size:"icon",className:"h-7 w-7 text-destructive hover:text-destructive hover:bg-destructive/10",onClick:j(s),title:o("common.delete","Löschen"),children:t.jsx(L,{className:"h-3.5 w-3.5"})}),!s&&m&&t.jsx(E,{variant:"ghost",size:"icon",className:"h-7 w-7 text-muted-foreground/50 cursor-not-allowed",disabled:!0,onClick:j(()=>{}),title:m,children:t.jsx(L,{className:"h-3.5 w-3.5"})})]})]})}function De(e){const r=f.useRef(new Set);f.useEffect(()=>{const i=I(),s=e.filter(m=>m.storage_bucket==="crm-attachments"&&(m.ai_status==null||m.ai_status==="failed")&&!r.current.has(m.attachment_id));for(const m of s)r.current.add(m.attachment_id),y.from("crm_attachments").update({ai_status:"pending"}).eq("id",m.attachment_id).then(()=>{y.functions.invoke("attachment-ai-describe",{body:{attachment_id:m.attachment_id,lang:i}}).catch(()=>{})})},[e])}function ke({pendingDelete:e,onOpenChange:r,onConfirm:i}){const{t:s}=S();return t.jsx(Y,{open:!!e,onOpenChange:r,children:t.jsxs(Q,{children:[t.jsxs(Z,{children:[t.jsx(X,{children:s("attachments.deleteConfirmTitle","Datei löschen?")}),t.jsx(J,{children:s("attachments.deleteConfirmDescription","â{{name}}â wird unwiderruflich entfernt.",{name:e?e.display_name||e.file_name:""})})]}),t.jsxs(ee,{children:[t.jsx(te,{children:s("common.cancel","Abbrechen")}),t.jsx(ae,{className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",onClick:()=>{e&&i(e)},children:s("common.delete","Löschen")})]})]})})}const Ae=["pdf","spreadsheet","office","image","cad","archive","media","other"],Ce={pdf:"pdf",xls:"spreadsheet",xlsx:"spreadsheet",xlsm:"spreadsheet",ods:"spreadsheet",csv:"spreadsheet",doc:"office",docx:"office",rtf:"office",odt:"office",txt:"office",ppt:"office",pptx:"office",odp:"office",jpg:"image",jpeg:"image",png:"image",webp:"image",heic:"image",gif:"image",tif:"image",tiff:"image",bmp:"image",svg:"image",z
1ip:"archive","7z":"archive",rar:"archive",tar:"archive",gz:"archive",tgz:"archive",dwg:"cad",dxf:"cad",step:"cad",stp:"cad",igs:"cad",iges:"cad",ai:"cad",eps:"cad",psd:"cad",mp4:"media",mov:"media",avi:"media",mp3:"media",wav:"media",m4a:"media"};function Ee(e){const r=e.lastIndexOf(".");return r<0?"":e.slice(r+1).toLowerCase()}function $e(e,r){const i=Ce[Ee(e)];if(i)return i;const s=(r??"").toLowerCase();return s==="application/pdf"?"pdf":s.startsWith("image/")?"image":s.startsWith("video/")||s.startsWith("audio/")?"media":s.includes("spreadsheet")||s.includes("excel")?"spreadsheet":s.includes("word")||s.includes("presentation")||s.startsWith("text/")?"office":s.includes("zip")||s.includes("compressed")||s.includes("tar")?"archive":"other"}const ze={pdf:"PDF-Dokumente",spreadsheet:"Tabellen",office:"Office-Dateien",image:"Bilder",cad:"Zeichnungen und Entwürfe",archive:"Archive",media:"Video und Audio",other:"Sonstige Dateien"},Te=["deal","note","ticket","meeting","task"];function Be(e){return!!e&&e.startsWith("image/")}function ct({dealId:e}){const r=pe(),{t:i}=S(),{data:s=[],isLoading:m}=ve(e),o=se("deal",e),_=be(e),[l,d]=f.useState(null),[u,N]=f.useState(null),h=f.useRef(null);De(s);const[x,A]=f.useState("all"),C=f.useMemo(()=>{const a={all:s.length,deal:0,note:0,ticket:0,meeting:0,task:0};for(const c of s)a[c.source_type]=(a[c.source_type]??0)+1;return a},[s]),v=f.useMemo(()=>x==="all"?s:s.filter(a=>a.source_type===x),[s,x]),$=f.useMemo(()=>{const a=new Map;for(const c of v){const n=$e(c.file_name,c.mime_type),g=a.get(n)??[];g.push(c),a.set(n,g)}for(const c of a.values())c.sort((n,g)=>(g.email_received_at??g.created_at).localeCompare(n.email_received_at??n.created_at));return Ae.filter(c=>a.has(c)).map(c=>[c,a.get(c)??[]])},[v]),b=f.useMemo(()=>v.filter(a=>Be(a.mime_type)&&a.storage_bucket==="crm-attachments").map(a=>a.attachment_id),[v]),{data:z={}}=ie(b),j=async a=>{if(!a.length)return;const c=new Set(s.map(w=>`${w.file_name}::${w.file_size??""}`)),n=a.filter(w=>c.has(`${w.name}::${w.size}`));let g=a;if(n.length>0){const w=n.map(T=>`⢠${T.name}`).join(` 2`),G=i("attachments.duplicateConfirm",{defaultValue:`{{count}} Datei(en) sind bereits angehängt: 3{{list}} 4 5Trotzdem erneut hochladen?`,count:n.length,list:w});if(!await r({title:G,variant:"destructive"})){const T=new Set(n.map(k=>`${k.name}::${k.size}`));if(g=a.filter(k=>!T.has(`${k.name}::${k.size}`)),g.length===0){h.current&&(h.current.value="");return}}}o.mutate(g,{onSuccess:()=>{F.success(i("attachments.uploadedCount",{count:g.length})),h.current&&(h.current.value="")}})},{isDragOver:p,onDragEnter:D,onDragOver:U,onDragLeave:V,onDrop:W,onPaste:K}=ge({onFiles:a=>{j(a)}});return t.jsxs("div",{className:"relative space-y-3 outline-none",onDragEnter:D,onDragOver:U,onDragLeave:V,onDrop:W,onPaste:K,tabIndex:0,children:[p&&t.jsxs("div",{className:"pointer-events-none absolute inset-0 z-30 flex flex-col items-center justify-center rounded-lg border-2 border-dashed border-primary bg-primary/10 backdrop-blur-[1px]",children:[t.jsx(O,{className:"h-8 w-8 text-primary mb-2"}),t.jsx("p",{className:"text-sm font-medium text-primary",children:i("attachments.dropOverlay")})]}),t.jsxs("div",{className:"border-2 border-dashed rounded-lg p-4 text-center transition-colors cursor-pointer border-border hover:border-primary/50",onClick:()=>{var a;return(a=h.current)==null?void 0:a.click()},children:[o.isPending?t.jsx(R,{className:"h-5 w-5 animate-spin mx-auto text-muted-foreground"}):t.jsxs(t.Fragment,{children:[t.jsx(O,{className:"h-5 w-5 mx-auto text-muted-foreground mb-1"}),t.jsx("p",{className:"text-xs text-muted-foreground",children:i("attachments.dropHintLong")})]}),t.jsx("input",{ref:h,type:"file",multiple:!0,className:"hidden",onChange:a=>a.target.files&&j(Array.from(a.target.files))})]}),m?null:s.length===0?t.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:i("attachments.empty")}):t.jsxs(t.Fragment,{children:[t.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[t.jsx(M,{active:x==="all",label:i("attachments.tabAll"),count:C.all,onClick:()=>A("all")}),Te.filter(a=>C[a]>0).map(a=>t.jsx(M,{active:x===a,label:i(`attachments.sourcePill.${a}`),count:C[a],onClick:()=>A(a)},a))]}),t.jsx("div",{className:"space-y-4",children:$.map(([a,c])=>t.jsxs("div",{className:"space-y-1.5",children:[t.jsxs("p",{className:"text-xs font-semibold text-muted-foreground",children:[i(`attachments.formatGroup.${a}`,{defaultValue:ze[a]})," ","· ",c.length]}),c.map(n=>t.jsx(Ne,{doc:n,signedUrl:z[n.attachment_id],onPreview:n.storage_bucket==="crm-attachments"&&ne(n.file_name,n.mime_type)!=="none"?()=>N(n):null,onDelete:n.source_type==="deal"&&n.storage_bucket==="crm-attachments"?()=>d(n):null,deleteDisabledReason:n.source_type!=="deal"&&n.storage_bucket==="crm-attachments"?i("attachments.deleteAtSource",{defaultValue:"Bitte in der Quelle löschen ({{source}}).",source:i(`attachments.sourcePill.${n.source_type}`)}):null},n.row_id))]},a))})]}),u&&t.jsx(re,{open:!!u,onOpenChange:a=>{a||N(null)},filePath:u.file_path,fileName:u.file_name,mimeType:u.mime_type,bucket:u.storage_bucket}),t.jsx(ke,{pendingDelete:l,onOpenChange:a=>{a||d(null)},onConfirm:a=>{_.mutate({attachmentId:a.attachment_id,filePath:a.file_path,sourceType:a.source_type,sourceId:a.source_id},{onSettled:()=>d(null)})}})]})}export{ct as default};
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.