1import{u as A,a as ue,r as g,j as e,B as S,L as V,E as ne,T as oe,b as W,c as _,C as xe,d as Z,e as U,A as le,I as q,f as ge,g as ye,h as ce,i as ae,k as fe,l as be}from"./index-CxYxjiaW.js";import{f as de}from"./pathLabel-CTDd-KTv.js";import{M as ie}from"./Modal-D0jJoeS9.js";function je(t){const o=[",",";"," "];let a="",i=!1;for(const r of t){if(r==='"'&&(i=!i),(r===` 2`||r==="\r")&&!i){if(a.trim())break;continue}a+=r}let l=",",c=0;for(const r of o){const s=a.split(r).length-1;s>c&&(l=r,c=s)}return l}function ve(t,o){t.charCodeAt(0)===65279&&(t=t.slice(1));const a=je(t),i=[];let l="",c=[],r=!1,s=0;const k=t.length;for(;s<k;){const x=t[s];if(r){if(x==='"'){if(t[s+1]==='"'){l+='"',s+=2;continue}r=!1,s+=1;continue}l+=x,s+=1;continue}if(x==='"'){r=!0,s+=1;continue}if(x===a){c.push(l),l="",s+=1;continue}if(x==="\r"){s+=1;continue}if(x===` 3`){c.push(l),i.push(c),l="",c=[],s+=1;continue}l+=x,s+=1}for((l.length>0||c.length>0)&&(c.push(l),i.push(c));i.length>0&&i[i.length-1].every(x=>x==="");)i.pop();if(i.length===0)return{headers:[],rows:[]};const y=i[0].map(x=>x.trim()),h=i.slice(1),N=y.length;for(const x of h)for(;x.length<N;)x.push("");return{headers:y,rows:h}}function ke(t){const o=t.trim();if(o==="")return null;if(/^-?\d+(\.\d+)?$/.test(o)){const a=Number(o);if(!Number.isNaN(a))return a}return o}function he(t){const o=t.toLowerCase().replace(/[^a-z0-9_-]+/g,"-").replace(/^-+|-+$/g,"").replace(/-+/g,"-");return(/^[a-z]/.test(o)?o:`a-${o}`).slice(0,64)}function Ne(t,o,a){const i=t.trim().toLowerCase();if(i===""||i==="ignore")return{kind:"ignore"};if(i==="email"||i==="e-mail"||i==="mail")return{kind:"email"};if(i==="display_name"||i==="display name"||i==="name")return{kind:"display_name"};if(i==="active"||i==="enabled")return{kind:"active"};const l=a.find(r=>r.key.toLowerCase()===i||r.label.toLowerCase()===i);if(l)return{kind:"existing_attr",key:l.key};const c=Array.from(new Set(o.map(r=>r.trim()).filter(Boolean))).slice(0,50);return{kind:"new_attr",key:he(t),label:t.trim(),type:"select",options:c}}function we({onClose:t,onImported:o,attrs:a,existingEmails:i}){const{t:l}=A("app"),c=ue(),[r,s]=g.useState("upload"),[k,y]=g.useState(""),[h,N]=g.useState(null),[x,I]=g.useState([]),[L,X]=g.useState([]),[j,F]=g.useState([]),[K,D]=g.useState(!1),[H,u]=g.useState(null),[b,T]=g.useState(null);function P(m){var d;const v=(d=m.target.files)==null?void 0:d[0];if(!v)return;const n=new FileReader;n.onload=()=>{y(String(n.result??""))},n.readAsText(v,"utf-8")}function O(){try{const{headers:m,rows:v}=ve(k);if(m.length===0){N(l("importWizard.parse.noHeaderRow"));return}if(v.length===0){N(l("importWizard.parse.noDataRows"));return}N(null),I(m),X(v);const n=m.map((p,w)=>({header:p,target:Ne(p,v.map(E=>E[w]??""),a)}));if(!n.some(p=>p.target.kind==="email")){const p=v[0].findIndex(w=>/^\S+@\S+\.\S+$/.test(w||""));p!==-1&&(n[p].target={kind:"email"})}F(n),s("map")}catch(m){N(m instanceof Error?m.message:l("importWizard.parse.parseFailed"))}}const R=[],G=j.filter(m=>m.target.kind==="email");G.length!==1&&R.push(l("importWizard.issues.exactlyOneEmail",{count:G.length}));const Y=new Set;for(const m of j)if(m.target.kind==="existing_attr"||m.target.kind==="new_attr"){const v=m.target.key;Y.has(v)&&R.push(l("importWizard.issues.duplicateAttrKey",{key:v})),Y.add(v)}for(const m of["display_name","active"]){const v=j.filter(n=>n.target.kind===m).length;v>1&&R.push(l("importWizard.issues.builtinMappedToMany",{field:m,count:v}))}const C=G[0]?j.findIndex(m=>m.target.kind==="email"):-1,$=g.useMemo(()=>C<0?[]:L.map((m,v)=>{const n=(m[C]??"").trim().toLowerCase(),d=/^\S+@\S+\.\S+$/.test(n);let p=null,w;const E={};for(let f=0;f<j.length;f++){const z=j[f],M=(m[f]??"").trim();switch(z.target.kind){case"display_name":p=M||null;break;case"active":w=/^(true|1|yes|y|active)$/i.test(M);break;case"existing_attr":case"new_attr":E[z.target.key]=ke(M);break}}return{idx:v,email:n,valid:d,willUpdate:d&&i.has(n),displayName:p,active:w,attributes:E}}),[L,j,C,i]),ee=$.filter(m=>!m.valid),te=$.filter(m=>m.valid&&!m.willUpdate).length,se=$.filter(m=>m.valid&&m.willUpdate).length,B=j.filter(m=>m.target.kind==="new_attr");async function re(){D(!0),u(null);try{for(const n of B){if(n.target.kind!=="new_attr")continue;const d={key:n.target.key,label:n.target.label,value_type:n.target.type,options:n.target.options};await U("/org-attributes",{method:"POST",body:d})}const m={employees:$.filter(n=>n.valid).map(n=>({email:n.email,display_name:n.displayName,attributes:n.attributes}))},v=await U("/employees/bulk",{method:"POST",body:m});T({created:v.created,updated:v.updated,newAttrs:B.length}),c.invalidateQueries({queryKey:["employees"]}),c.invalidateQueries({queryKey:["org-attributes"]}),o()}catch(m){u(m instanceof le?m.message:l("importWizard.importFailed"))}finally{D(!1)}}return e.jsxs(ie,{open:!0,onClose:t,title:l("importWizard.modalTitle"),size:"xl",footer:e.jsx(Ee,{step:r,canAdvance:r==="upload"&&k.trim().length>0||r==="map"&&R.length===0||r==="confirm",running:K,onBack:()=>{r==="map"?s("upload"):r==="confirm"&&s("map")},onClose:t,onNext:()=>{r==="upload"?O():r==="map"?s("confirm"):re()},nextLabel:r==="confirm"?K?l("importWizard.footer.importing"):l("importWizard.footer.importRows",{count:te+se}):l("importWizard.footer.next")}),children:[e.jsx(Me,{step:r}),b?e.jsx(Pe,{result:b,onClose:t}):r==="upload"?e.jsx(Ce,{raw:k,onRaw:y,onFile:P,parseError:h}):r==="map"?e.jsx(ze,{headers:x,rows:L,mappings:j,onChange:F,attrs:a,issues:R}):e.jsx(Le,{toCreate:te,toUpdate:se,invalidRows:ee,newAttrs:B,previewRows:$,runError:H})]})}function Ce({raw:t,onRaw:o,onFile:a,parseError:i}){const{t:l}=A("app");return e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs text-fg-dim",children:l("importWizard.upload.help")}),e.jsxs("div",{children:[e.jsx(V,{children:l("importWizard.upload.csvFile")}),e.jsx("input",{type:"file",accept:".csv,text/csv",onChange:a,className:"block w-full text-sm text-fg-dim file:mr-3 file:py-1.5 file:px-3 file:rounded-md file:border file:border-border file:bg-surface file:text-fg file:cursor-pointer"})]}),e.jsxs("div",{children:[e.jsx(V,{children:l("importWizard.upload.orPaste")}),e.jsx("textarea",{rows:10,className:"w-full rounded-md bg-bg border border-border px-3 py-2 text-sm font-mono text-fg focus:outline-none focus:border-accent",value:t,onChange:c=>o(c.target.value),placeholder:`email,display_name,department 4[email protected],Alice Doe,Sales 5[email protected],Bob Roe,Engineering`})]}),i&&e.jsx(ne,{message:i})]})}function ze({headers:t,rows:o,mappings:a,onChange:i,attrs:l,issues:c}){const{t:r}=A("app");function s(y,h){const N=a.slice();N[y]=h,i(N)}const k=5;return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("p",{className:"text-xs text-fg-dim",children:[r("importWizard.map.helpBefore")," ",e.jsx("b",{children:r("importWizard.map.newAttributeBold")})," ",r("importWizard.map.helpAfter")]}),c.length>0&&e.jsx("div",{className:"rounded-md border border-amber-500/40 bg-amber-500/10 text-amber-200 text-xs px-3 py-2",children:e.jsx("ul",{className:"list-disc pl-4 space-y-0.5",children:c.map(y=>e.jsx("li",{children:y},y))})}),e.jsx("div",{className:"border border-border rounded-md overflow-hidden",children:e.jsxs(oe,{children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx(W,{children:r("importWizard.map.colCsvColumn")}),e.jsx(W,{children:r("importWizard.map.colSampleValues")}),e.jsx(W,{children:r("importWizard.map.colMapTo")}),e.jsx(W,{children:r("importWizard.map.colDetails")})]})}),e.jsx("tbody",{children:t.map((y,h)=>{const N=o.slice(0,k).map(x=>x[h]??"");return e.jsxs("tr",{children:[e.jsx(_,{className:"font-mono text-xs",children:y||r("importWizard.map.colFallback",{num:h+1})}),e.jsx(_,{className:"text-xs text-fg-dim max-w-[240px] truncate",children:N.filter(
5Boolean).join(", ")||r("importWizard.map.empty")}),e.jsx(_,{children:e.jsx(Se,{value:a[h].target,onChange:x=>s(h,{header:y,target:x}),attrs:l,headerName:y,columnValues:o.map(x=>x[h]??"")})}),e.jsx(_,{children:a[h].target.kind==="new_attr"&&e.jsx(Ae,{target:a[h].target,onChange:x=>s(h,{header:y,target:x})})})]},h)})})]})})]})}function Se({value:t,onChange:o,attrs:a,headerName:i,columnValues:l}){const{t:c}=A("app"),r=We(t);return e.jsxs("select",{value:r,onChange:s=>o(_e(s.target.value,i,l,a)),className:"rounded-md bg-bg border border-border px-2 py-1 text-sm text-fg",children:[e.jsx("option",{value:"ignore",children:c("importWizard.targetPicker.ignore")}),e.jsx("option",{value:"email",children:c("importWizard.targetPicker.email")}),e.jsx("option",{value:"display_name",children:c("importWizard.targetPicker.displayName")}),e.jsx("option",{value:"active",children:c("importWizard.targetPicker.active")}),e.jsx("optgroup",{label:c("importWizard.targetPicker.existingAttribute"),children:a.map(s=>e.jsxs("option",{value:`existing:${s.key}`,children:[s.label," (",s.key,")"]},s.key))}),e.jsx("option",{value:"new",children:c("importWizard.targetPicker.newAttribute")})]})}function We(t){switch(t.kind){case"ignore":return"ignore";case"email":return"email";case"display_name":return"display_name";case"active":return"active";case"existing_attr":return`existing:${t.key}`;case"new_attr":return"new"}}function _e(t,o,a,i){if(t==="ignore")return{kind:"ignore"};if(t==="email")return{kind:"email"};if(t==="display_name")return{kind:"display_name"};if(t==="active")return{kind:"active"};if(t.startsWith("existing:")){const c=t.slice(9),r=i.find(s=>s.key===c);return{kind:"existing_attr",key:(r==null?void 0:r.key)??c}}const l=Array.from(new Set(a.map(c=>c.trim()).filter(Boolean))).slice(0,50);return{kind:"new_attr",key:he(o||"attr"),label:o||"New attribute",type:"select",options:l}}function Ae({target:t,onChange:o}){const{t:a}=A("app");return e.jsxs("div",{className:"space-y-1 min-w-[260px]",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-1",children:[e.jsx(q,{className:"h-7 py-0.5 text-xs",value:t.key,onChange:i=>o({...t,key:i.target.value}),placeholder:a("importWizard.newAttr.keyPlaceholder")}),e.jsx(q,{className:"h-7 py-0.5 text-xs",value:t.label,onChange:i=>o({...t,label:i.target.value}),placeholder:a("importWizard.newAttr.labelPlaceholder")})]}),e.jsxs("div",{className:"text-[10px] text-fg-dim",children:[a("importWizard.newAttr.optionsDerived",{count:t.options.length})," ",t.options.slice(0,4).join(", "),t.options.length>4&&` ${a("importWizard.newAttr.plusMore",{count:t.options.length-4})}`]})]})}function Le({toCreate:t,toUpdate:o,invalidRows:a,newAttrs:i,previewRows:l,runError:c}){const{t:r}=A("app");return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-4 gap-2",children:[e.jsx(Q,{label:r("importWizard.confirm.willCreate"),value:t}),e.jsx(Q,{label:r("importWizard.confirm.willUpdate"),value:o}),e.jsx(Q,{label:r("importWizard.confirm.newAttributes"),value:i.length}),e.jsx(Q,{label:r("importWizard.confirm.invalidRows"),value:a.length,tone:a.length>0?"warn":"default"})]}),i.length>0&&e.jsxs(xe,{children:[e.jsx("div",{className:"text-sm font-semibold text-fg mb-1",children:r("importWizard.confirm.newAttrsToCreate")}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:i.map(s=>s.target.kind!=="new_attr"?null:e.jsxs(Z,{tone:"accent",children:[s.target.label," (",s.target.key,") â"," ",r("importWizard.confirm.attrOptionsCount",{count:s.target.options.length})]},s.target.key))})]}),a.length>0&&e.jsxs("div",{className:"rounded-md border border-amber-500/40 bg-amber-500/10 text-amber-200 text-xs px-3 py-2",children:[e.jsx("div",{className:"font-semibold mb-1",children:r("importWizard.confirm.rowsSkipped",{count:a.length})}),e.jsxs("div",{className:"max-h-20 overflow-y-auto font-mono",children:[a.slice(0,10).map(s=>e.jsx("div",{children:r("importWizard.confirm.rowLine",{num:s.idx+2,email:s.email||r("importWizard.confirm.empty")})},s.idx)),a.length>10&&e.jsx("div",{children:r("importWizard.confirm.plusMore",{count:a.length-10})})]})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-semibold text-fg mb-1",children:r("importWizard.confirm.previewHeading")}),e.jsx("div",{className:"border border-border rounded-md max-h-64 overflow-auto",children:e.jsxs(oe,{children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx(W,{children:r("importWizard.confirm.colEmail")}),e.jsx(W,{children:r("importWizard.confirm.colAction")}),e.jsx(W,{children:r("importWizard.confirm.colDisplayName")}),e.jsx(W,{children:r("importWizard.confirm.colAttributes")})]})}),e.jsx("tbody",{children:l.slice(0,5).map(s=>e.jsxs("tr",{children:[e.jsx(_,{className:"font-mono text-xs",children:s.email||"â"}),e.jsx(_,{children:s.valid?s.willUpdate?e.jsx(Z,{tone:"warn",children:r("importWizard.confirm.actionUpdate")}):e.jsx(Z,{tone:"ok",children:r("importWizard.confirm.actionCreate")}):e.jsx(Z,{tone:"danger",children:r("importWizard.confirm.actionSkip")})}),e.jsx(_,{className:"text-xs",children:s.displayName??"â"}),e.jsx(_,{className:"text-[11px] text-fg-dim",children:Object.entries(s.attributes).filter(([,k])=>k!==null).map(([k,y])=>`${k}=${y}`).join(", ")||"â"})]},s.idx))})]})})]}),c&&e.jsx(ne,{message:c})]})}function Pe({result:t,onClose:o}){const{t:a}=A("app");return e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"rounded-md border border-emerald-500/40 bg-emerald-500/10 text-emerald-200 text-sm px-3 py-3",children:a("importWizard.result.finished")}),e.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[e.jsx(Q,{label:a("importWizard.result.created"),value:t.created}),e.jsx(Q,{label:a("importWizard.result.updated"),value:t.updated}),e.jsx(Q,{label:a("importWizard.result.newAttributes"),value:t.newAttrs})]}),e.jsx("div",{className:"flex justify-end pt-2",children:e.jsx(S,{onClick:o,children:a("importWizard.result.done")})})]})}function Ee({step:t,canAdvance:o,running:a,onBack:i,onClose:l,onNext:c,nextLabel:r}){const{t:s}=A("app");return e.jsxs("div",{className:"flex justify-between w-full",children:[e.jsx("div",{children:t!=="upload"&&e.jsx(S,{variant:"secondary",onClick:i,disabled:a,children:s("importWizard.footer.back")})}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(S,{variant:"secondary",onClick:l,disabled:a,children:s("importWizard.footer.cancel")}),e.jsx(S,{onClick:c,disabled:!o||a,children:r})]})]})}function Me({step:t}){const{t:o}=A("app"),a=[{id:"upload",label:o("importWizard.steps.upload")},{id:"map",label:o("importWizard.steps.map")},{id:"confirm",label:o("importWizard.steps.confirm")}],i=a.findIndex(l=>l.id===t);return e.jsx("div",{className:"flex gap-1.5 text-xs mb-3 pb-2 border-b border-border",children:a.map((l,c)=>{const r=c<i,s=c===i;return e.jsx("div",{className:"px-2 py-1 rounded",style:{color:s?"#fff":r?"var(--color-fg)":"var(--color-fg-dim)",backgroundColor:s?"#4f8cff":"transparent",fontWeight:s?600:400},children:l.label},l.id)})})}function Q({label:t,value:o,tone:a="default"}){const i=a==="warn"?"text-amber-300":"text-fg";return e.jsxs("div",{className:"rounded-md border border-border bg-bg/60 px-3 py-2",children:[e.jsx("div",{className:"text-[11px] text-fg-dim uppercase tracking-wide",children:t}),e.jsx("div",{className:`text-lg font-semibold mt-0.5 ${i}`,children:o})]})}function Fe(t){return new Set(t.map(o=>o.email.toLowerCase()))}function Te(t,o){const a=t.trim();if(a==="")return null;if(o==="number"){const i=Number(a);return Number.isNaN(i)?a:i}return a}function me(t){return t==null?"":String(t)}function qe(t){const o=t.lastIndexOf("@");return o===-1?{local:t,domain:""}:{local:t.slice(0,o),domain:t.slice(o+1)}}function pe({initial:t,attrDefs:o,emailDomains:a,onSave:i,onCancel:l,submitLabel:c,emailDisabled:r}){const{t:s}=A("app"),k=a.length>0&&!r,y=(t==null?void 0:t.email)??"",h=qe(y),N=k&&a.includes(h.domain)?h.domain:a[0]??"",[x,I]=g.useState(y),[L,X]=g.useState(h.local),[j,F]=g.useState(N),[K,D]=g.useState((t==null?void 0:t.display_name)??""),[H,u]=g.useState(()=>{const n={};for(const d of o)d.value_type!=="multiselect"&&(n[d.key]=me(t==null?void 0:t.attributes[d.key]));return n}),[b,T]=g.useState(()=>{const n={};for(const d of o){if(d.value_type!=="multiselect")continue;const p=t==null?void 0:t.attributes[d.key];Array.isArray(p)?n[d.key]=p.map(String):p!=null&&p!==""?n[d.key]=[String(p)]:n[d.key]=[]}return n}),[P,O]=g.useState(()=>t?Object.entries(t.attributes).filter(([n])=>n.startsWith("contact_")).map(([n,d])=>({channel:n.slice(8),value:me(d),locked:!1})):[]),[R,G]=g.useState(!1),[Y,C]=g.useState(null),[$,ee]=g.useState(!1);function te(n,d){u(p=>({...p,[n]:d}))}function se(n,d){T(p=>{const w=p[n]??[],E=w.includes(d)?w.filter(f=>f!==d):[...w,d];return{...p,[n]:E}})}function B(n){G(!1),O(d=>[...d,n==="custom"?{channel:"",value:"",locked:!1}:{channel:n,value:"",locked:!0}])}function re(n,d){O(p=>p.map((w,E)=>E===n?{...w,...d}:w))}function m(n){O(d=>d.filter((p,w)=>w!==n))}async function v(n){n.preventDefault(),C(null);let d;if(k){const f=L.trim().toLowerCase();if(!f){C(s("employees.form.errors.localPartRequired"));return}if(f.includes("@")){C(s("employees.form.errors.noAtSign"));return}if(!j||!a.includes(j)){C(s("employees.form.errors.pickDomain"));return}d=`${f}@${j}`}else if(d=x.trim().toLowerCase(),!d){C(s("employees.form.errors.emailRequired"));return}const p={};for(const f of o){if(f.value_type==="multiselect"){const J=b[f.key]??[];J.length===0&&t?p[f.key]=null:J.length>0&&(p[f.key]=J);continue}const z=H[f.key]??"",M=Te(z,f.value_type==="number"?"number":"text");M===null&&t?p[f.key]=null:M!==null&&(p[f.key]=M)}const w=new Set,E=new Set;for(const f of P){const z=f.channel.trim().toLowerCase(),M=f.value.trim();if(!z&&!M)continue;if(!z){C(s("employees.form.errors.contactChannelRequired"));return}if(!/^[a-z][a-z0-9_-]{0,55}$/.test(z)){C(s("employees.form.errors.contactChannelInvalid",{channel:z}));return}if(w.has(z)){C(s("employees.form.errors.contactChannelDuplicate",{channel:z}));return}w.add(z);const J=`contact_${z}`;M&&(p[J]=M,E.add(J))}if(t)for(const f of Object.keys(t.attributes))f.startsWith("contact_")&&!E.has(f)&&(p[f]=null);ee(!0);try{await i({email:d,display_name:K.trim()||null,attributes:p})}catch(f){C(f instanceof le?f.message:s("employees.form.errors.unexpected"))}finally{ee(!1)}}return e.jsxs("form",{onSubmit:v,className:"space-y-4",id:"employee-form",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(V,{htmlFor:"emp-email",children:s("employees.form.email")}),k?e.jsxs("div",{className:"flex items-stretch gap-1",children:[e.jsx(q,{id:"emp-email",value:L,onChange:n=>X(n.target.value),placeholder:s("employees.form.localPartPlaceholder"),required:!0,className:"flex-1 rounded-r-none"}),a.length===1?e.jsxs("span",{className:"inline-flex items-center px-3 bg-bg border border-l-0 border-border rounded-md rounded-l-none text-sm font-mono text-fg-dim",children:["@",a[0]]}):e.jsx("select",{value:j,onChange:n=>F(n.target.value),className:"rounded-md rounded-l-none bg-bg border border-l-0 border-border px-2 text-sm font-mono text-fg focus:outline-none focus:border-accent","aria-label":s("employees.form.domainAriaLabel"),children:a.map(n=>e.jsxs("option",{value:n,children:["@",n]},n))})]}):e.jsx(q,{id:"emp-email",type:"email",value:x,onChange:n=>I(n.target.value),disabled:r,required:!0}),k&&e.jsxs("p",{className:"text-[11px] text-fg-dim mt-1",children:[s("employees.form.domainsHintPrefix")," ",e.jsx(fe,{to:"/settings",className:"underline hover:text-accent",children:s("employees.form.settingsLink")}),s("employees.form.domainsHintSuffix")]})]}),e.jsxs("div",{children:[e.jsx(V,{htmlFor:"emp-name",children:s("employees.form.displayName")}),e.jsx(q,{id:"emp-name",value:K,onChange:n=>D(n.target.value),placeholder:s("employees.form.optionalPlaceholder")})]})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-fg-dim",children:s("employees.form.contacts")}
5),e.jsxs("div",{className:"relative",children:[e.jsx(S,{type:"button",variant:"secondary",onClick:()=>G(n=>!n),children:s("employees.form.addContact")}),R&&e.jsxs("div",{className:"absolute right-0 mt-1 z-10 min-w-40 rounded-md border border-border bg-surface shadow-lg overflow-hidden",children:[e.jsx("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm hover:bg-bg cursor-pointer",onClick:()=>B("phone"),children:s("employees.form.contactKind.phone")}),e.jsx("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm hover:bg-bg cursor-pointer",onClick:()=>B("slack"),children:s("employees.form.contactKind.slack")}),e.jsx("button",{type:"button",className:"w-full text-left px-3 py-2 text-sm hover:bg-bg cursor-pointer border-t border-border",onClick:()=>B("custom"),children:s("employees.form.contactKind.custom")})]})]})]}),P.length===0?e.jsx("p",{className:"text-xs text-fg-dim",children:s("employees.form.contactsEmpty")}):e.jsx("div",{className:"space-y-2",children:P.map((n,d)=>e.jsxs("div",{className:"grid grid-cols-12 gap-2",children:[n.locked?e.jsx("div",{className:"col-span-4 inline-flex items-center px-3 bg-bg border border-border rounded-md text-sm text-fg-dim font-mono text-xs",children:n.channel==="phone"?s("employees.form.contactKind.phone"):n.channel==="slack"?s("employees.form.contactKind.slack"):n.channel}):e.jsx(q,{className:"col-span-4 font-mono text-xs",placeholder:s("employees.form.channelPlaceholder"),value:n.channel,onChange:p=>re(d,{channel:p.target.value})}),e.jsx(q,{className:"col-span-7",placeholder:n.channel==="phone"?"+1 555 0100":n.channel==="slack"?"U01ABC2DEF3":s("employees.form.valuePlaceholder"),value:n.value,onChange:p=>re(d,{value:p.target.value})}),e.jsx(S,{type:"button",variant:"ghost",className:"col-span-1",onClick:()=>m(d),"aria-label":s("employees.form.removeAriaLabel"),children:"Ã"})]},d))})]}),o.length>0&&e.jsxs("div",{className:"border-t border-border pt-4",children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-fg-dim mb-3",children:s("employees.form.attributes")}),e.jsx("div",{className:"grid grid-cols-2 gap-3",children:o.map(n=>n.value_type==="multiselect"?e.jsx(Ie,{def:n,selected:b[n.key]??[],onToggle:d=>se(n.key,d)},n.id):e.jsx($e,{def:n,value:H[n.key]??"",onChange:d=>te(n.key,d)},n.id))})]}),Y&&e.jsx(ne,{message:Y}),e.jsxs("div",{className:"flex gap-2 justify-end pt-2",children:[e.jsx(S,{type:"button",variant:"secondary",onClick:l,children:s("employees.form.cancel")}),e.jsx(S,{type:"submit",disabled:$,children:$?s("employees.form.saving"):c})]})]})}function $e({def:t,value:o,onChange:a}){const{t:i}=A("app"),l=`field-${t.key}`;return t.value_type==="select"&&Array.isArray(t.options)?e.jsxs("div",{children:[e.jsx(V,{htmlFor:l,children:t.label}),e.jsxs("select",{id:l,value:o,onChange:c=>a(c.target.value),className:"w-full rounded-md bg-bg border border-border px-3 py-2 text-sm text-fg focus:outline-none focus:border-accent",children:[e.jsx("option",{value:"",children:i("employees.form.selectNone")}),t.options.map(c=>e.jsx("option",{value:c,children:c},c))]})]}):e.jsxs("div",{children:[e.jsx(V,{htmlFor:l,children:t.label}),e.jsx(q,{id:l,type:t.value_type==="number"?"number":"text",value:o,onChange:c=>a(c.target.value)})]})}function Ie({def:t,selected:o,onToggle:a}){const{t:i}=A("app"),l=Array.isArray(t.options)?t.options:[],c=new Set(o);return e.jsxs("div",{children:[e.jsx(V,{children:t.label}),e.jsxs("div",{className:"flex flex-wrap gap-1",children:[l.length===0&&e.jsx("span",{className:"text-xs text-fg-dim italic",children:i("employees.form.noOptions")}),l.map(r=>{const s=c.has(r);return e.jsx("button",{type:"button",onClick:()=>a(r),className:"text-xs px-2 py-1 rounded border font-medium transition",style:s?{backgroundColor:"#4f8cff",color:"#fff",borderColor:"#4f8cff",boxShadow:"0 0 0 2px rgba(79,140,255,0.22)"}:{backgroundColor:"transparent",color:"var(--color-fg-dim)",border:"1px solid var(--color-border)"},children:r},r)})]})]})}function Be(){const{t}=A("app"),o=ue(),{me:a,isDemo:i}=ge(),[l,c]=g.useState(""),[r,s]=g.useState({kind:"none"}),[k,y]=g.useState(!1),h=(a==null?void 0:a.organization.email_domains)??[],N=!i&&ye(a==null?void 0:a.user.role);async function x(){const u=be(),b=await fetch("/api/v1/employees/export.csv",{headers:u?{Authorization:`Bearer ${u}`}:{}});if(!b.ok){alert(t("employees.exportFailed",{status:b.status}));return}const T=await b.blob(),P=document.createElement("a");P.href=URL.createObjectURL(T),P.download="employees.csv",P.click(),URL.revokeObjectURL(P.href)}const I=ce({queryKey:["org-attributes"],queryFn:()=>U("/org-attributes")}),L=g.useMemo(()=>I.data??[],[I.data]),X=g.useMemo(()=>{const u=new Map;for(const b of L)u.set(b.key,b.label);return u},[L]),j=ce({queryKey:["employees",{q:l}],queryFn:()=>U("/employees",{query:{q:l||void 0}})}),F=j.data??[],K=ae({mutationFn:u=>U("/employees",{method:"POST",body:u}),onSuccess:()=>{o.invalidateQueries({queryKey:["employees"]}),s({kind:"none"})}}),D=ae({mutationFn:({id:u,body:b})=>U(`/employees/${u}`,{method:"PATCH",body:b}),onSuccess:()=>{o.invalidateQueries({queryKey:["employees"]}),s({kind:"none"})}}),H=ae({mutationFn:u=>U(`/employees/${u}`,{method:"DELETE"}),onSuccess:()=>o.invalidateQueries({queryKey:["employees"]})});
5return e.jsxs("div",{className:"max-w-6xl mx-auto space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-2",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("h1",{className:"text-xl font-semibold text-fg",children:t("employees.title")}),h.length>0&&e.jsxs(fe,{to:"/settings",className:"inline-flex items-center gap-1.5 rounded-full bg-bg border border-border px-2 py-0.5 text-xs text-fg-dim hover:text-accent hover:border-accent",title:t("employees.manageInSettings"),children:[t("employees.domainLock")," ",e.jsx("code",{className:"text-fg",children:h.length===1?`@${h[0]}`:t("employees.domainCount",{count:h.length})})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(q,{className:"max-w-xs",placeholder:t("employees.searchPlaceholder"),value:l,onChange:u=>c(u.target.value)}),e.jsx(S,{variant:"secondary",onClick:x,children:t("employees.exportCsv")}),N&&e.jsx(S,{variant:"secondary",onClick:()=>y(!0),children:t("employees.importCsv")}),N&&e.jsx(S,{onClick:()=>s({kind:"create"}),children:t("employees.addEmployee")})]})]}),e.jsxs(xe,{children:[j.isLoading&&e.jsx("div",{className:"text-fg-dim text-sm",children:t("employees.loading")}),j.isError&&e.jsx(ne,{message:j.error instanceof le?j.error.message:t("employees.loadFailed")}),F.length===0&&!j.isLoading&&e.jsxs("div",{className:"text-fg-dim text-sm text-center py-6",children:[t("employees.empty.none"),N&&e.jsxs(e.Fragment,{children:[" ",e.jsx("button",{className:"text-accent hover:underline",onClick:()=>s({kind:"create"}),children:t("employees.empty.addFirst")}),"."]})]}),F.length>0&&e.jsxs(oe,{children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx(W,{children:t("employees.table.email")}),e.jsx(W,{children:t("employees.table.name")}),e.jsx(W,{children:t("employees.table.active")}),e.jsx(W,{children:t("employees.table.attributes")}),e.jsx(W,{className:"w-32",children:t("employees.table.actions")})]})}),e.jsx("tbody",{children:F.map(u=>e.jsxs("tr",{children:[e.jsx(_,{className:"font-mono text-xs",children:u.email}),e.jsx(_,{children:u.display_name??"â"}),e.jsx(_,{children:e.jsx(Z,{tone:u.active?"ok":"default",children:u.active?t("employees.table.statusActive"):t("employees.table.statusInactive")})}),e.jsx(_,{className:"text-xs text-fg-dim",children:Object.entries(u.attributes).length===0?e.jsx("span",{children:"â"}):e.jsx("div",{className:"flex flex-wrap gap-1",children:Object.entries(u.attributes).map(([b,T])=>{const P=b.startsWith("contact_")?b==="contact_phone"?t("employees.contactLabel.phone"):b==="contact_slack"?t("employees.contactLabel.slack"):b.slice(8):X.get(b)??b,O=Array.isArray(T)?T.map(de).join(", "):de(String(T));return e.jsxs("span",{className:"inline-flex items-center gap-1 rounded bg-bg border border-border px-1.5 py-0.5",title:b,children:[e.jsxs("span",{className:"text-fg-dim",children:[P,":"]}),e.jsx("span",{className:"text-fg",children:O})]},b)})})}),e.jsx(_,{children:N?e.jsxs("div",{className:"flex gap-1",children:[e.jsx(S,{variant:"secondary",onClick:()=>s({kind:"edit",emp:u}),children:t("employees.table.edit")}),u.active&&e.jsx(S,{variant:"ghost",onClick:()=>{confirm(t("employees.confirmDeactivate",{email:u.email}))&&H.mutate(u.id)},children:t("employees.table.deactivate")})]}):e.jsx("span",{className:"text-xs text-fg-dim",children:"â"})})]},u.id))})]})]}),e.jsx(ie,{open:r.kind==="create",onClose:()=>s({kind:"none"}),title:t("employees.modal.addTitle"),size:"lg",children:I.isLoading?e.jsx("div",{className:"text-fg-dim text-sm",children:t("employees.modal.loadingAttributes")}):e.jsx(pe,{attrDefs:L,emailDomains:h,onSave:async u=>{await K.mutateAsync(u)},onCancel:()=>s({kind:"none"}),submitLabel:t("employees.modal.create")})}),e.jsx(ie,{open:r.kind==="edit",onClose:()=>s({kind:"none"}),title:r.kind==="edit"?e.jsxs(e.Fragment,{children:[t("employees.modal.editTitle")," ",e.jsx("code",{className:"font-mono text-xs text-fg-dim",children:r.emp.email})]}):"",size:"lg",children:r.kind==="edit"&&e.jsx(pe,{initial:r.emp,emailDisabled:!0,attrDefs:L,emailDomains:h,onSave:async u=>
5{await D.mutateAsync({id:r.emp.id,body:{display_name:u.display_name,attributes:u.attributes}})},onCancel:()=>s({kind:"none"}),submitLabel:t("employees.modal.saveChanges")})}),k&&e.jsx(we,{onClose:()=>y(!1),onImported:()=>{},attrs:L,existingEmails:Fe(F)})]})}export{Be as EmployeesPage};
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.