1import{r as m,p as M,j as e,n as U,b8 as W,aW as V,bI as B,a1 as pe,h as re,b4 as R,k as $,c6 as be,ji as fe,o as oe,bL as he,bM as ve,bN as ge,bC as _,_ as ye,aG as Ne,aH as je,aI as Ce,aJ as we,aK as ke,aL as Ae,aM as Te,aN as Pe,cA as Se,eK as te,bW as Fe,a_ as $e,aS as Ie,bZ as G,X as Le,g3 as Oe,g4 as De,g2 as Be,jy as Ke,eb as Ee,ec as qe}from"./index-DNBzpCbc.js";import{C as Me,a as Re,b as Qe,c as He,d as _e,e as ze,f as Ge,g as Ue,h as We}from"./cover-address-section-CMrcGRLg.js";import{C as Ve}from"./cover-thread-follow-button-DpQYsU_r.js";import{e as Xe,g as ne,h as ie,b as le,i as ce,d as de,j as Ze,C as Je}from"./cover-link-picker-B72IRTmN.js";import{C as ue}from"./cover-pick-list-combobox-BCsnShU2.js";import{P as Ye}from"./cover-crm-page-shell-vWylcmcv.js";import{a as et,C as tt}from"./cover-floating-actions-Ck3225CJ.js";import{C as at}from"./cover-proposal-wizard-BFD8wAOo.js";import{e as st}from"./cover-address-form-BVCQOsXs.js";import{b as rt,C as ot}from"./cover-configure-fields-modal-CkGniaA9.js";import{C as nt,c as it}from"./cover-custom-field-input-BDxwXvHC.js";import{u as I,s as lt,b as ct,a as dt,C as ut}from"./cover-contact-tabs-BXRD6sZl.js";import{L as mt}from"./layers-c3n_grte.js";import"./cover-status-lists-CwDoOk6C.js";import"./cover-status-lists-Cct0J6BU.js";import"./cover-pick-lists-BrT1TjHG.js";import"./user-check-CLb88laL.js";import"./pen-line-HazjfUdF.js";import"./cover-schedule-task-dialog-AZNQf5G-.js";import"./file-spreadsheet-DJs9nxxm.js";import"./address-helpers-D-4VEE4C.js";import"./star-DnLK5cKe.js";import"./ellipsis-CQgP51cE.js";import"./rows-3-oTlExNqW.js";import"./cover-threads-composer-BmgtaNyB.js";import"./repeat-2-Be2VmFSU.js";import"./paperclip-CP4Mzfmw.js";import"./user-round-CLSkVt5G.js";import"./sheet-BumWBnxk.js";import"./cover-proposals-8DTBBQV3.js";import"./cover-shift-glyph-DNjkD_x0.js";import"./cover-wizard-fields-C8azn6sK.js";import"./cover-menu-query-keys-BponkbWV.js";import"./cover-address-autocomplete-BkDq03m1.js";import"./switch-BXai0--o.js";import"./shield-check-Bp-k-ygB.js";import"./cover-more-records-CeRW6Kpr.js";import"./pin-qmMtqBjZ.js";function xt({open:u,onOpenChange:r,linkedAccountIds:h,onConfirm:N,confirming:n}){const[o,g]=m.useState(""),[d,A]=m.useState(new Set),[j,v]=m.useState([]),[l,C]=m.useState(!1);m.useEffect(()=>{u&&(g(""),A(new Set),v([]),C(!1))},[u]);const{data:S=[],isLoading:k}=M({queryKey:["/api/cover/accounts"],enabled:u}),T=m.useMemo(()=>{const i=new Map;for(const b of S)i.set(b.id,b);for(const b of j)i.set(b.id,b);return Array.from(i.values())},[S,j]),L=m.useMemo(()=>Xe(T,o,h,d),[T,o,h,d]);function F(i){A(b=>{const p=new Set(b);return p.has(i)?p.delete(i):p.add(i),p})}async function P(){const i=Array.from(d);i.length!==0&&(await N(i),r(!1))}return u?e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center p-4","data-testid":"cover-add-account-modal",children:[e.jsx("button",{type:"button",className:"absolute inset-0 bg-background/40","aria-label":"Close add account",onClick:()=>l?C(!1):r(!1)}),l?e.jsx(pt,{initialName:o,onBack:()=>C(!1),onClose:()=>r(!1),onCreated:i=>{v(b=>[i,...b.filter(p=>p.id!==i.id)]),A(b=>new Set(b).add(i.id)),g(""),C(!1)}}):e.jsxs("div",{className:"relative w-full max-w-[520px] rounded-2xl bg-card shadow-xl border border-border",children:[e.jsxs("div",{className:"flex items-start gap-3 px-5 pt-5 pb-4",children:[e.jsx("div",{className:"w-10 h-10 rounded-full bg-[hsl(var(--cover-user-accent-subtle))] text-[hsl(var(--cover-user-accent-text))] flex items-center justify-center shrink-0",children:e.jsx(U,{className:"w-4 h-4"})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("h2",{className:"text-base font-semibold text-foreground",children:"Add Account"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Search and select accounts to link to this contact"})]}),e.jsx("button",{type:"button",onClick:()=>r(!1),className:"w-8 h-8 rounded-md text-muted-foreground hover:bg-accent hover:text-muted-foreground","aria-label":"Close",children:e.jsx(W,{className:"w-4 h-4 mx-auto"})})]}),e.jsx("div",{className:"px-5 pb-4",children:e.jsxs("div",{className:"relative",children:[e.jsx(V,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx("input",{value:o,onChange:i=>g(i.target.value),placeholder:"Search by name, type, or email...",autoFocus:!0,className:"w-full h-11 pl-10 pr-3 rounded-xl border border-border text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-[hsl(var(--cover-user-accent)/0.4)]","data-testid":"cover-add-account-search"})]})}),e.jsxs("div",{className:B("px-5 space-y-2",L.length>5&&"max-h-80 overflow-y-auto"),"data-testid":"cover-add-account-list",children:[k&&e.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:"Loading accountsâ¦"}),!k&&L.length===0&&e.jsxs("div",{className:"py-8 text-center text-sm text-muted-foreground","data-testid":"cover-add-account-empty",children:[e.jsx("div",{children:"No accounts match your search."}),e.jsx("div",{children:"Hit create new below to add a new account."})]}),!k&&L.map(i=>{const b=ne(i),p=d.has(i.id);return e.jsxs("button",{type:"button",onClick:()=>
1F(i.id),className:"w-full flex items-center gap-3 rounded-xl border border-border bg-card px-3 py-2.5 text-left hover:border-[hsl(var(--cover-user-accent-border))]","data-testid":`cover-add-account-row-${i.id}`,"data-selected":p,children:[e.jsx("div",{className:B("w-9 h-9 rounded-lg text-white text-sm font-semibold flex items-center justify-center shrink-0",le(i.id||b)),children:ie(i)}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("div",{className:"text-sm font-semibold text-foreground truncate",children:b}),e.jsxs("div",{className:"flex items-center gap-2 mt-0.5 min-w-0",children:[i.category&&e.jsx(Ye,{tone:ce(i.category),children:i.category}),e.jsx("span",{className:"text-xs text-muted-foreground truncate",children:de(i.city,i.stateProvince)||"â"})]})]}),e.jsx("span",{className:B("w-5 h-5 rounded-full border flex items-center justify-center shrink-0",p?"bg-[hsl(var(--cover-user-accent))] border-[hsl(var(--cover-user-accent))] text-white":"border-border"),children:p&&e.jsx(pe,{className:"w-3 h-3"})})]},i.id)})]}),e.jsx("div",{className:"px-5 pt-3 pb-2",children:e.jsx("button",{type:"button",onClick:()=>C(!0),className:"w-full h-11 rounded-xl border border-dashed border-[hsl(var(--cover-user-accent-border))] text-sm font-medium text-[hsl(var(--cover-user-accent-text))] hover:bg-[hsl(var(--cover-user-accent-subtle))]","data-testid":"cover-add-account-create",children:"+ Create a new account"})}),e.jsxs("div",{className:"flex items-center justify-between gap-3 px-5 py-4 border-t border-border",children:[e.jsx("span",{className:"text-sm text-muted-foreground",children:d.size>0?`${d.size} selected`:"Select accounts above"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{type:"button",onClick:()=>r(!1),className:"h-9 px-3 text-sm font-medium text-muted-foreground",children:"Cancel"}),e.jsx("button",{type:"button",disabled:d.size===0||n,onClick:()=>{P()},className:"h-9 px-4 rounded-lg bg-[hsl(var(--cover-user-accent))] text-white text-sm font-medium hover:brightness-90 disabled:bg-border disabled:text-white","data-testid":"cover-add-account-confirm",children:n?"Addingâ¦":"Add Accounts"})]})]})]})]}):null}function pt({initialName:u,onBack:r,onClose:h,onCreated:N}){const n=re(),[o,g]=m.useState({companyName:u.trim(),accountCategory:"",email:"",website:"",address1:"",city:"",stateProvince:"",postalCode:""}),d=R({mutationFn:async()=>{const l=await $("POST","/api/cover/accounts",{companyName:o.companyName.trim(),accountCategory:o.accountCategory.trim(),email:o.email.trim(),website:o.website.trim()||void 0,address1:o.address1.trim()||void 0,city:o.city.trim()||void 0,stateProvince:o.stateProvince.trim()||void 0,postalCode:o.postalCode.trim()||void 0});if(!l.ok)throw new Error(await l.text()||`HTTP ${l.status}`);return await l.json()},onSuccess:async l=>{await n.invalidateQueries({queryKey:["/api/cover/accounts"]}),await n.invalidateQueries({queryKey:["/api/cover/accounts/stats"]}),N(l)}}),A=o.companyName.trim().length>0&&!!o.accountCategory&&o.email.trim().length>0&&!d.isPending,j=d.error;function v(l,C){g(S=>({...S,[l]:C}))}return e.jsxs("div",{className:"relative w-full max-w-[520px] max-h-[90vh] overflow-y-auto rounded-2xl bg-card shadow-xl border border-border","data-testid":"cover-create-account-modal",children:[e.jsxs("div",{className:"flex items-start gap-3 px-5 pt-5 pb-4",children:[e.jsx("div",{className:"w-10 h-10 rounded-full bg-[hsl(var(--cover-user-accent-subtle))] text-[hsl(var(--cover-user-accent-text))] flex items-center justify-center shrink-0",children:e.jsx(U,{className:"w-4 h-4"})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("h2",{className:"text-base font-semibold text-foreground",children:"Create New Account"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Fill in the details â they will be linked automatically"})]}),e.jsx("button",{type:"button",onClick:h,className:"w-8 h-8 rounded-md text-muted-foreground hover:bg-accent","aria-label":"Close",children:e.jsx(W,{className:"w-4 h-4 mx-auto"})})]}),e.jsxs("div",{className:"px-5 pb-4 space-y-3",children:[e.jsxs("label",{className:"block text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["Account Name",e.jsx("span",{className:"text-[hsl(var(--cover-user-accent-text))] ml-0.5",children:"*"}),e.jsx("input",{value:o.companyName,onChange:l=>v("companyName",l.target.value),placeholder:"e.g. Meridian Corp",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm text-foreground","data-testid":"cover-create-account-name"})]}),e.jsxs("label",{className:"block text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["Account Category",e.jsx("span",{className:"text-[hsl(var(--cover-user-accent-text))] ml-0.5",children:"*"}),e.jsx(ue,{inputId:"cover-create-account-type-input",listKey:"accounts-category",value:o.accountCategory,onValueChange:l=>v("accountCategory",l),allowFreeText:!0,placeholder:"Select or enter typeâ¦",className:"mt-1",testId:"cover-create-account-type"})]}),e.jsxs("label",{className:"block text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["Email",e.jsx("span",{className:"text-[hsl(var(--cover-user-accent-text))] ml-0.5",children:"*"}),e.jsx("input",{type:"email",value:o.email,onChange:l=>v("email",l.target.value),placeholder:"[email protected]",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm","data-testid":"cover-create-account-email"})]}),e.jsxs("label",{className:"block text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["Website",e.jsx("input",{value:o.website,onChange:l=>v("website",l.target.value),placeholder:"www.company.com",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm","data-testid":"cover-create-account-website"})]}),e.jsxs("label",{className:"block text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["Street Address",e.jsx("input",{value:o.address1,onChange:l=>v("address1",l.target.value),placeholder:"123 Main St",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm","data-testid":"cover-create-account-address"})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("label",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["City",e.jsx("input",{value:o.city,onChange:l=>v("city",l.target.value),placeholder:"City",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm","data-testid":"cover-create-account-city"})]}),e.jsxs("label",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["State",e.jsx("input",{value:o.stateProvince,onChange:l=>v("stateProvince",l.target.value),placeholder:"ST",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm","data-testid":"cover-create-account-state"})]}),e.jsxs("label",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:["Zip",e.jsx("input",{value:o.postalCode,onChange:l=>v("postalCode",l.target.value),placeholder:"00000",className:"mt-1 w-full h-10 rounded-lg border border-border bg-muted px-3 text-sm","data-testid":"cover-create-account-zip"})]})]})]}),j&&e.jsx("div",{className:"px-5 pb-2 text-sm text-red-600",children:j.message}),e.jsxs("div",{className:"flex items-center justify-between gap-3 px-5 py-4 border-t border-border",children:[e.jsxs("button",{type:"button",onClick:r,className:"inline-flex items-center gap-1.5 h-9 text-sm font-medium text-muted-foreground","data-testid":"cover-create-account-back",children:[e.jsx(be,{className:"w-4 h-4"})," Back to list"]}),e.jsx("button",{type:"button",disabled:!A,onClick:()=>
1d.mutate(),className:"h-9 px-4 rounded-lg bg-[hsl(var(--cover-user-accent))] text-white text-sm font-medium hover:brightness-90 disabled:bg-border disabled:text-white","data-testid":"cover-create-account-submit",children:d.isPending?"Creatingâ¦":"Create & Link Account"})]})]})}function bt(u){const r=(u.firstName||"").trim().charAt(0),h=(u.lastName||"").trim().charAt(0);return(r+h).toUpperCase()||"?"}function ft(u){return[u.firstName,u.lastName].filter(Boolean).join(" ")}function ht({trigger:u,onPick:r,onCreateNew:h}){const[N,n]=m.useState(!1),[o,g]=m.useState(""),d=m.useRef(null),{contacts:A,loading:j,error:v}=fe({limit:200}),l=I(lt),C=I(ct),S=I(s=>s.closeAll),k=I(s=>s.closeOthers),[,T]=oe(),L=m.useMemo(()=>new Set(l.map(s=>s.id)),[l]),P=l.filter(s=>!s.pinned).length>0,i=m.useMemo(()=>{if(C&&l.some(s=>s.id===C))return C;for(let s=l.length-1;s>=0;s-=1)if(!l[s].pinned)return l[s].id;return null},[l,C]),b=!!i&&l.some(s=>s.id!==i&&!s.pinned),[p,K]=m.useState(null),O=m.useCallback(()=>{S(),T("/cover/contacts"),n(!1)},[S,T]),D=m.useCallback(s=>{k(s),T(`/cover/contacts/${s}`),n(!1)},[k,T]),a=m.useCallback(()=>{if(I.getState().tabs.some(f=>f.isDirty&&!f.pinned)){K({kind:"all"});return}O()},[O]),w=m.useCallback(()=>{if(!i)return;if(I.getState().tabs.some(f=>f.isDirty&&!f.pinned&&f.id!==i)){K({kind:"others",keepId:i});return}D(i)},[i,D]),Q=m.useCallback(()=>{p&&(p.kind==="all"?O():D(p.keepId),K(null))},[p,O,D]),y=m.useMemo(()=>{const s=o.trim().toLowerCase();return s?A.filter(f=>[f.firstName??"",f.lastName??"",f.email??"",f.title??"",f.primaryAccountName??"",f.city??""].join(" ").toLowerCase().includes(s)):A},[A,o]);return e.jsxs(he,{open:N,onOpenChange:s=>{n(s),s?setTimeout(()=>d.current?.focus(),0):g("")},children:[e.jsx(ve,{asChild:!0,children:u}),e.jsxs(ge,{align:"end",sideOffset:6,className:"w-[380px] p-0","data-testid":"cover-contact-picker",children:[e.jsx("div",{className:"px-3 pt-3 pb-1",children:e.jsx("div",{className:"text-[11px] font-semibold uppercase tracking-wider text-muted-foreground",children:"Open another contact"})}),e.jsx("div",{className:"px-3 pb-2",children:e.jsxs("div",{className:"relative",children:[e.jsx(V,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground"}),e.jsx("input",{ref:d,type:"text",value:o,onChange:s=>g(s.target.value),placeholder:"Search contacts...",className:B("w-full h-8 pl-8 pr-2 rounded-md border border-border","text-sm placeholder:text-muted-foreground","focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:border-transparent"),"data-testid":"cover-contact-picker-search"})]})}),e.jsxs("div",{className:"max-h-[320px] overflow-y-auto border-t border-border","data-testid":"cover-contact-picker-list",children:[j&&e.jsx("div",{className:"px-3 py-6 text-center text-xs text-muted-foreground",children:"Loading contactsâ¦"}),!j&&v&&e.jsx("div",{className:"px-3 py-6 text-center text-xs text-rose-600",children:"Failed to load contacts."}),!j&&!v&&y.length===0&&e.jsx("div",{className:"px-3 py-6 text-center text-xs text-muted-foreground",children:o?"No matches.":"No contacts yet."}),!j&&!v&&y.map(s=>{const f=L.has(s.id),E=ft(s),c=[s.title,s.primaryAccountName].filter(Boolean).join(" · ");return e.jsxs("button",{type:"button",onClick:()=>{r(s),n(!1)},className:B("w-full flex items-center gap-3 px-3 py-2 text-left","hover:bg-muted transition-colors","focus:outline-none focus:bg-muted",f&&"italic text-muted-foreground"),"data-testid":`cover-contact-picker-item-${s.id}`,children:[e.jsx("span",{className:"text-xs font-semibold text-emerald-700 bg-emerald-50 shrink-0 w-8 h-8 rounded-md flex items-center justify-center",children:bt(s)}),e.jsxs("span",{className:"min-w-0 flex-1",children:[e.jsx("span",{className:"block truncate text-sm font-medium text-foreground",children:_(E,o)}),e.jsx("span",{className:"block truncate text-xs text-muted-foreground",children:c?_(c,o):"â"})]}),s.email&&e.jsx("span",{className:"text-[10px] truncate max-w-[120px] text-muted-foreground shrink-0",children:_(s.email,o)}),f&&e.jsx("span",{className:"text-[10px] uppercase tracking-wider text-muted-foreground shrink-0",children:"Open"})]},s.id)})]}),e.jsx("div",{className:"border-t border-border",children:e.jsxs("button",{type:"button",onClick:()=>{h(),n(!1)},className:"w-full flex items-center gap-2 px-3 py-2.5 text-sm text-emerald-600 hover:bg-emerald-50 transition-colors","data-testid":"cover-contact-picker-create",children:[e.jsx(ye,{className:"w-3.5 h-3.5"}),"Create new contact"]})}),(P||b)&&e.jsxs("div",{className:"px-1 py-1 border-t border-border bg-muted/60","data-testid":"cover-contact-picker-close-group",children:[b&&e.jsxs("button",{type:"button",onClick:w,disabled:!b,className:B("w-full flex items-center gap-2 px-2 py-1.5 text-xs rounded-md","text-muted-foreground hover:bg-accent disabled:opacity-40 disabled:cursor-not-allowed"),"data-testid":"cover-contact-picker-close-others",children:[e.jsx(mt,{className:"w-3.5 h-3.5"}),"Close other tabs"]}),P&&e.jsxs("button",{type:"button",onClick:a,disabled:!P,className:B("w-full flex items-center gap-2 px-2 py-1.5 text-xs rounded-md","text-muted-foreground hover:bg-accent disabled:opacity-40 disabled:cursor-not-allowed"),"data-testid":"cover-contact-picker-close-all",children:[e.jsx(W,{className:"w-3.5 h-3.5"}),"Close all tabs"]})]}),e.jsx(Ne,{open:p!==null,onOpenChange:s=>{s||K(null)},children:e.jsxs(je,{children:[e.jsxs(Ce,{children:[e.jsx(we,{children:p?.kind==="all"?"Close all tabs with unsaved changes?":"Close other tabs with unsaved changes?"}),e.jsx(ke,{children:p?.kind==="all"?"One or more open contacts have unsaved changes. Closing all tabs will discard them. Pinned tabs are preserved.":"One or more of the other tabs have unsaved changes. Closing them will discard those changes. Pinned tabs are preserved."})]}),e.jsxs(Ae,{children:[e.jsx(Te,{children:"Cancel"}),e.jsx(Pe,{onClick:Q,children:"Discard and close"})]})]})})]})]})}function ae(u){return u?new Date(u).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"}):"â"}function z(u,r){return`${(u||"")[0]||""}${(r||"")[0]||""}`.toUpperCase()||"?"}function na(){const r=Se().contactId,[,h]=oe(),N=re(),n=`/api/cover/contacts/${r}`,o=M({queryKey:[n],queryFn:async()=>{const t=await $("GET",n);if(!t.ok)throw new Error(`HTTP ${t.status}`);return await t.json()},enabled:!!r,staleTime:3e4}),g=I(t=>t.open),d=I(t=>t.replaceActive),A=I(t=>t.activate),[j]=dt();m.useEffect(()=>{if(!r)return;const t=I.getState(),x=o.data,J=(x?`${x.firstName||""} ${x.lastName||""}`.trim():"")||"Contact",Y=z(x?.firstName,x?.lastName);if(t.tabs.find(xe=>xe.id===r)){g({id:r,title:J,initials:Y},{activate:!1}),t.activeId!==r&&A(r);return}const ee={id:r,title:J,initials:Y};j?g(ee):d(ee)},[r,o.data,j,g,d,A]);const v=m.useCallback(t=>{const x=`${t.firstName||""} ${t.lastName||""}`.trim(),H={id:t.id,title:x||"Contact",initials:z(t.firstName,t.lastName)};j?g(H):d(H),h(`/cover/contacts/${t.id}`)},[j,g,d,h]),l=m.useCallback(()=>{console.info("[HOR-666] New Contact wizard not implemented yet.")},[]),[C,S]=m.useState("details"),[k,T]=m.useState("accounts"),[L,F]=m.useState(!1),[P,i]=m.useState({}),[b,p]=m.useState(!1),[K,O]=m.useState(!1),D=M({queryKey:[`${n}
1/accounts`],queryFn:async()=>{const t=await $("GET",`${n}/accounts`);if(!t.ok)throw new Error(`HTTP ${t.status}`);return await t.json()},enabled:!!r&&k==="accounts",staleTime:3e4}),a=M({queryKey:[`${n}/notes`],queryFn:async()=>await(await $("GET",`${n}/notes`)).json(),enabled:!!r&&k==="notes",staleTime:3e4}),w=M({queryKey:[`${n}/files`],queryFn:async()=>await(await $("GET",`${n}/files`)).json(),enabled:!!r&&k==="files",staleTime:3e4}),Q=M({queryKey:[`${n}/activity`],queryFn:async()=>await(await $("GET",`${n}/activity`)).json(),enabled:!!r&&k==="activity",staleTime:3e4}),y=R({mutationFn:async t=>{const x=await $("PATCH",n,t);if(!x.ok)throw new Error(await x.text()||`HTTP ${x.status}`);return await x.json()},onSuccess:()=>{N.invalidateQueries({queryKey:[n]}),F(!1)}}),s=R({mutationFn:async t=>{const x=await $("POST",`${n}/accounts`,{accountIds:t});if(!x.ok)throw new Error(await x.text()||`HTTP ${x.status}`)},onSuccess:()=>{N.invalidateQueries({queryKey:[`${n}/accounts`]}),N.invalidateQueries({queryKey:[n]}),N.invalidateQueries({queryKey:["/api/cover/accounts"]})}}),f=R({mutationFn:async t=>{const x=await $("DELETE",`${n}/accounts/${t}`);if(!x.ok)throw new Error(await x.text()||`HTTP ${x.status}`)},onSuccess:()=>N.invalidateQueries({queryKey:[`${n}/accounts`]})}),E=R({mutationFn:async t=>{const x=await $("POST",`${n}/primary-account`,{accountId:t});if(!x.ok)throw new Error(await x.text()||`HTTP ${x.status}`)},onSuccess:()=>{N.invalidateQueries({queryKey:[`${n}/accounts`]}),N.invalidateQueries({queryKey:[n]})}}),c=o.data,q=m.useMemo(()=>Ze(D.data??[],c?.primaryAccountId),[D.data,c?.primaryAccountId]),me=m.useMemo(()=>q.map(t=>{const x=ne(t);return{id:t.id,initials:ie(t),initialsBg:`${le(t.id||x)} text-white`,title:x,badge:t.category,badgeTone:ce(t.category),location:de(t.city,t.stateProvince),isPrimary:t.id===c?.primaryAccountId}}),[q,c?.primaryAccountId]);if(o.isLoading||!c)return e.jsx(te,{children:e.jsx("div",{className:"max-w-[1600px] mx-auto px-3 sm:px-6 py-4",children:o.isError?e.jsxs("div",{className:"rounded-2xl bg-card border border-red-200 p-10 text-center shadow-sm",children:[e.jsx("div",{className:"text-lg font-semibold text-foreground mb-2",children:"Couldn't load this contact"}),e.jsx("button",{onClick:()=>o.refetch(),className:"inline-flex items-center h-9 px-4 rounded-lg bg-primary text-white text-sm font-medium hover:bg-primary/90",children:"Retry"})]}):e.jsxs("div",{className:"animate-pulse space-y-4",children:[e.jsx("div",{className:"h-8 w-64 bg-border rounded"}),e.jsx("div",{className:"grid grid-cols-3 gap-4",children:Array.from({length:3}).map((t,x)=>e.jsx("div",{className:"h-32 rounded-2xl bg-accent"},x))})]})})});const Z=`${c.firstName} ${c.lastName}`.trim();return e.jsxs(te,{children:[e.jsx(ut,{rightSlot:e.jsx(ht,{onPick:v,onCreateNew:l,trigger:e.jsx("button",{type:"button",title:"Open another contact","aria-label":"Open another contact",className:"inline-flex items-center justify-center w-8 h-8 rounded-md text-muted-foreground hover:text-foreground hover:bg-border","data-testid":"cover-contact-tabs-open-another",children:e.jsx(V,{className:"w-4 h-4"})})})}),e.jsx("div",{className:"max-w-[1600px] mx-auto px-3 sm:px-6 py-4",children:e.jsx(Me,{testId:"cover-contact-details",backTo:"/cover/contacts",hideBack:!0,title:Z,pill:c.title?{label:c.title,tone:"purple"}:void 0,subtitle:[c.primaryAccountName,c.email].filter(Boolean).join(" · "),actions:e.jsxs(e.Fragment,{children:[e.jsx(Ve,{recordKind:"contact",recordId:r}),e.jsx("button",{onClick:()=>{i(c),F(!0),S("details")},className:"h-9 px-3 rounded-lg border border-border text-sm font-medium text-foreground hover:bg-muted","data-testid":"cover-contact-edit",children:"Edit"}),e.jsx("button",{onClick:()=>p(!0),className:"h-9 px-3 rounded-lg bg-primary text-primary-foreground text-sm font-medium hover:bg-primary/90","data-testid":"cover-contact-new-proposal",children:"New Proposal"})]}),heroCards:[e.jsx(ze,{stripTone:"green",initials:z(c.firstName,c.lastName),title:Z,subtitle:c.title,pill:c.primaryAccountName?{label:c.primaryAccountName,tone:"purple"}:void 0,topRightSecondary:"0 proposals",testId:"cover-contact-summary-card"},"summary"),e.jsx(Ge,{title:"Contact Info",rows:[{icon:Fe,label:"Email",value:c.email||"â"},{icon:$e,label:"Phone",value:G(c.phone)||G(c.mobilePhone)||"â"},{icon:Ie,label:"Location",value:[c.city,c.stateProvince].filter(
1Boolean).join(", ")||"â"},{icon:U,label:"Account",value:c.primaryAccountName||"â"}],testId:"cover-contact-info-card"},"contact-info"),e.jsx(Ue,{variant:"audit",createdBy:c.createdByName||"Unknown",created:ae(c.createdAt),modifiedBy:c.updatedByName||c.createdByName||"Unknown",modified:ae(c.updatedAt),testId:"cover-contact-audit-card"},"audit")],leftTabs:[{key:"details",label:"Details"},{key:"proposals",label:"Proposals"}],leftActiveTab:C,onLeftTabChange:t=>S(t),leftBody:C==="details"?e.jsx(yt,{contact:c,editMode:L,editValues:P,setEditValues:i,onCancel:()=>F(!1),onSave:()=>y.mutate(P),saving:y.isPending}):e.jsx(_e,{parentApiBase:n,onNewProposal:()=>p(!0)}),rightTabs:[{key:"accounts",label:"Accounts"},{key:"notes",label:"Notes"},{key:"files",label:"Files"},{key:"activity",label:"Activity"}],rightActiveTab:k,onRightTabChange:t=>T(t),rightBody:k==="accounts"?e.jsx(Je,{title:"Accounts",records:me,primaryTone:"light-green",goToLabel:"Go to Account",onAdd:()=>O(!0),onRemove:t=>f.mutate(t),onMakePrimary:t=>E.mutate(t),onGoTo:t=>h(`/cover/accounts/${t}`),testId:"cover-contact-accounts-list"}):k==="notes"?e.jsx(Re,{parentApiBase:n,notes:a.data??[],invalidateKey:`${n}/notes`}):k==="files"?e.jsx(Qe,{parentApiBase:n,files:w.data??[],invalidateKey:`${n}/files`}):e.jsx(et,{recordKind:"contact",recordId:r||"",title:`${c.firstName} ${c.lastName}`.trim(),timeline:e.jsx(He,{parentApiBase:n,activity:Q.data??[],invalidateKey:`${n}/activity`})})})}),e.jsx(tt,{parentKind:"contact",parentId:r,parentLabel:`${c.firstName} ${c.lastName}`.trim(),defaultTo:c.email}),e.jsx(xt,{open:K,onOpenChange:O,linkedAccountIds:q.map(t=>t.id),confirming:s.isPending,onConfirm:t=>s.mutateAsync(t)}),e.jsx(at,{open:b,onOpenChange:p,prefill:{relatedType:"contact",relatedId:r||"",relatedName:`${c.firstName} ${c.lastName}`.trim()}})]})}function vt({label:u,value:r,editMode:h,onChange:N,dirty:n,phone:o=!1}){const g=Ee(r,o);return e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:u}),h?e.jsx("input",{value:o?g.value:r||"",onFocus:g.onFocus,onBlur:g.onBlur,inputMode:o?"tel":void 0,onChange:d=>N?.(o?qe(d.target.value):d.target.value),className:B("mt-1 w-full h-9 rounded-lg border px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary/70",n?"bg-yellow-50 border-yellow-300":"bg-card border-border"),"data-testid":n?"cover-field-dirty":void 0}):e.jsx("div",{className:"mt-1 w-full min-h-9 flex items-center rounded-lg border border-border bg-card px-3 text-sm text-foreground",children:(o?G(r):r)||"â"})]})}const X=[{key:"salutation",label:"Salutation"},{key:"firstName",label:"First Name"},{key:"lastName",label:"Last Name"},{key:"title",label:"Title"},{key:"account",label:"Account"},{key:"email",label:"Email"},{key:"phone",label:"Phone"},{key:"mobilePhone",label:"Mobile Phone"},{key:"secondaryPhone",label:"Secondary Phone"},{key:"fax",label:"Fax"},{key:"reference",label:"Reference"},{key:"salesRepName",label:"Sales Rep"},{key:"createdByName",label:"Created By"},{key:"updatedByName",label:"Modified By"}],gt=X.map(u=>u.key),se=Object.fromEntries(X.map((u,r)=>[u.key,r<9]));function yt({contact:u,editMode:r,editValues:h,setEditValues:N,onCancel:n,onSave:o,saving:g}){const d=r?h:u,A=a=>w=>N({...h,[a]:w}),j=a=>r&&Object.prototype.hasOwnProperty.call(h,a)&&h[a]!==u[a],v=Le(),[l,C]=m.useState(!1),k=M({queryKey:["/api/cover/settings/custom-fields"]}).data?.find(a=>a.object==="contacts")?.slots||[],T=Oe(X,k),L=T.map(a=>a.key),F=De(v.get("coverContactFieldOrder",gt),L),P={...se,...v.get("coverContactFieldVisibility",se)},i=F.filter(a=>P[a]),b=i.length>0?i:["firstName","lastName","email"],p=Math.max(2,...b.map(a=>F.indexOf(a))),K=F.slice(0,Math.ceil((p+1)/3)*3);function O(a,w){N({...h,customFields:{...h.customFields||u.customFields||{},[String(a)]:w}})}function D(a){const w=Be(a);if(w!=null){const f=T.find(q=>q.key===a);if(!f?.custom)return e.jsx("div",{"aria-hidden":"true"},a);const E=(d.customFields||{})[String(w)]||"",c=(u.customFields||{})[String(w)]||"";return e.jsx(nt,{label:f.label,type:f.custom.type,pickListKey:f.custom.type==="text"?it("contacts",w):void 0,value:E,editMode:r,dirty:r&&E!==c,onChange:q=>O(w,q)},a)}const y={salutation:{label:"Salutation",value:d.salutation,field:"salutation",editable:!0},firstName:{label:"First Name",value:d.firstName,field:"firstName",editable:!0},lastName:{label:"Last Name",value:d.lastName,field:"lastName",editable:!0},title:{label:"Title",value:d.title,field:"title",editable:!0},account:{label:"Account",value:u.primaryAccountName,editable:!1},email:{label:"Email",value:d.email,field:"email",editable:!0},phone:{label:"Phone",value:d.phone,field:"phone",editable:!0},mobilePhone:{label:"Mobile Phone",value:d.mobilePhone,field:"mobilePhone",editable:!0},secondaryPhone:{label:"Secondary Phone",value:d.secondaryPhone,field:"secondaryPhone",editable:!0},fax:{label:"Fax",value:d.fax,field:"fax",editable:!0},reference:{label:"Reference",value:d.reference,field:"reference",editable:!0},salesRepName:{label:"Sales Rep",value:d.salesRepName,field:"salesRepName",editable:!0},createdByName:{label:"Created By",value:u.createdByName,editable:!1},updatedByName:{label:"Modified By",value:u.updatedByName,editable:!1}}[a];if(!y)return e.jsx("div",{"aria-hidden":"true"},a);const s=a==="title"?"contacts-title":a==="reference"?"general-reference":void 0;return s&&r&&y.field?e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wide",children:y.label}),e.jsx(ue,{label:y.label,listKey:s,value:String(y.value||""),onValueChange:A(y.field),allowFreeText:!0,className:B("mt-1 h-9",j(y.field)&&"bg-yellow-50 border-yellow-300"),testId:`cover-contact-${a}`})]},a):e.jsx(vt,{phone:Ke(a),label:y.label,value:y.value,editMode:r&&!!y.editable,onChange:y.field?A(y.field):void 0,dirty:y.field?j(y.field):!1},a)}return e.jsxs("div",{className:"space-y-6",children:[r&&e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx("button",{onClick:n,className:"h-8 px-3 rounded-md text-sm text-muted-foreground hover:bg-accent",children:"Cancel"}),e.jsx("button",{onClick:o,disabled:g,className:"h-8 px-3 rounded-md bg-primary text-white text-sm font-medium hover:bg-primary/90 disabled:opacity-60","data-testid":"cover-contact-save",children:g?"Savingâ¦":"Save"})]}),e.jsxs("section",{className:"rounded-2xl bg-card border border-border shadow-sm p-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("h3",{className:"text-sm font-semibold text-foreground",children:"Contact Information"}),e.jsx(rt,{onClick:()=>C(!0),testId:"cover-contact-fields"})]}),e.jsx("div",{className:"grid grid-cols-3 gap-4",children:K.map(a=>P[a]||i.length===0&&b.includes(a)?D(a):e.jsx("div",{"aria-hidden":"true"},a))})]}),e.jsx(We,{values:d,editMode:r,onChange:(a,w)=>A(a)(w),isDirty:a=>j(a),accent:"green",mapTitle:"Contact location map"}),r?e.jsx(st,{value:d.otherAddresses||[],onChange:a=>N({...h,otherAddresses:a})}):(d.otherAddresses||[]).length>0?e.jsxs("section",{className:"rounded-2xl bg-card border border-border shadow-sm p-4",children:[e.jsx("h3",{className:"text-sm font-semibold text-foreground mb-3",children:"Other Addresses"}),e.jsx("div",{className:"space-y-2 text-sm text-foreground",children:(d.otherAddresses||[]).map((a,w)=>e.jsxs("div",{children:[e.jsxs("span",{className:"font-medium",children:[a.label,":"]}
1)," ",[a.address1,a.city,a.stateProvince,a.postalCode].filter(Boolean).join(", ")]},w))})]}):null,e.jsx(ot,{open:l,onClose:()=>C(!1),subtitle:"Choose and arrange Contact Information fields",catalog:T,order:F,visibility:P,testId:"cover-configure-contact-fields",columns:3,onApply:(a,w)=>{v.set("coverContactFieldOrder",a),v.set("coverContactFieldVisibility",w),C(!1)}})]})}export{na 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.