PageSourceSearch

https://lexuvoapp.lovable.app/assets/MemberProfile-BasDzN7X.js

js lexuvoapp.lovable.app collected 2026-10-03 03:37:08 UTC 94,624 bytes, 1 lines download raw bytes

1import{ae as te,G as ae,r as a,g as p,j as e,af as be,ag as ke,ah as Ce,ai as Se,aj as Je,aR as qe,I as Y,y as Be,B as L,aA as Ve,J as De,K as Oe,O as Re,P as Le,Q as de,da as je,ab as Ye,d8 as As,am as U,dc as ns,at as W,dd as rs,L as Ts,a3 as Ds,a2 as Os,ak as hs,a1 as ce,aq as me,aP as is,cU as Ie,al as Rs,b1 as we,V as Ls,M as fs,f as ee,b as ss,Y as Is,U as qs,S as Ms,a5 as gs,C as X,x as ls,ap as zs,t as $s,v as Ps,db as ts,A as Us,b2 as Fs,H as Gs,$ as Bs,aK as Vs,i as cs,Z as os,a9 as Hs,de as Ws,aF as Ys,aa as Js,b0 as $e,z as js,ax as Zs,c4 as Ks,ch as Xs,c5 as Qs,cn as oe,ay as as,ca as et}from"./index-Ch9yA5uW.js";import{u as Me}from"./useRequireFreshAuth-3huKDQD5.js";import{E as ds}from"./EmptyState-DufBUen1.js";import{A as vs,a as _s,b as Ns,c as ys,d as ws,e as bs,f as ks,g as Cs}from"./alert-dialog-BNA5E5RB.js";import{T as st,a as tt,b as ms,c as Pe,d as at,e as Ue}from"./table-CRYU_QgS.js";import{M as He}from"./map-pin-C9_p6z7_.js";import{G as We}from"./globe-BNFgsq8_.js";import{E as xs}from"./eye-QooeNKB1.js";import{I as Ze}from"./info-DPrz50HW.js";import{T as it}from"./TimeZoneSelector-DDLI3_4_.js";import{T as Ss}from"./trash-2-pS8w8NGY.js";import{L as nt}from"./link-2-NUI3wxo7.js";import{U as rt}from"./unlink-Cg3c0B9Z.js";import{S as Es}from"./save-xa0DitZq.js";import{R as lt}from"./router-DUol_ajg.js";import{C as ct}from"./coins-CACmFZfc.js";import{D as ot}from"./dollar-sign-r1oYqqpc.js";import{m as dt,f as mt}from"./auditLabels-BlCYCnmy.js";import{C as xt}from"./crown-C46WLW6O.js";import{S as ut}from"./smartphone-C1MSJV-w.js";import{B as pt}from"./ban-BVCV0pY_.js";import{B as ht}from"./building-BGMYB-C0.js";import{f as ft}from"./formatDistanceToNow-Cw-FGoNP.js";import"./compareAsc-CEu8Jeqn.js";import"./differenceInCalendarMonths-C00eu0AI.js";import"./endOfDay-CRa6bmAZ.js";import"./endOfMonth-DT4Y5kxz.js";function gt({open:t,onOpenChange:v,membershipId:g,memberName:x,existingClientIds:b,onGranted:S}){const{workspaceId:f}=te(),{toast:k}=ae(),N=Me(),[$,y]=a.useState([]),[o,E]=a.useState(!1),[R,D]=a.useState(""),[r,C]=a.useState(null),[h,A]=a.useState(!1);a.useEffect(()=>{!t||!f||(E(!0),p.from("clients").select("id, name, client_code").eq("workspace_id",f).order("name").then(({data:u})=>{y(u??[]),E(!1)}),C(null),D(""))},[t,f]);const j=a.useMemo(()=>{const u=R.toLowerCase();return $.filter(n=>!b.includes(n.id)).filter(n=>{var _,P;return!u||((_=n.name)==null?void 0:_.toLowerCase().includes(u))||((P=n.client_code)==null?void 0:P.toLowerCase().includes(u))})},[$,R,b]),c=$.find(u=>u.id===r),d=async()=>{if(!(!r||!f||!await N("Assign client scope"))){A(!0);try{const{data:n,error:_}=await p.functions.invoke("rbac-admin",{body:{action:"grant_scope",workspace_id:f,membership_id:g,scope_type:"assigned_clients",client_id:r}});if(_)throw _;if(n!=null&&n.error)throw new Error(n.error);k({title:"Client scope granted",description:`${c==null?void 0:c.name} assigned to ${x}`}),v(!1),S()}catch(n){k({title:"Error",description:n.message,variant:"destructive"})}A(!1)}};return e.jsx(be,{open:t,onOpenChange:v,children:e.jsxs(ke,{className:"max-w-sm",children:[e.jsxs(Ce,{children:[e.jsx(Se,{className:"text-sm",children:"Assign Client Scope"}),e.jsxs(Je,{className:"text-xs",children:["Grant ",e.jsx("span",{className:"font-medium text-foreground",children:x})," access to a specific client."]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(qe,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(Y,{className:"h-8 text-xs pl-8",placeholder:"Search clients…",value:R,onChange:u=>D(u.target.value)})]}),e.jsx("div",{className:"border rounded-lg max-h-48 overflow-y-auto",children:o?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:"Loading…"}):j.length===0?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:R?"No matching clients.":"No unassigned clients available."}):j.map(u=>e.jsxs("button",{type:"button",className:`w-full flex items-center gap-2 px-3 py-2 text-left text-xs hover:bg-muted/50 transition-colors ${r===u.id?"bg-primary/10":""}`,onClick:()=>C(u.id),children:[e.jsx(Be,{className:"h-3.5 w-3.5 text-muted-foreground shrink-0"}),e.jsx("span",{className:"font-medium truncate",children:u.name}),u.client_code&&e.jsx("span",{className:"text-muted-foreground ml-auto shrink-0",children:u.client_code})]},u.id))}),c&&e.jsxs("div",{className:"rounded-lg border bg-muted/20 p-2.5 text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Selected: "}),e.jsx("span",{className:"font-medium",children:c.name})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(L,{variant:"outline",size:"sm",className:"h-8 text-xs",onClick:()=>v(!1),children:"Cancel"}),e.jsx(L,{size:"sm",className:"h-8 text-xs",onClick:d,disabled:!r||h,children:h?"Granting…":"Grant Access"})]})]})]})})}function jt({open:t,onOpenChange:v,membershipId:g,memberName:x,existingEntityIds:b,onGranted:S}){var u;const{workspaceId:f}=te(),{toast:k}=ae(),N=Me(),[$,y]=a.useState([]),[o,E]=a.useState(!1),[R,D]=a.use
1State(""),[r,C]=a.useState(null),[h,A]=a.useState(!1);a.useEffect(()=>{!t||!f||(E(!0),p.from("entities").select("id, company_name, company_number, client_id, clients(name)").eq("workspace_id",f).order("company_name").then(({data:n})=>{y(n??[]),E(!1)}),C(null),D(""))},[t,f]);const j=a.useMemo(()=>{const n=R.toLowerCase();return $.filter(_=>!b.includes(_.id)).filter(_=>{var P,m;return!n||((P=_.company_name)==null?void 0:P.toLowerCase().includes(n))||((m=_.company_number)==null?void 0:m.toLowerCase().includes(n))})},[$,R,b]),c=$.find(n=>n.id===r),d=async()=>{if(!(!r||!f||!await N("Assign entity scope"))){A(!0);try{const{data:_,error:P}=await p.functions.invoke("rbac-admin",{body:{action:"grant_scope",workspace_id:f,membership_id:g,scope_type:"assigned_entities",entity_id:r}});if(P)throw P;if(_!=null&&_.error)throw new Error(_.error);k({title:"Entity scope granted",description:`${c==null?void 0:c.company_name} assigned to ${x}`}),v(!1),S()}catch(_){k({title:"Error",description:_.message,variant:"destructive"})}A(!1)}};return e.jsx(be,{open:t,onOpenChange:v,children:e.jsxs(ke,{className:"max-w-sm",children:[e.jsxs(Ce,{children:[e.jsx(Se,{className:"text-sm",children:"Assign Entity Scope"}),e.jsxs(Je,{className:"text-xs",children:["Grant ",e.jsx("span",{className:"font-medium text-foreground",children:x})," access to a specific entity."]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(qe,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(Y,{className:"h-8 text-xs pl-8",placeholder:"Search entities…",value:R,onChange:n=>D(n.target.value)})]}),e.jsx("div",{className:"border rounded-lg max-h-48 overflow-y-auto",children:o?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:"Loading…"}):j.length===0?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:R?"No matching entities.":"No unassigned entities available."}):j.map(n=>{var _;return e.jsxs("button",{type:"button",className:`w-full flex items-center gap-2 px-3 py-2 text-left text-xs hover:bg-muted/50 transition-colors ${r===n.id?"bg-primary/10":""}`,onClick:()=>C(n.id),children:[e.jsx(Ve,{className:"h-3.5 w-3.5 text-muted-foreground shrink-0"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("span",{className:"font-medium truncate block",children:n.company_name}),((_=n.clients)==null?void 0:_.name)&&e.jsxs("span",{className:"text-[10px] text-muted-foreground truncate block",children:["Client: ",n.clients.name]})]}),n.company_number&&e.jsx("span",{className:"text-muted-foreground ml-auto shrink-0",children:n.company_number})]},n.id)})}),c&&e.jsxs("div",{className:"rounded-lg border bg-muted/20 p-2.5 text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Selected: "}),e.jsx("span",{className:"font-medium",children:c.company_name}),((u=c.clients)==null?void 0:u.name)&&e.jsxs("span",{className:"text-muted-foreground",children:[" (",c.clients.name,")"]})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(L,{variant:"outline",size:"sm",className:"h-8 text-xs",onClick:()=>v(!1),children:"Cancel"}),e.jsx(L,{size:"sm",className:"h-8 text-xs",onClick:d,disabled:!r||h,children:h?"Granting…":"Grant Access"})]})]})]})})}const vt=[{value:"local_only",label:"Local Only",description:"Access only this unit's data"},{value:"unit_and_children",label:"Unit & Children",description:"This unit and its sub-units"},{value:"all_units",label:"All Units",description:"Access across all operating units"}];function _t({open:t,onOpenChange:v,userId:g,memberName:x,existingOuIds:b,onGranted:S}){const{workspaceId:f}=te(),{toast:k}=ae(),[N,$]=a.useState([]),[y,o]=a.useState(!1),[E,R]=a.useState(""),[D,r]=a.useState(null),[C,h]=a.useState("local_only"),[A,j]=a.useState(!1);a.useEffect(()=>{!t||!f||(o(!0),p.from("operating_units").select("id, name, code").eq("workspace_id",f).eq("status","active").order("name").then(({data:n})=>{$(n??[]),o(!1)}),r(null),R(""),h("local_only"))},[t,f]);const c=a.useMemo(()=>{const n=E.toLowerCase();return N.filter(_=>!b.includes(_.id)).filter(_=>{var P,m;return!n||((P=_.name)==null?void 0:P.toLowerCase().includes(n))||((m=_.code)==null?void 0:m.toLowerCase().includes(n))})},[N,E,b]),d=N.find(n=>n.id===D),u=async()=>
1{if(!(!D||!f)){j(!0);try{const{error:n}=await p.from("operating_unit_memberships").insert({operating_unit_id:D,user_id:g,workspace_id:f,role:"member",access_scope:C});if(n)throw n;k({title:"Operating unit assigned",description:`${d==null?void 0:d.name} assigned to ${x}`}),v(!1),S()}catch(n){k({title:"Error",description:n.message,variant:"destructive"})}j(!1)}};return e.jsx(be,{open:t,onOpenChange:v,children:e.jsxs(ke,{className:"max-w-sm",children:[e.jsxs(Ce,{children:[e.jsx(Se,{className:"text-sm",children:"Assign Operating Unit"}),e.jsxs(Je,{className:"text-xs",children:["Grant ",e.jsx("span",{className:"font-medium text-foreground",children:x})," access to an operating unit."]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(qe,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(Y,{className:"h-8 text-xs pl-8",placeholder:"Search operating units…",value:E,onChange:n=>R(n.target.value)})]}),e.jsx("div",{className:"border rounded-lg max-h-48 overflow-y-auto",children:y?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:"Loading…"}):c.length===0?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:E?"No matching units.":"No unassigned operating units available."}):c.map(n=>e.jsxs("button",{type:"button",className:`w-full flex items-center gap-2 px-3 py-2 text-left text-xs hover:bg-muted/50 transition-colors ${D===n.id?"bg-primary/10":""}`,onClick:()=>r(n.id),children:[e.jsx(He,{className:"h-3.5 w-3.5 text-muted-foreground shrink-0"}),e.jsx("span",{className:"font-medium truncate",children:n.name}),n.code&&e.jsx("span",{className:"text-muted-foreground ml-auto shrink-0",children:n.code})]},n.id))}),d&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"rounded-lg border bg-muted/20 p-2.5 text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Selected: "}),e.jsx("span",{className:"font-medium",children:d.name})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] font-medium text-muted-foreground uppercase tracking-wider",children:"Access Scope"}),e.jsxs(De,{value:C,onValueChange:h,children:[e.jsx(Oe,{className:"h-8 text-xs mt-1",children:e.jsx(Re,{})}),e.jsx(Le,{children:vt.map(n=>e.jsx(de,{value:n.value,className:"text-xs",children:e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"font-medium",children:n.label}),e.jsx("span",{className:"text-muted-foreground text-[10px]",children:n.description})]})},n.value))})]})]})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(L,{variant:"outline",size:"sm",className:"h-8 text-xs",onClick:()=>v(!1),children:"Cancel"}),e.jsx(L,{size:"sm",className:"h-8 text-xs",onClick:u,disabled:!D||A,children:A?"Assigning…":"Assign Unit"})]})]})]})})}const Nt=[{module:"firm",label:"Firm Administration",keys:["firm.manage_settings","firm.manage_roles","firm.invite_users","firm.manage_scope_assignments","firm.view_reports","firm.view_audit"]},{module:"clients",label:"Clients / Entities / Contacts",keys:["clients.read","clients.create","clients.update","clients.archive","entities.read","entities.create","entities.update","entities.archive","contacts.read","contacts.create","contacts.update"]},{module:"tasks",label:"Tasks",keys:["tasks.read","tasks.create","tasks.edit","tasks.assign","tasks.review","tasks.approve","tasks.cancel","tasks.archive"]},{module:"compliance",label:"Compliance",keys:["compliance.read","compliance.create","compliance.edit","compliance.assign","compliance.review","compliance.approve","compliance.override"]},{module:"documents",label:"Documents",keys:["documents.read_internal","documents.upload","documents.edit_metadata","documents.delete","documents.change_visibility"]},{module:"billing",label:"Billing",keys:["billing.read","billing.create","billing.edit","billing.issue","billing.record_payment","billing.adjust","billing.writeoff","billing.configure","billing.credit_note","billing.refund","billing.approve_adjustment"]},{module:"imports",label:"Imports",keys:["imports.read","imports.upload","imports.validate","imports.commit"]},{module:"conversations",label:"Conversations",keys:["conversations.read","conversations.reply","conversations.internal_note","conversations.assign"]},{module:"audit",label:"Audit & Reports",keys:["audit.read","timeline.read","timeline.create","t
1imeline.edit","timeline.archive"]}];function yt({membership:t,canManage:v,isSelf:g,onRefresh:x,onOpenCopyDialog:b}){var Te;const{workspaceId:S}=te(),{toast:f}=ae(),k=Me(),[N,$]=a.useState(t.role),[y,o]=a.useState(!1),[E,R]=a.useState(!1),[D,r]=a.useState([]),[C,h]=a.useState([]),[A,j]=a.useState(!0),[c,d]=a.useState(new Map),[u,n]=a.useState(new Map),[_,P]=a.useState(!1),[m,q]=a.useState(!1),[O,s]=a.useState(!1),[i,T]=a.useState(!1),[B,ie]=a.useState([]),[xe,ue]=a.useState(new Map),[pe,he]=a.useState([]),[Ee,F]=a.useState(!1);a.useEffect(()=>{(async()=>{j(!0);const[{data:w},{data:M}]=await Promise.all([p.from("rbac_role_capabilities").select("capability_key").eq("role",t.role),p.from("membership_capability_overrides").select("capability_key, grant_mode").eq("membership_id",t.id)]);r((w??[]).map(z=>z.capability_key)),h(M??[]),j(!1)})()},[t.role,t.id]),a.useEffect(()=>{const l=t.scopeRules.filter(z=>!z.valid_to||new Date(z.valid_to)>new Date),w=l.filter(z=>z.client_id).map(z=>z.client_id),M=l.filter(z=>z.entity_id).map(z=>z.entity_id);w.length>0?p.from("clients").select("id, name").in("id",w).then(({data:z})=>{const J=new Map;(z??[]).forEach(H=>J.set(H.id,H.name)),d(J)}):d(new Map),M.length>0?p.from("entities").select("id, company_name").in("id",M).then(({data:z})=>{const J=new Map;(z??[]).forEach(H=>J.set(H.id,H.company_name)),n(J)}):n(new Map)},[t.scopeRules]),a.useEffect(()=>{!S||!t.user_id||p.from("operating_unit_memberships").select("id, operating_unit_id, access_scope, role").eq("workspace_id",S).eq("user_id",t.user_id).then(({data:l})=>{const w=l??[];ie(w);const M=w.map(z=>z.operating_unit_id);M.length>0?p.from("operating_units").select("id, name").in("id",M).then(({data:z})=>{const J=new Map;(z??[]).forEach(H=>J.set(H.id,H.name)),ue(J)}):ue(new Map)})},[S,t.user_id,t.scopeRules]),a.useEffect(()=>{!y||N===t.role||(F(!0),p.from("rbac_role_capabilities").select("capability_key").eq("role",N).then(({data:l})=>{he((l??[]).map(w=>w.capability_key)),F(!1)}))},[y,N,t.role]);const re=(()=>{const l=new Set(D);for(const w of C)w.grant_mode==="allow"&&l.add(w.capability_key),w.grant_mode==="deny"&&l.delete(w.capability_key);return l})(),Q=l=>{$(l),l!==t.role&&o(!0)},ve=async()=>{if(!await k("Change team member role")){o(!1);return}R(!0);try{const{data:w,error:M}=await p.functions.invoke("rbac-admin",{body:{action:"change_role",workspace_id:S,membership_id:t.id,new_role:N}});if(M)throw M;if(w!=null&&w.error)throw new Error(w.error);f({title:"Role updated",description:`Changed to ${je[N]}`}),x()}catch(w){f({title:"Error",description:w.message,variant:"destructive"}),$(t.role)}R(!1),o(!1)},le=async l=>{if(await k("Revoke scope assignment"))try{const{data:M,error:z}=await p.functions.invoke("rbac-admin",{body:{action:"revoke_scope",workspace_id:S,scope_rule_id:l}});if(z)throw z;if(M!=null&&M.error)throw new Error(M.error);f({title:"Scope revoked"}),x()}catch(M){f({title:"Error",description:M.message,variant:"destructive"})}},Ae=async l=>{if(await k("Grant scope assignment")){T(!0);try{const{data:M,error:z}=await p.functions.invoke("rbac-admin",{body:{action:"grant_scope",workspace_id:S,membership_id:t.id,scope_type:l}});if(z)throw z;if(M!=null&&M.error)throw new Error(M.error);f({title:"Scope granted",description:`${rs[l]??l} scope applied.`}),x()}catch(M){f({title:"Error",description:M.message,variant:"destructive"})}T(!1)}},ze=async l=>{try{const{error:w}=await p.from("operating_unit_memberships").delete().eq("id",l);if(w)throw w;f({title:"Operating unit removed"}),x()}catch(w){f({title:"Error",description:w.message,variant:"destructive"})}},I=t.scopeRules.filter(l=>!l.valid_to||new Date(l.valid_to)>new Date),se=[...new Set(I.map(l=>l.scope_type))],G=se.includes("firm_all"),Z=I.filter(l=>l.scope_type==="assigned_clients").length,ne=I.filter(l=>l.scope_type==="assigned_entities").length,Ke=I.filter(l=>l.client_id).map(l=>l.client_id),Xe=I.filter(l=>l.entity_id).map(l=>l.entity_id),Qe=B.map(l=>l.operating_unit_id),fe=je[t.role]??t.role,K=[];if(t.role==="firm_admin")K.push("This user has full administrative control over the firm workspace, including settings, team management, and all operational data.");else{if(G)K.push(`This user is a ${fe} who can work across the whole firm because they have firm-wide scope.`);else if(Z>0||ne>0||B.length>0){const l=[];Z>0&&l.push(`${Z} assigned client${Z!==1?"s":""}`),ne>0&&l.push(`${ne} assigned entit${ne!==1?"ies":"y"}`),B.length>0&&l.push(`${B.length} operating unit${B.length!==1?"s":""}`),K.push(`This user is a ${fe} who can operate on ${l.join(", ")}.`)}else se.includes("own_tasks")?K.push(`This user is a ${fe}
1 scoped to their own tasks only.`):K.push(`This user is a ${fe} who defaults to own tasks only because no broader scope rules are active.`);re.has("firm.manage_roles")?K.push("They can manage team access and roles."):K.push("They cannot manage team access because they do not have firm access administration capability."),re.has("firm.manage_settings")?K.push("They can modify firm settings."):K.push("They cannot modify firm settings."),re.has("billing.read")||K.push("They do not have access to billing data."),re.has("compliance.approve")||K.push("They cannot approve compliance items.")}const _e=((Te=t.profiles)==null?void 0:Te.full_name)??"this member";return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ye,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("h4",{className:"text-sm font-medium",children:"Base Role"})]}),v&&!g?e.jsxs(De,{value:N,onValueChange:Q,children:[e.jsx(Oe,{className:"h-9 text-sm w-full",children:e.jsx(Re,{})}),e.jsx(Le,{children:As.map(l=>e.jsx(de,{value:l.value,className:"text-xs",children:e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"font-medium",children:l.label}),e.jsx("span",{className:"text-muted-foreground text-[10px]",children:l.description})]})},l.value))})]}):e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(U,{variant:"outline",className:"text-xs",children:fe}),e.jsx("span",{className:"text-xs text-muted-foreground",children:ns[t.role]})]})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(We,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("h4",{className:"text-sm font-medium",children:"Data Scope"})]}),t.role==="firm_admin"?e.jsx("div",{className:"rounded-lg border bg-muted/30 p-3",children:e.jsx("p",{className:"text-xs text-muted-foreground",children:"Admins automatically have firm-wide scope. No scope restrictions apply."})}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-1.5",children:[G&&e.jsxs(U,{variant:"default",className:"text-[10px] gap-1",children:[e.jsx(We,{className:"h-2.5 w-2.5"})," Firm-wide"]}),!G&&Z>0&&e.jsxs(U,{variant:"secondary",className:"text-[10px] gap-1",children:[e.jsx(Be,{className:"h-2.5 w-2.5"})," ",Z," client",Z!==1?"s":""]}),!G&&ne>0&&e.jsxs(U,{variant:"secondary",className:"text-[10px] gap-1",children:[e.jsx(Ve,{className:"h-2.5 w-2.5"})," ",ne," entit",ne!==1?"ies":"y"]}),!G&&B.length>0&&e.jsxs(U,{variant:"secondary",className:"text-[10px] gap-1",children:[e.jsx(He,{className:"h-2.5 w-2.5"})," ",B.length," operating unit",B.length!==1?"s":""]}),!G&&Z===0&&ne===0&&B.length===0&&se.includes("own_tasks")&&e.jsxs(U,{variant:"outline",className:"text-[10px] gap-1",children:[e.jsx(xs,{className:"h-2.5 w-2.5"})," Own tasks only"]}),!G&&Z===0&&ne===0&&B.length===0&&!se.includes("own_tasks")&&e.jsx(U,{variant:"outline",className:"text-[10px] text-muted-foreground",children:"No scope assigned"})]}),I.length===0&&B.length===0?e.jsx("div",{className:"rounded-lg border bg-muted/30 p-3",children:e.jsx("p",{className:"text-xs text-muted-foreground",children:"No explicit scope rules assigned. This member defaults to own tasks only."})}):e.jsxs("div",{className:"space-y-1.5",children:[I.map(l=>e.jsxs("div",{className:"flex items-center justify-between rounded-lg border px-3 py-2",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx(U,{variant:"secondary",className:"text-[10px] shrink-0",children:rs[l.scope_type]??l.scope_type}),l.client_id&&e.jsxs("span",{className:"text-[10px] text-muted-foreground truncate flex items-center gap-1",children:[e.jsx(Be,{className:"h-2.5 w-2.5 shrink-0"}),c.get(l.client_id)??l.client_id.slice(0,8)+"…"]}),l.entity_id&&e.jsxs("span",{className:"text-[10px] text-muted-foreground truncate flex items-center gap-1",children:[e.jsx(Ve,{className:"h-2.5 w-2.5 shrink-0"}),u.get(l.entity_id)??l.entity_id.slice(0,8)+"…"]})]}),v&&!g&&l.id&&e.jsx(L,{variant:"ghost",size:"sm",className:"h-6 text-[10px] text-destructive shrink-0",onClick:()=>le(l.id),children:"Revoke"})]},l.id||`${l.scope_type}-${l.client_id}-${l.entity_id}`)),B.map(l=>{var w;return e.jsxs("div",{className:"flex items-center justify-between rounded-lg border px-3 py-2",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx(U,{variant:"secondary",className:"text-[10px] shrink-0",children:"Operating Unit"}),e.jsxs("span",{className:"text-[10px] text-muted-foreground truncate flex items-center gap-1",children:[e.jsx(He,{className:"h-2.5 w-2.5 shrink-0"}),xe.get(l.operating_unit_id)??l.operating_unit_id.slice(0,8)+"…"]}),e.jsx(U,{variant:"outline",className:"text-[10px]",children:(w=l.access_scope)==null?void 0:w.replace(/_/g," ")})]}),v&&!g&&e.jsx(L,{variant:"ghost",size:"sm",className:"h-6 text-[10px] text-destructive shrink-0",onClick:()=>ze(l.id),children:"Remove"})]},l.id)})]}),v&&!g&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("p",{className:"text-[10px] font-medium text-muted-foreground uppercase tracking-wider",children:"Grant Scope"}),e.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[!G&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-7 text-[10px] gap-1",onClick:()=>Ae("firm_all"),disabled:i,children:[e.jsx(We,{className:"h-3 w-3"})," Firm-wide"]}),!se.includes("own_tasks")&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-7 text-[10px] gap-1",onClick:()=>Ae("own_tasks"),disabled:i,children:[e.jsx(xs,{className:"h-3 w-3"})," Own Tasks"]}),e.jsxs(L,{variant:"outline",size:"sm",className:"h-7 text-[10px] gap-1",onClick:()=>P(!0),children:[e.jsx(Be,{className:"h-3 w-3"})," Add Client"]}),e.jsxs(L,{variant:"outline",size:"sm",className:"h-7 text-[10px] gap-1",onClick:()=>q(!0),children:[e.jsx(Ve,{className:"h-3 w-3"})," Add Entity"]}),e.jsxs(L,{variant:"outline",size:"sm",className:"h-7 text-[10px] gap-1",onClick:()=>s(!0),children:[e.jsx(He,{className:"h-3 w-3"})," Add Operating Unit"]})]})]})]})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ts,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("h4",{className:"text-sm font-medium",children:"Capability Summary"})]}),A?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading c
1apabilities…"}):e.jsx("div",{className:"space-y-3",children:Nt.map(l=>{const w=l.keys.filter(H=>re.has(H)),M=l.keys.filter(H=>!re.has(H));if(w.length===0&&M.length===0)return null;const z=M.length===0,J=w.length===0;return e.jsxs("div",{className:"rounded-lg border px-3 py-2 space-y-1",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-medium",children:l.label}),z&&e.jsx(U,{variant:"secondary",className:"text-[10px] bg-emerald-50 text-emerald-700 border-emerald-200",children:"Full access"}),J&&e.jsx(U,{variant:"secondary",className:"text-[10px] bg-red-50 text-red-600 border-red-200",children:"No access"}),!z&&!J&&e.jsxs(U,{variant:"secondary",className:"text-[10px]",children:[w.length,"/",l.keys.length]})]}),!z&&!J&&e.jsxs("div",{className:"flex flex-wrap gap-1 mt-1",children:[w.map(H=>{var ge;return e.jsxs("span",{className:"inline-flex items-center gap-0.5 text-[10px] text-emerald-700",children:[e.jsx(Ds,{className:"h-2.5 w-2.5"}),(ge=H.split(".").pop())==null?void 0:ge.replace(/_/g," ")]},H)}),M.slice(0,3).map(H=>{var ge;return e.jsxs("span",{className:"inline-flex items-center gap-0.5 text-[10px] text-muted-foreground",children:[e.jsx(Os,{className:"h-2.5 w-2.5"}),(ge=H.split(".").pop())==null?void 0:ge.replace(/_/g," ")]},H)}),M.length>3&&e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:["+",M.length-3," more"]})]})]},l.module)})})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ze,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("h4",{className:"text-sm font-medium",children:"Access Explanation"})]}),e.jsx("div",{className:"rounded-lg border bg-muted/20 p-3 space-y-1.5",children:K.map((l,w)=>e.jsx("p",{className:"text-xs text-muted-foreground leading-relaxed",children:l},w))})]}),v&&!g&&e.jsxs(e.Fragment,{children:[e.jsx(W,{}),e.jsxs(L,{variant:"outline",size:"sm",className:"w-full h-8 text-xs gap-1.5",onClick:b,children:[e.jsx(hs,{className:"h-3.5 w-3.5"}),"Copy Permissions from Another Member"]})]}),e.jsx(vs,{open:y,onOpenChange:l=>{l||($(t.role),o(!1))},children:e.jsxs(_s,{children:[e.jsxs(Ns,{children:[e.jsx(ys,{className:"text-sm",children:"Change Role?"}),e.jsxs(ws,{className:"text-xs space-y-2",children:[e.jsxs("span",{className:"block",children:["Change ",e.jsx("span",{className:"font-medium text-foreground",children:_e})," from ",e.jsx(U,{variant:"outline",className:"text-[10px]",children:je[t.role]})," to ",e.jsx(U,{variant:"outline",className:"text-[10px]",children:je[N]}),"."]}),e.jsx("span",{className:"block text-muted-foreground",children:ns[N]}),Ee?e.jsx("span",{className:"block text-muted-foreground",children:"Calculating capability changes…"}):(()=>{const l=new Set(D),w=new Set(pe),M=pe.filter(J=>!l.has(J)).length,z=D.filter(J=>!w.has(J)).length;return M===0&&z===0?null:e.jsxs("span",{className:"block rounded-md bg-muted/50 px-2 py-1.5 text-[11px]",children:["Will ",e.jsxs("span",{className:"text-emerald-600 font-medium",children:["gain ",M]})," and ",e.jsxs("span",{className:"text-destructive font-medium",children:["lose ",z]})," capabilit",M+z!==1?"ies":"y"," from the base role.",C.length>0&&e.jsxs("span",{className:"block text-muted-foreground mt-0.5",children:["Existing capability overrides (",C.length,") will be preserved."]})]})})()]})]}),e.jsxs(bs,{children:[e.jsx(ks,{className:"h-8 text-xs",children:"Cancel"}),e.jsx(Cs,{className:"h-8 text-xs",onClick:ve,disabled:E,children:E?"Saving…":"Confirm"})]})]})}),e.jsx(gt,{open:_,onOpenChange:P,membershipId:t.id,memberName:_e,existingClientIds:Ke,onGranted:x}),e.jsx(jt,{open:m,onOpenChange:q,membershipId:t.id,memberName:_e,existingEntityIds:Xe,onGranted:x}),e.jsx(_t,{open:O,onOpenChange:s,userId:t.user_id,memberName:_e,existingOuIds:Qe,onGranted:x})]})}const wt=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];function bt({membership:t,canManage:v,isSelf:g}){const{workspaceId:x}=te(),{toast:b}=ae(),[S,f]=a.useState(!0),[k,N]=a.useState(!1),[$,y]=a.useState(!1),[o,E]=a.useState("Europe/London"),[R,D]=a.use
1State("available"),[r,C]=a.useState([{day:0,enabled:!1,start:"09:00",end:"17:30"},{day:1,enabled:!0,start:"09:00",end:"17:30"},{day:2,enabled:!0,start:"09:00",end:"17:30"},{day:3,enabled:!0,start:"09:00",end:"17:30"},{day:4,enabled:!0,start:"09:00",end:"17:30"},{day:5,enabled:!0,start:"09:00",end:"17:30"},{day:6,enabled:!1,start:"09:00",end:"17:30"}]),[h,A]=a.useState([]),[j,c]=a.useState(!1),[d,u]=a.useState({override_date:"",end_date:"",override_type:"unavailable",custom_start:"09:00",custom_end:"17:30",reason:""}),n=v||g,_=a.useCallback(async()=>{if(!(t!=null&&t.id)||!x)return;f(!0);const{data:s}=await p.from("member_availability_profiles").select("*").eq("membership_id",t.id).maybeSingle();if(s){const i=s;E(i.timezone??"Europe/London"),D(i.status??"available"),Array.isArray(i.weekly_hours)&&C(i.weekly_hours);const{data:T}=await p.from("member_availability_overrides").select("*").eq("availability_profile_id",i.id).order("override_date",{ascending:!0});A(T??[])}f(!1),y(!1)},[t==null?void 0:t.id,x]);a.useEffect(()=>{_()},[_]);const P=async()=>{N(!0);try{const{data:s,error:i}=await p.functions.invoke("team-settings",{body:{action:"save_availability",workspace_id:x,membership_id:t.id,timezone:o,status:R,weekly_hours:r}});if(i)throw i;if(s!=null&&s.error)throw new Error(s.error);b({title:"Availability saved"}),y(!1),_()}catch(s){b({title:"Error",description:s.message,variant:"destructive"})}N(!1)},m=async()=>{if(d.override_date)try{const{data:s,error:i}=await p.functions.invoke("team-settings",{body:{action:"save_availability_override",workspace_id:x,membership_id:t.id,...d,end_date:d.end_date||null}});if(i)throw i;if(s!=null&&s.error)throw new Error(s.error);b({title:"Override added"}),c(!1),u({override_date:"",end_date:"",override_type:"unavailable",custom_start:"09:00",custom_end:"17:30",reason:""}),_()}catch(s){b({title:"Error",description:s.message,variant:"destructive"})}},q=async s=>{try{const{data:i,error:T}=await p.functions.invoke("team-settings",{body:{action:"delete_availability_override",workspace_id:x,membership_id:t.id,override_id:s}});if(T)throw T;if(i!=null&&i.error)throw new Error(i.error);b({title:"Override removed"}),_()}catch(i){b({title:"Error",description:i.message,variant:"destructive"})}},O=(s,i,T)=>{C(B=>B.map(ie=>ie.day===s?{...ie,[i]:T}:ie)),y(!0)};return S?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ce,{className:"h-4 w-4 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:"Availability Status"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Current operator status"})]}),n?e.jsxs(De,{value:R,onValueChange:s=>{D(s),y(!0)},children:[e.jsx(Oe,{className:"w-[140px] h-8 text-xs",children:e.jsx(Re,{})}),e.jsxs(Le,{children:[e.jsx(de,{value:"available",children:"Available"}),e.jsx(de,{value:"out_of_office",children:"Out of Office"}),e.jsx(de,{value:"custom",children:"Custom"})]})]}):e.jsx(U,{variant:"outline",className:"text-[10px] capitalize",children:R.replace(/_/g," ")})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("p",{className:"text-xs font-medium",children:"Time Zone"}),n?e.jsx(it,{value:o,onChange:s=>{E(s),y(!0)}}):e.jsx("p",{className:"text-xs text-muted-foreground",children:o})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-xs font-medium",children:"Weekly Working Hours"}),e.jsx("div",{className:"space-y-1.5",children:r.map(s=>e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-20",children:e.jsx("span",{className:"text-[11px] text-muted-foreground",children:wt[s.day]})}),n?e.jsxs(e.Fragment,{children:[e.jsx(me,{checked:s.enabled,onCheckedChange:i=>O(s.day,"enabled",i),className:"scale-75"}),e.jsx(Y,{type:"time",value:s.start,onChange:i=>O(s.day,"start",i.target.value),disabled:!s.enabled,className:"h-7 w-24 text-[11px]"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"to"}),e.jsx(Y,{type:"time",value:s.end,onChange:i=>O(s.day,"end",i.target.value),disabled:!s.enabled,className:"h-7 w-24 text-[11px]"})]}):e.jsx("span",{className:"text-[11px]",children:s.enabled?`${s.start} – ${s.end}`:"Off"})]},s.day))})]}),n&&$&&e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(L,{size:"sm",className:"h-7 text-xs",onClick:P,disabled:k,children:[k?e.jsx(ce,{className:"h-3 w-3 animate-spin mr-1"}):null,"Save"]}),e.jsx(L,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:_,children:"Cancel"})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs font-medium",children:"Date-Specific Overrides"}),n&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-6 text-[10px] gap-1",onClick:()=>c(!0),children:[e.jsx(is,{className:"h-3 w-3"})," Add Override"]})]}),h.length===0?e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"No date-specific overrides configured."}):e.jsx("div",{className:"space-y-1",children:h.map(s=>{var i;return e.jsxs("div",{className:"flex items-center justify-between p-2 rounded border text-[11px]",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ie,{className:"h-3 w-3 text-muted-foreground"}),e.jsxs("span",{children:[s.override_date,s.end_date?` – ${s.end_date}`:""]}),e.jsx(U,{variant:"secondary",className:"text-[9px] capitalize",children:(i=s.override_type)==null?void 0:i.replace(/_/g," ")}),s.override_type==="custom_hours"&&s.custom_start&&e.jsxs("span",{className:"text-muted-foreground",children:[s.custom_start," – ",s.custom_end]}),s.reason&&e.jsxs("span",{className:"text-muted-foreground italic",children:['"',s.reason,'"']}
1)]}),n&&e.jsx(L,{variant:"ghost",size:"sm",className:"h-5 w-5 p-0",onClick:()=>q(s.id),children:e.jsx(Ss,{className:"h-3 w-3 text-destructive"})})]},s.id)})})]}),e.jsx(be,{open:j,onOpenChange:c,children:e.jsxs(ke,{className:"sm:max-w-md",children:[e.jsx(Ce,{children:e.jsx(Se,{className:"text-sm",children:"Add Date Override"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("label",{className:"text-xs font-medium",children:"Start Date"}),e.jsx(Y,{type:"date",value:d.override_date,onChange:s=>u(i=>({...i,override_date:s.target.value})),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("label",{className:"text-xs font-medium",children:"End Date (optional)"}),e.jsx(Y,{type:"date",value:d.end_date,onChange:s=>u(i=>({...i,end_date:s.target.value})),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("label",{className:"text-xs font-medium",children:"Type"}),e.jsxs(De,{value:d.override_type,onValueChange:s=>u(i=>({...i,override_type:s})),children:[e.jsx(Oe,{className:"h-8 text-xs",children:e.jsx(Re,{})}),e.jsxs(Le,{children:[e.jsx(de,{value:"unavailable",children:"Unavailable"}),e.jsx(de,{value:"custom_hours",children:"Custom Hours"})]})]})]}),d.override_type==="custom_hours"&&e.jsxs("div",{className:"flex gap-2",children:[e.jsxs("div",{className:"space-y-1 flex-1",children:[e.jsx("label",{className:"text-xs font-medium",children:"Start"}),e.jsx(Y,{type:"time",value:d.custom_start,onChange:s=>u(i=>({...i,custom_start:s.target.value})),className:"h-8 text-xs"})]}),e.jsxs("div",{className:"space-y-1 flex-1",children:[e.jsx("label",{className:"text-xs font-medium",children:"End"}),e.jsx(Y,{type:"time",value:d.custom_end,onChange:s=>u(i=>({...i,custom_end:s.target.value})),className:"h-8 text-xs"})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("label",{className:"text-xs font-medium",children:"Reason (optional)"}),e.jsx(Y,{value:d.reason,onChange:s=>u(i=>({...i,reason:s.target.value})),placeholder:"e.g. Annual leave",className:"h-8 text-xs"})]})]}),e.jsxs(Rs,{children:[e.jsx(L,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>c(!1),children:"Cancel"}),e.jsx(L,{size:"sm",className:"h-7 text-xs",onClick:m,disabled:!d.override_date,children:"Add Override"})]})]})})]})}function kt({membership:t}){const{workspaceId:v}=te(),[g,x]=a.useState(!0),[b,S]=a.useState([]),f=a.useCallback(async()=>{if(!(t!=null&&t.id)||!v)return;x(!0);const{data:k}=await p.from("member_calendar_connections").select("*").eq("membership_id",t.id).order("created_at",{ascending:!0});S(k??[]),x(!1)},[t==null?void 0:t.id,v]);return a.useEffect(()=>{f()},[f]),g?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ce,{className:"h-4 w-4 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"rounded-lg border border-dashed bg-muted/30 p-6 text-center space-y-3",children:[e.jsx("div",{className:"mx-auto flex h-12 w-12 items-center justify-center rounded-xl bg-muted",children:e.jsx(Ie,{className:"h-6 w-6 text-muted-foreground"})}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-center justify-center gap-2",children:[e.jsx("h3",{className:"text-sm font-medium",children:"Calendar integrations"}),e.jsx(U,{variant:"secondary",className:"text-[10px]",children:"Coming soon"})]}),e.jsx("p",{className:"text-xs text-muted-foreground max-w-md mx-auto",children:"Google Calendar and Microsoft Outlook OAuth connect, primary calendar selection, conflict-checking, and free/busy sync are part of an upcoming release."})]}),e.jsx("div",{className:"text-left max-w-md mx-auto rounded-md bg-background border p-3 space-y-1.5",children:e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(Ze,{className:"h-3.5 w-3.5 text-muted-foreground mt-0.5 shrink-0"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-[11px] font-medium",children:"What this section will provide"}),e.jsxs("ul",{className:"text-[11px] text-muted-foreground space-y-0.5 list-disc list-inside",children:[e.jsx("li",{children:"Connect Google Calendar or Outlook via OAuth"}),e.jsx("li",{children:"Set a primary calendar for scheduling"}),e.jsx("li",{children:"Mark conflict calendars to prevent double-booking"}),e.jsx("li",{children:"Choose sync mode: free/busy or full details"})]})]})]})})]}),b.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wider",children:"Existing records"}),e.jsx(U,{variant:"outline",className:"text-[9px]",children:"Read-only preview"})]}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"These rows exist in the database from earlier setup. They are displayed for visibility only — connect/disconnect controls will activate when the OAuth flow ships."}),e.jsx("div",{className:"space-y-2",children:b.map(k=>{var N;return e.jsxs("div",{className:"border rounded-lg p-3 space-y-1.5 bg-muted/20",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ie,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"text-xs font-medium",children:k.account_label||k.provider}),e.jsx(U,{variant:"outline",className:"text-[9px] capitalize",children:k.provider})]}),e.jsx(U,{variant:k.status==="connected"?"default":"secondary",className:"text-[9px]",children:k.status==="connected"?"Connected":"Not Connected"})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-2 text-[11px] text-muted-foreground pt-1",children:[e.jsxs("div",{children:["Primary: ",e.jsx("span",{className:"text-foreground",children:k.is_primary?"Yes":"No"})]}),e.jsxs("div",{children:["Conflict: ",e.jsx("span",{className:"text-foreground",children:k.is_conflict_calendar?"Yes":"No"})]}),e.jsxs("div",{children:["Sync: ",e.jsx("span",{className:"text-foreground capitalize",children:((N=k.sync_mode)==null?void 0:N.replace(/_/g," "))??"—"})]})]})]},k.id)})})]})]})}const us=[{key:"task_assigned",label:"Task Assigned"},{key:"task_overdue",label:"Task Overdue"},{key:"compliance_review",label:"Compliance Review"},{key:"billing_exception",label:"Billing Exception"},{key:"conversation_mention",label:"Conversation Mention"},{key:"conversation_assigned",label:"Conversation Assigned"},{key:"system_alert",label:"System Alert"}],Fe=[{key:"in_app",label:"In-App",available:!0},{key:"email",label:"Email",available:!0},{key:"sms",label:"SMS",available:!1},{key:"whatsapp",label:"WhatsApp",available:!1}];function Ct({membership:t,canManage:v,isSelf:g}){const{workspaceId:x}=te(),{toast:b}=ae(),[S,f]=a.useState(!0),[k,N]=a.useState(!1),[$,y]=a.useState(!1),[o,E]=a.useState({}
1),[R,D]=a.useState({}),r=v||g,C=a.useCallback(async()=>{if(!(t!=null&&t.user_id)||!x)return;f(!0);const{data:j}=await p.from("notification_preferences").select("notification_type, channel_type, is_enabled").eq("user_id",t.user_id).eq("workspace_id",x),c={};for(const d of us)for(const u of Fe)u.available&&(c[`${d.key}:${u.key}`]=!0);for(const d of j??[]){const u=`${d.notification_type}:${d.channel_type}`;c[u]=d.is_enabled??!0}E(c),D({...c}),f(!1),y(!1)},[t==null?void 0:t.user_id,x]);a.useEffect(()=>{C()},[C]);const h=(j,c)=>{const d=`${j}:${c}`;E(u=>({...u,[d]:!u[d]})),y(!0)},A=async()=>{N(!0);try{const j=Object.entries(o).filter(([u])=>{var _;const[,n]=u.split(":");return(_=Fe.find(P=>P.key===n))==null?void 0:_.available}).map(([u,n])=>{const[_,P]=u.split(":");return{notification_type:_,channel_type:P,is_enabled:n}}),{data:c,error:d}=await p.functions.invoke("team-settings",{body:{action:"save_notification_preferences",workspace_id:x,membership_id:t.id,preferences:j}});if(d)throw d;if(c!=null&&c.error)throw new Error(c.error);b({title:"Notification preferences saved"}),D({...o}),y(!1)}catch(j){b({title:"Error",description:j.message,variant:"destructive"})}N(!1)};return S?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ce,{className:"h-4 w-4 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:"Notification Preferences"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Control which notifications are received per channel."})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-[11px]",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b",children:[e.jsx("th",{className:"text-left py-1.5 pr-3 font-medium text-muted-foreground",children:"Event"}),Fe.map(j=>e.jsx("th",{className:"text-center px-2 py-1.5 font-medium text-muted-foreground",children:e.jsxs("div",{className:"flex flex-col items-center gap-0.5",children:[e.jsx("span",{children:j.label}),!j.available&&e.jsx(U,{variant:"secondary",className:"text-[8px] px-1 py-0",children:"Soon"})]})},j.key))]})}),e.jsx("tbody",{children:us.map(j=>e.jsxs("tr",{className:"border-b last:border-0",children:[e.jsx("td",{className:"py-2 pr-3",children:j.label}),Fe.map(c=>{const d=`${j.key}:${c.key}`;return e.jsx("td",{className:"text-center px-2 py-2",children:e.jsx(me,{checked:!!o[d],onCheckedChange:()=>h(j.key,c.key),disabled:!c.available||!r,className:"scale-75 mx-auto"})},c.key)})]},j.key))})]})}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Preferences are saved server-side. SMS and WhatsApp channels are pending channel rollout."}),r&&$&&e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(L,{size:"sm",className:"h-7 text-xs",onClick:A,disabled:k,children:[k?e.jsx(ce,{className:"h-3 w-3 animate-spin mr-1"}):null,"Save"]}),e.jsx(L,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:C,children:"Cancel"})]})]})}function St({membership:t,canManage:v,isSelf:g}){const{workspaceId:x}=te(),{toast:b}=ae(),[S,f]=a.useState(!0),[k,N]=a.useState(!1),[$,y]=a.useState(!1),[o,E]=a.useState(""),[R,D]=a.useState(""),[r,C]=a.useState(""),h=v||g,A=a.useCallback(async()=>{if(!(t!=null&&t.user_id))return;f(!0);const{data:c}=await p.from("profiles").select("mobile, office_phone_extension, communications_signature").eq("id",t.user_id).maybeSingle();c&&(E(c.mobile??""),D(c.office_phone_extension??""),C(c.communications_signature??"")),f(!1),y(!1)},[t==null?void 0:t.user_id]);a.useEffect(()=>{A()},[A]);const j=async()=>{N(!0);try{const{data:c,error:d}=await p.functions.invoke("team-settings",{body:{action:"save_communications",workspace_id:x,membership_id:t.id,mobile:o||null,office_phone_extension:R||null,communications_signature:r||null}});if(d)throw d;if(c!=null&&c.error)throw new Error(c.error);b({title:"Communications settings saved"}),y(!1)}catch(c){b({title:"Error",description:c.message,variant:"destructive"})}N(!1)};return S?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ce,{className:"h-4 w-4 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:"Communications Settings"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Contact information and communication preferences."})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsxs("label",{className:"text-xs font-medium flex items-center gap-1.5",children:[e.jsx(we,{className:"h-3 w-3 text-muted-foreground"}),"Mobile Number"]}),h?e.jsx(Ls,{value:o,onChange:c=>{E(c),y(!0)},placeholder:"Phone number"}):e.jsx("p",{className:"text-xs text-muted-foreground",children:o||"Not set"})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("label",{className:"text-xs font-medium flex items-center gap-1.5",children:[e.jsx(we,{className:"h-3 w-3 text-muted-foreground"}),"Office Extension"]}),h?e.jsx(Y,{value:R,onChange:c=>{D(c.target.value),y(!0)},placeholder:"e.g. 2401",className:"h-8 text-xs"}):e.jsx("p",{className:"text-xs text-muted-foreground",children:R||"Not set"})]}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("label",{className:"text-xs font-medium flex items-center gap-1.5",children:[e.jsx(fs,{className:"h-3 w-3 text-muted-foreground"}),"Communications Signature"]}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Used in outbound emails and messages."}),h?e.jsx("textarea",{value:r,onChange:c=>{C(c.target.value),y(!0)},placeholder:"Your signature text…",className:"w-full min-h-[80px] rounded-md border border-input bg-background px-3 py-2 text-xs ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",rows:3}):e.jsx("p",{className:"text-xs text-muted-foreground whitespace-pre-wrap",children:r||"Not set"})]})]}),h&&$&&e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(L,{size:"sm",className:"h-7 text-xs",onClick:j,disabled:k,children:[k?e.jsx(ce,{className:"h-3 w-3 animate-spin mr-1"}):null,"Save"]}),e.jsx(L,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:A,children:"Cancel"})]})]})}const ps={provider_user_uuid:"",provider_user_id:"",provider_nickname:"",display_name:"",extension:"",sip_username:"",caller_id:"",app_mobile_number:"",desktop_email:"",is_voice_identity_active:!0,last_voice_identity_synced_at:null};function Et({membership:t,canManage:v,isSelf:g}){const{workspaceId:x}=te(),{toast:b}=ae(),[S,f]=a.useState(!0),[k,N]=a.useState(!1),[$,y]=a.useState(!1),[o,E]=a.useState({...ps}
1),[R,D]=a.useState(!1),[r,C]=a.useState([]),[h,A]=a.useState(""),j=v||g,c=async()=>{if(!(t!=null&&t.id)||!x)return;f(!0);const{data:m}=await p.from("member_voice_identities").select("*").eq("membership_id",t.id).eq("provider","yay").maybeSingle();if(m)E({provider_user_uuid:m.provider_user_uuid??"",provider_user_id:m.provider_user_id??"",provider_nickname:m.provider_nickname??"",display_name:m.display_name??"",extension:m.extension??"",sip_username:m.sip_username??"",caller_id:m.caller_id??"",app_mobile_number:m.app_mobile_number??"",desktop_email:m.desktop_email??"",is_voice_identity_active:m.is_voice_identity_active??!0,last_voice_identity_synced_at:m.last_voice_identity_synced_at??null}),D(!0),C([]);else if(E({...ps}),D(!1),v){const{data:q}=await p.from("member_voice_identities").select("id, provider_nickname, extension, desktop_email, caller_id").eq("workspace_id",x).is("membership_id",null);C(q??[])}f(!1)};a.useEffect(()=>{c()},[t==null?void 0:t.id,x]);const d=async()=>{if(!(!x||!(t!=null&&t.id))){N(!0);try{const{data:m,error:q}=await p.functions.invoke("team-settings",{body:{action:"save_voice_identity",workspace_id:x,membership_id:t.id,provider:"yay",...o}});if(q)throw q;if(m!=null&&m.error)throw new Error(m.error);b({title:"Voice identity saved"}),D(!0)}catch(m){b({title:"Error",description:m.message,variant:"destructive"})}N(!1)}},u=async()=>{if(!(!x||!(t!=null&&t.id)||!h)){y(!0);try{const{data:m,error:q}=await p.functions.invoke("team-settings",{body:{action:"pair_voice_identity",workspace_id:x,membership_id:t.id,voice_identity_id:h}});if(q)throw q;if(m!=null&&m.error)throw new Error(m.error);b({title:"Voice identity paired successfully"}),A(""),await c()}catch(m){b({title:"Error",description:m.message,variant:"destructive"})}y(!1)}},n=async()=>{if(!(!x||!(t!=null&&t.id))){y(!0);try{const{data:m,error:q}=await p.functions.invoke("team-settings",{body:{action:"unpair_voice_identity",workspace_id:x,membership_id:t.id}});if(q)throw q;if(m!=null&&m.error)throw new Error(m.error);b({title:"Voice identity unpaired"}),await c()}catch(m){b({title:"Error",description:m.message,variant:"destructive"})}y(!1)}},_=(m,q)=>{E(O=>({...O,[m]:q}))};if(S)return e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ce,{className:"h-4 w-4 animate-spin text-muted-foreground"})});if(!R&&v&&r.length>0)return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(we,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("h3",{className:"text-sm font-medium",children:"Voice Identity"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"This member doesn't have a voice identity yet. You can pair an existing unlinked provider identity or create a new one manually below."}),e.jsx(W,{}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(ee,{className:"text-xs font-medium",children:"Pair from unlinked identities"}),e.jsxs(De,{value:h,onValueChange:A,children:[e.jsx(Oe,{className:"h-8 text-xs",children:e.jsx(Re,{placeholder:"Select an identity to pair…"})}),e.jsx(Le,{children:r.map(m=>e.jsxs(de,{value:m.id,className:"text-xs",children:[m.provider_nickname??"Unknown"," — ext ",m.extension??"—"," · ",m.desktop_email??m.caller_id??""]},m.id))})]}),e.jsxs(L,{size:"sm",className:"w-full h-8 text-xs gap-1.5",onClick:u,disabled:!h||$,children:[$?e.jsx(ce,{className:"h-3 w-3 animate-spin"}):e.jsx(nt,{className:"h-3 w-3"}),"Pair Voice Identity"]})]}),e.jsxs("div",{className:"flex items-center gap-2 py-1",children:[e.jsx(W,{className:"flex-1"}),e.jsx("span",{className:"text-[10px] text-muted-foreground uppercase",children:"or create manually"}),e.jsx(W,{className:"flex-1"})]}),P()]});return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(we,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("h3",{className:"text-sm font-medium",children:"Voice Identity"})]}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[R&&e.jsx(U,{variant:"outline",className:"text-[10px]",children:o.is_voice_identity_active?"Active":"Inactive"}),R&&v&&e.jsxs(L,{variant:"ghost",size:"sm",className:"h-6 px-2 text-[10px] text-destructive hover:text-destructive gap-1",onClick:n,disabled:$,children:[$?e.jsx(ce,{className:"h-3 w-3 animate-spin"}):e.jsx(rt,{className:"h-3 w-3"}),"Unpair"]})]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Provider identity fields used for call attribution and future outbound calling. These are provider-side metadata, not canonical Lexuvo identity."}),e.jsx(W,{}),P()]});function P(){const m=[{key:"provider_user_uuid",label:"Provider User UUID"},{key:"provider_user_id",label:"Provider User ID"},{key:"provider_nickname",label:"Provider Nickname"},{key:"extension",label:"Extension"},{key:"sip_username",label:"SIP Username"}],q=[{key:"display_name",label:"Display Name",placeholder:"Friendly name"},{key:"caller_id",label:"Caller ID",placeholder:"+441217665477"},{key:"app_mobile_number",label:"App Mobile Number",placeholder:"+44..."},{key:"desktop_email",label:"Desktop Email",placeholder:"[email protected]"}];return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-3",children:[m.map(({key:O,label:s})=>e.jsxs("div",{className:"space-y-1",children:[e.jsx(ee,{className:"text-xs text-muted-foreground",children:s}),e.jsx(Y,{className:"h-8 text-xs bg-muted/50 font-mono",value:o[O]||"—",disabled:!0,readOnly:!0})]},O)),e.jsx(W,{className:"my-2"}),q.map(({key:O,label:s,placeholder:i})=>e.jsxs("div",{className:"space-y-1",children:[e.jsx(ee,{className:"text-xs",children:s}),e.jsx(Y,{className:"h-8 text-xs",value:o[O],placeholder:i,disabled:!j,onChange:T=>_(O,T.target.value)})]},O)),e.jsxs("div",{className:"flex items-center justify-between pt-1",children:[e.jsx(ee,{className:"text-xs",children:"Voice Identity Active"}),e.jsx(me,{checked:o.is_voice_identity_active,onCheckedChange:O=>_("is_voice_identity_active",O),disabled:!j})]}),o.last_voice_identity_synced_at&&e.jsxs("p",{className:"text-[10px] text-muted-foreground",children:["Last synced: ",new Date(o.last_voice_identity_synced_at).toLocaleStr
1ing()]})]}),j&&e.jsxs(e.Fragment,{children:[e.jsx(W,{}),e.jsxs(L,{size:"sm",className:"w-full h-8 text-xs gap-1.5",onClick:d,disabled:k,children:[k?e.jsx(ce,{className:"h-3 w-3 animate-spin"}):e.jsx(Es,{className:"h-3 w-3"}),"Save Voice Identity"]})]})]})}}function At({open:t,onOpenChange:v,membership:g,existingClientIds:x,onAssigned:b}){const{workspaceId:S}=te(),{toast:f}=ae(),[k,N]=a.useState([]),[$,y]=a.useState(!1),[o,E]=a.useState(""),[R,D]=a.useState(null);a.useEffect(()=>{!t||!S||(y(!0),p.from("clients").select("id, name, client_code").eq("workspace_id",S).order("name").then(({data:h})=>{N(h??[]),y(!1)}))},[t,S]);const r=a.useMemo(()=>{const h=k.filter(j=>!x.includes(j.id));if(!o.trim())return h.slice(0,50);const A=o.toLowerCase();return h.filter(j=>{var c,d;return((c=j.name)==null?void 0:c.toLowerCase().includes(A))||((d=j.client_code)==null?void 0:d.toLowerCase().includes(A))}).slice(0,50)},[k,x,o]),C=async h=>{D(h);try{const{data:A,error:j}=await p.functions.invoke("team-settings",{body:{action:"assign_client_owner",workspace_id:S,membership_id:g.id,client_id:h}});if(j)throw j;if(A!=null&&A.error)throw new Error(A.error);f({title:"Client assigned"}),b(),v(!1)}catch(A){f({title:"Error",description:A.message,variant:"destructive"})}D(null)};return e.jsx(be,{open:t,onOpenChange:v,children:e.jsxs(ke,{className:"max-w-sm",children:[e.jsx(Ce,{children:e.jsx(Se,{className:"text-sm",children:"Assign Client"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(qe,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(Y,{className:"h-8 text-xs pl-8",placeholder:"Search clients…",value:o,onChange:h=>E(h.target.value)})]}),e.jsx("div",{className:"max-h-60 overflow-y-auto space-y-1",children:$?e.jsx("p",{className:"text-xs text-muted-foreground p-2",children:"Loading…"}):r.length===0?e.jsx("p",{className:"text-xs text-muted-foreground p-2",children:"No unassigned clients found."}):r.map(h=>e.jsxs("div",{className:"flex items-center justify-between p-2 rounded hover:bg-muted/50",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:h.name}),h.client_code&&e.jsx("p",{className:"text-[10px] text-muted-foreground",children:h.client_code})]}),e.jsx(L,{variant:"ghost",size:"icon",className:"h-6 w-6",disabled:R===h.id,onClick:()=>C(h.id),children:e.jsx(is,{className:"h-3 w-3"})})]},h.id))})]})]})})}const Ge={is_reviewer_eligible:!1,reviewer_notes:"",can_receive_task_assignments:!0,can_receive_compliance_assignments:!0,can_receive_billing_exceptions:!1,is_escalation_owner:!1,escalation_priority:0};function Tt({membership:t,canManage:v,isSelf:g}){const{workspaceId:x}=te(),{toast:b}=ae(),[S,f]=a.useState([]),[k,N]=a.useState(!0),[$,y]=a.useState(!1),[o,E]=a.useState([]),[R,D]=a.useState(!0),[r,C]=a.useState(Ge),[h,A]=a.useState(Ge),[j,c]=a.useState(!0),[d,u]=a.useState(!1),n=v||g,_=JSON.stringify(r)!==JSON.stringify(h),P=a.useCallback(async()=>{if(!x||!(t!=null&&t.id))return;N(!0);const{data:s}=await p.from("clients").select("id, name, client_code, lifecycle_status").eq("workspace_id",x).eq("owner_id",t.user_id);f(s??[]),N(!1),D(!0);try{const{data:T}=await p.from("membership_scope_rules").select("id, entity_id, client_id").eq("membership_id",t.id).eq("workspace_id",x).eq("scope_type","assigned_entities");if(T&&T.length>0){const B=T.map(ie=>ie.entity_id).filter(Boolean);if(B.length>0){const{data:ie}=await p.from("entities").select("id, entity_name, client_id, clients(name)").in("id",B);E((ie??[]).map(xe=>{var ue;return{...xe,scopeRuleId:(ue=T.find(pe=>pe.entity_id===xe.id))==null?void 0:ue.id}}))}else E([])}else E([])}catch{E([])}D(!1),c(!0);const{data:i}=await p.from("member_routing_profiles").select("*").eq("membership_id",t.id).maybeSingle();if(i){const T={is_reviewer_eligible:i.is_reviewer_eligible??!1,reviewer_notes:i.reviewer_notes??"",can_receive_task_assignments:i.can_receive_task_assignments??!0,can_receive_compliance_assignments:i.can_receive_compliance_assignments??!0,can_receive_billing_exceptions:i.can_receive_billing_exceptions??!1,is_escalation_owner:i.is_escalation_owner??!1,escalation_priority:i.escalation_priority??0};C(T),A(T)}else C(Ge),A(Ge);c(!1)},[x,t==null?void 0:t.id,t==null?void 0:t.user_id]);a.useEffect(()=>{P()},[P]);const m=async s=>{try{const{data:i,error:T}
1=await p.functions.invoke("team-settings",{body:{action:"remove_client_owner",workspace_id:x,membership_id:t.id,client_id:s}});if(T)throw T;if(i!=null&&i.error)throw new Error(i.error);b({title:"Client unassigned"}),P()}catch(i){b({title:"Error",description:i.message,variant:"destructive"})}},q=async()=>{u(!0);try{const{data:s,error:i}=await p.functions.invoke("team-settings",{body:{action:"save_routing_profile",workspace_id:x,membership_id:t.id,...r}});if(i)throw i;if(s!=null&&s.error)throw new Error(s.error);b({title:"Routing profile saved"}),A({...r})}catch(s){b({title:"Error",description:s.message,variant:"destructive"})}u(!1)},O=a.useMemo(()=>{const s=[],i=[];r.can_receive_task_assignments&&i.push("task"),r.can_receive_compliance_assignments&&i.push("compliance"),r.can_receive_billing_exceptions&&i.push("billing exception");const T=[];return r.can_receive_task_assignments||T.push("task"),r.can_receive_compliance_assignments||T.push("compliance"),r.can_receive_billing_exceptions||T.push("billing exception"),i.length>0&&T.length>0?s.push(`This member receives ${i.join(" and ")} routing but not ${T.join(" or ")}.`):i.length>0?s.push(`This member receives ${i.join(", ")} routing.`):s.push("This member has no routing categories enabled."),r.is_reviewer_eligible&&S.length>0?s.push(`This member is reviewer-eligible and owns ${S.length} client${S.length!==1?"s":""}.`):r.is_reviewer_eligible&&s.push("This member is reviewer-eligible but has no assigned client ownership."),r.is_escalation_owner&&s.push(`This member is an escalation owner (priority ${r.escalation_priority}).`),S.length>0&&!r.can_receive_task_assignments&&!r.can_receive_compliance_assignments&&s.push("This member has client ownership but no routing categories enabled."),s},[r,S]);return e.jsxs("div",{className:"space-y-6",children:[O.length>0&&e.jsx("section",{className:"space-y-1.5 p-3 rounded-lg border bg-muted/30",children:O.map((s,i)=>e.jsx("p",{className:"text-xs text-muted-foreground",children:s},i))}),e.jsxs("section",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h4",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wider flex items-center gap-1.5",children:[e.jsx(ss,{className:"h-3 w-3"})," Assigned Clients"]}),v&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-6 text-[10px] gap-1",onClick:()=>y(!0),children:[e.jsx(is,{className:"h-3 w-3"})," Assign"]})]}),k?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading…"}):S.length===0?e.jsx("p",{className:"text-xs text-muted-foreground",children:"No clients assigned as owner."}):e.jsx("div",{className:"space-y-1",children:S.map(s=>e.jsxs("div",{className:"flex items-center justify-between p-2 rounded border",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:s.name}),s.client_code&&e.jsx("p",{className:"text-[10px] text-muted-foreground",children:s.client_code})]}),v&&e.jsx(L,{variant:"ghost",size:"icon",className:"h-6 w-6 text-muted-foreground hover:text-destructive",onClick:()=>m(s.id),children:e.jsx(Is,{className:"h-3 w-3"})})]},s.id))})]}),e.jsx(W,{}),e.jsxs("section",{className:"space-y-2",children:[e.jsxs("h4",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wider flex items-center gap-1.5",children:[e.jsx(qs,{className:"h-3 w-3"})," Assigned Entities"]}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Entity assignments are managed via scope rules in the Access tab."}),R?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading…"}):o.length===0?e.jsx("p",{className:"text-xs text-muted-foreground",children:"No entities assigned via scope."}):e.jsx("div",{className:"space-y-1",children:o.map(s=>{var i;return e.jsx("div",{className:"flex items-center justify-between p-2 rounded border",children:e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:s.entity_name}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:((i=s.clients)==null?void 0:i.name)??"—"})]})},s.id)})})]}),e.jsx(W,{}),e.jsxs("section",{className:"space-y-3",children:[e.jsxs("h4",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wider flex items-center gap-1.5",children:[e.jsx(Ms,{className:"h-3 w-3"})," Reviewer Responsibilities"]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(ee,{className:"text-xs",children:"Eligible as reviewer"}),e.jsx(me,{checked:r.is_reviewer_eligible,onCheckedChange:s=>C(i=>({...i,is_reviewer_eligible:s})),disabled:!n})]}),r.is_reviewer_eligible&&e.jsxs("div",{className:"space-y-1",children:[e.jsx(ee,{className:"text-[10px] text-muted-foreground",children:"Reviewer notes"}),e.jsx("textarea",{className:"flex w-full rounded-md border border-input bg-background px-2 py-1.5 text-xs ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring min-h-[50px] resize-none",value:r.reviewer_notes,onChange:s=>C(i=>({...i,reviewer_notes:s.target.value})),disabled:!n,placeholder:"E.g. Tax compliance, audit sign-off…",maxLength:500})]})]}),e.jsx(W,{}),e.jsxs("section",{className:"space-y-3",children:[e.jsxs("h4",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wider flex items-center gap-1.5",children:[e.jsx(lt,{className:"h-3 w-3"})," Default Routing"]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(ee,{className:"text-xs",children:"Receive task assignments"}),e.jsx(me,{checked:r.can_receive_task_assignments,onCheckedChange:s=>C(i=>({...i,can_receive_task_assignments:s}
1)),disabled:!n})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(ee,{className:"text-xs",children:"Receive compliance assignments"}),e.jsx(me,{checked:r.can_receive_compliance_assignments,onCheckedChange:s=>C(i=>({...i,can_receive_compliance_assignments:s})),disabled:!n})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(ee,{className:"text-xs",children:"Receive billing exceptions"}),e.jsx(me,{checked:r.can_receive_billing_exceptions,onCheckedChange:s=>C(i=>({...i,can_receive_billing_exceptions:s})),disabled:!n})]})]})]}),e.jsx(W,{}),e.jsxs("section",{className:"space-y-3",children:[e.jsxs("h4",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wider flex items-center gap-1.5",children:[e.jsx(gs,{className:"h-3 w-3"})," Escalation Ownership"]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(ee,{className:"text-xs",children:"Can be escalation owner"}),e.jsx(me,{checked:r.is_escalation_owner,onCheckedChange:s=>C(i=>({...i,is_escalation_owner:s})),disabled:!n})]}),r.is_escalation_owner&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ee,{className:"text-xs whitespace-nowrap",children:"Priority"}),e.jsx(Y,{type:"number",className:"h-7 w-20 text-xs",min:0,max:99,value:r.escalation_priority,onChange:s=>C(i=>({...i,escalation_priority:parseInt(s.target.value)||0})),disabled:!n}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Lower = higher priority"})]})]}),n&&_&&e.jsxs(e.Fragment,{children:[e.jsx(W,{}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(L,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>C(h),children:"Cancel"}),e.jsx(L,{size:"sm",className:"h-7 text-xs",onClick:q,disabled:d,children:d?"Saving…":"Save Routing"})]})]}),e.jsx(At,{open:$,onOpenChange:y,membership:t,existingClientIds:S.map(s=>s.id),onAssigned:P})]})}function Ne(t){return t==null?"":(t/100).toFixed(2)}function ye(t){const v=t.trim();if(!v)return null;const g=Number(v);return Number.isNaN(g)||g<0?null:Math.round(g*100)}function Dt({membershipId:t,workspaceId:v,canManage:g}){const{toast:x}=ae(),[b,S]=a.useState(!0),[f,k]=a.useState(!1),[N,$]=a.useState({cost_rate_minor:null,default_bill_rate_minor:null,rate_currency_code:null}),[y,o]=a.useState({default_bill_rate_minor:null,default_currency_code:"GBP"}),[E,R]=a.useState(""),[D,r]=a.useState(""),[C,h]=a.useState("");a.useEffect(()=>{if(!t||!v)return;let d=!1;return(async()=>{S(!0);const[u,n]=await Promise.all([p.from("workspace_memberships").select("cost_rate_minor, default_bill_rate_minor, rate_currency_code").eq("id",t).maybeSingle(),p.from("workspaces").select("default_bill_rate_minor, default_currency_code").eq("id",v).maybeSingle()]);if(!d){if(u.data){const _=u.data;$(_),R(Ne(_.cost_rate_minor)),r(Ne(_.default_bill_rate_minor)),h(_.rate_currency_code||"")}n.data&&o(n.data),S(!1)}})(),()=>{d=!0}},[t,v]);const A=async()=>{k(!0);const d={cost_rate_minor:ye(E),default_bill_rate_minor:ye(D),rate_currency_code:C.trim().toUpperCase()||null},{error:u}=await p.from("workspace_memberships").update(d).eq("id",t);if(k(!1),u){x({title:"Could not save rates",description:u.message,variant:"destructive"});return}$(d),x({title:"Rates updated",description:"New time entries will use these rates."})},j=C.trim().toUpperCase()||y.default_currency_code||"GBP",c=ye(D)??y.default_bill_rate_minor;return b?e.jsx(X,{children:e.jsx(ls,{className:"p-6",children:e.jsx(zs,{className:"h-32 w-full"})})}):e.jsxs(X,{children:[e.jsx($s,{className:"pb-3",children:e.jsxs(Ps,{className:"text-sm flex items-center gap-2",children:[e.jsx(ct,{className:"h-4 w-4 text-amber-600"})," Cost & billing rates"]})}),e.jsxs(ls,{className:"space-y-4",children:[e.jsxs("p",{className:"text-xs text-muted-foreground flex items-start gap-2",children:[e.jsx(Ze,{className:"h-3.5 w-3.5 mt-0.5 shrink-0"}),"These rates feed cost, revenue, and margin calculations on every time entry. Empty fields fall back to the workspace defaults."]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(ee,{className:"text-xs",children:"Cost rate (per hour)"}),e.jsxs("div",{className:"relative",children:[e.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-xs text-muted-foreground",children:j}),e.jsx(Y,{type:"number",min:"0",step:"0.01",value:E,onChange:d=>R(d.target.value),disabled:!g||f,className:"pl-12",placeholder:"0.00"})]}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Internal labour cost per hour"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(ee,{className:"text-xs",children:"Default bill rate (per hour)"}),e.jsxs("div",{className:"relative",children:[e.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-xs text-muted-foreground",children:j}),e.jsx(Y,{type:"number",min:"0",step:"0.01",value:D,onChange:d=>r(d.target.value),disabled:!g||f,className:"pl-12",placeholder:y.default_bill_rate_minor?Ne(y.default_bill_rate_minor):"0.00"})]}),e.jsxs("p",{className:"text-[10px] text-muted-foreground",children:["Workspace default: ",y.default_bill_rate_minor?`${j} ${Ne(y.default_bill_rate_minor)}/hr`:"—"]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(ee,{className:"text-xs",children:"Currency code"}),e.jsx(Y,{value:C,onChange:d=>h(d.target.value.toUpperCase()),disabled:!g||f,maxLength:3,placeholder:y.default_currency_code||"GBP"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"3-letter ISO code (e.g. GBP, EUR)"})]})]}),c!==null&&e.jsxs("div",{className:"rounded-md bg-muted/50 p-3 text-xs",children:[e.jsxs("div",{className:"flex items-center gap-2 font-medium",children:[e.jsx(ot,{className:"h-3.5 w-3.5 text-emerald-600"})," Effective bill rate: ",j," ",Ne(c),"/hr"]}),ye(E)!==null&&c>0&&e.jsxs("div",{className:"text-muted-foreground mt-1",children:["Margin per hour: ",j," ",Ne(c-(ye(E)??
10))," (",((c-(ye(E)??0))/c*100).toFixed(0),"%)"]})]}),g&&e.jsx("div",{className:"flex justify-end",children:e.jsxs(L,{size:"sm",onClick:A,disabled:f,children:[e.jsx(Es,{className:"h-3.5 w-3.5 mr-1.5"})," ",f?"Saving…":"Save rates"]})})]})]})}function Ot({open:t,onOpenChange:v,targetMembership:g,allMembers:x,onCopied:b}){var n,_,P,m,q;const{workspaceId:S}=te(),{toast:f}=ae(),k=Me(),[N,$]=a.useState(""),[y,o]=a.useState(!1),[E,R]=a.useState(""),[D,r]=a.useState(0),[C,h]=a.useState(0),A=a.useMemo(()=>x.filter(O=>O.id!==g.id&&O.status==="active"),[x,g.id]),j=a.useMemo(()=>{const O=E.toLowerCase();return O?A.filter(s=>{var i,T;return(T=(i=s.profiles)==null?void 0:i.full_name)==null?void 0:T.toLowerCase().includes(O)}):A},[A,E]),c=a.useMemo(()=>A.find(O=>O.id===N),[A,N]);a.useEffect(()=>{if(!N||!S){r(0),h(0);return}Promise.all([p.from("membership_scope_rules").select("id",{count:"exact",head:!0}).eq("membership_id",N).is("valid_to",null),p.from("membership_capability_overrides").select("id",{count:"exact",head:!0}).eq("membership_id",N)]).then(([O,s])=>{r(O.count??0),h(s.count??0)})},[N,S]);const d=async()=>{if(!(!N||!S||!await k("Copy permissions"))){o(!0);try{const{data:s,error:i}=await p.functions.invoke("rbac-admin",{body:{action:"copy_permissions",workspace_id:S,source_membership_id:N,target_membership_id:g.id}});if(i)throw i;if(s!=null&&s.error)throw new Error(s.error);f({title:"Permissions copied",description:`Role, scope, and overrides copied. ${s.scopes_copied} scope rule(s), ${s.overrides_copied} override(s).`}),v(!1),b()}catch(s){f({title:"Error",description:s.message,variant:"destructive"})}o(!1)}},u=((n=g.profiles)==null?void 0:n.full_name)??"Target member";return e.jsx(be,{open:t,onOpenChange:v,children:e.jsxs(ke,{className:"max-w-md",children:[e.jsxs(Ce,{children:[e.jsxs(Se,{className:"text-sm flex items-center gap-2",children:[e.jsx(hs,{className:"h-4 w-4 text-primary"}),"Copy Permissions"]}),e.jsxs(Je,{className:"text-xs",children:["Copy role, scope, and capability overrides from another member to ",e.jsx("span",{className:"font-medium text-foreground",children:u}),"."]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("label",{className:"text-xs font-medium",children:"Copy from"}),e.jsxs("div",{className:"relative",children:[e.jsx(qe,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(Y,{className:"h-8 text-xs pl-8",placeholder:"Search members…",value:E,onChange:O=>R(O.target.value)})]}),e.jsx("div",{className:"border rounded-lg max-h-36 overflow-y-auto",children:j.length===0?e.jsx("p",{className:"text-xs text-muted-foreground p-3",children:"No matching members."}):j.map(O=>{var s;return e.jsxs("button",{type:"button",className:`w-full flex items-center gap-2 px-3 py-2 text-left text-xs hover:bg-muted/50 transition-colors ${N===O.id?"bg-primary/10":""}`,onClick:()=>$(O.id),children:[e.jsx("span",{className:"truncate",children:((s=O.profiles)==null?void 0:s.full_name)??O.user_id.slice(0,8)}),e.jsx(U,{variant:"outline",className:"text-[10px] ml-auto shrink-0",children:je[O.role]??O.role})]},O.id)})})]}),c&&e.jsxs(e.Fragment,{children:[e.jsx(W,{}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wider",children:"Preview"}),e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 rounded-lg border p-2.5 space-y-1",children:[e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Source"}),e.jsx(ts,{fullName:(_=c.profiles)==null?void 0:_.full_name,avatarUrl:(P=c.profiles)==null?void 0:P.avatar_url,variant:"table-row"})]}),e.jsx(Us,{className:"h-4 w-4 text-muted-foreground shrink-0"}),e.jsxs("div",{className:"flex-1 rounded-lg border p-2.5 space-y-1",children:[e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Target"}),e.jsx(ts,{fullName:(m=g.profiles)==null?void 0:m.full_name,avatarUrl:(q=g.profiles)==null?void 0:q.avatar_url,variant:"table-row"})]})]}),e.jsxs("div",{className:"rounded-lg border bg-muted/20 p-3 space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ye,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsxs("span",{className:"text-xs",children:["Role will change to ",e.jsx(U,{variant:"outline",className:"text-[10px]",children:je[c.role]??c.role})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(We,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsxs("span",{className:"text-xs",children:[D," active scope rule",D!==1?"s":""," will replace current rules"]})]}),C>0&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ye,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsxs("span",{className:"text-xs",children:[C," capability override",C!==1?"s":""," will be copied"]})]})]}),e.jsxs("div",{className:"flex items-start gap-2 rounded-lg border border-amber-200 bg-amber-50 p-2.5",children:[e.jsx(gs,{className:"h-3.5 w-3.5 text-amber-600 shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-[10px] text-amber-800 space-y-0.5",children:[e.jsx("p",{className:"font-medium",children:"Will NOT be copied:"}),e.jsx("p",{children:"Lifecycle status, notification preferences, calendar settings, availability schedules, communication identity"})]})]})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-1",children:[e.jsx(L,{variant:"outline",size:"sm",className:"h-8 text-xs",onClick:()=>v(!1),children:"Cancel"}),e.jsx(L,{size:"sm",className:"h-8 text-xs",onClick:d,disabled:!N||y,children:y?"Copying…":"Apply Permissions"})]})]})]})})}const Rt=[{value:"info",label:"Overview",icon:js},{value:"access",label:"Access",icon:Ye},{value:"rates",label:"Rates",icon:ss},{value:"availability",label:"Availability",icon:as},{value:"calendar",label:"Calendar",icon:Ie},{value:"notifications",label:"Notifications",icon:et},{value:"communications",label:"Communications",icon:fs},{value:"voice",label:"Voice",icon:we},{value:"assignments",label:"Assignments",icon:ss},{value:"audit",label:"Audit",icon:as}];function la(){var Ae,ze;const{membershipId:t}=Fs(),v=Gs(),{workspaceId:g,isAdmin:x}=te(),{user:b}=Bs(),{toast:S}=ae(),f=Me(),[k,N]=a.useState(!0),[$,y]=a.useState(!1),[o,E]=a.useState(null),[R,D]=a.use
1State([]),[r,C]=a.useState(null),[h,A]=a.useState(null),[j,c]=a.useState(0),[d,u]=a.useState(0),[n,_]=a.useState(0),[P,m]=a.useState([]),[q,O]=a.useState(null),[s,i]=a.useState("info"),[T,B]=a.useState(null),[ie,xe]=a.useState(!1),[ue,pe]=a.useState(!1),he=a.useCallback(async()=>{if(!t||!g)return;N(!0),y(!1);const{data:I,error:se}=await p.from("workspace_memberships").select("id, role, status, created_at, user_id, workspace_id").eq("id",t).eq("workspace_id",g).maybeSingle();if(se||!I){y(!0),N(!1);return}const{data:G}=await p.from("profiles").select("id, full_name, avatar_url, first_name, last_name, job_title, mobile, office_phone_extension, department, email").eq("id",I.user_id).maybeSingle(),[{data:Z},{data:ne},{data:Ke},{count:Xe},{count:Qe},{count:fe},{data:K},{data:_e},{data:Te}]=await Promise.all([p.from("membership_scope_rules").select("id, membership_id, scope_type, client_id, entity_id, valid_to").eq("workspace_id",g).eq("membership_id",I.id),p.from("member_routing_profiles").select("*").eq("workspace_id",g).eq("membership_id",I.id).maybeSingle(),p.from("member_availability_profiles").select("status, timezone").eq("workspace_id",g).eq("membership_id",I.id).maybeSingle(),p.from("clients").select("id",{count:"exact",head:!0}).eq("workspace_id",g).eq("owner_id",I.user_id),p.from("tasks").select("id",{count:"exact",head:!0}).eq("workspace_id",g).eq("assignee_id",I.user_id).not("status","in","(completed,cancelled)"),p.from("compliance_items").select("id",{count:"exact",head:!0}).eq("workspace_id",g).eq("assigned_user_id",I.user_id),p.from("activity_events").select("id, action, summary, actor_id, created_at, metadata, event_category").eq("workspace_id",g).in("event_category",["access_control","team_settings"]).eq("target_id",I.id).order("created_at",{ascending:!1}).limit(50),G!=null&&G.email?p.from("workspace_invites").select("created_at, accepted_at, email, invited_by").eq("workspace_id",g).eq("email",G.email).order("created_at",{ascending:!1}).limit(1).maybeSingle().then(V=>({data:V.data?[V.data]:[]})):Promise.resolve({data:[]}),p.from("workspace_memberships").select("id, user_id, role, status").eq("workspace_id",g).in("status",["active","suspended"])]),l=(Z??[]).filter(V=>!V.valid_to||new Date(V.valid_to)>=new Date),w=(_e??[])[0]??null,M=(Te??[]).map(V=>V.user_id).filter(V=>V!==I.user_id),z=Array.from(new Set((K??[]).map(V=>V.actor_id).filter(Boolean))),J=w!=null&&w.invited_by?[w.invited_by]:[],H=Array.from(new Set([...M,...z,...J])),{data:ge}=H.length>0?await p.from("profiles").select("id, full_name, avatar_url, email").in("id",H):{data:[]},es=new Map((ge??[]).map(V=>[V.id,V]));E({...I,profiles:G??{full_name:I.user_id.slice(0,8)},scopeRules:l}),D((Te??[]).map(V=>({...V,profiles:es.get(V.user_id)??{full_name:V.user_id.slice(0,8)}}))),C(ne),A(Ke),c(Xe??0),u(Qe??0),_(fe??0),m((K??[]).map(V=>({...V,actor:V.actor_id?es.get(V.actor_id)??null:null}))),O(w?{...w,inviter:w.invited_by?es.get(w.invited_by)??null:null}:null),N(!1)},[t,g]);a.useEffect(()=>{he()},[he]);const Ee=async I=>{if(!(["suspend_membership","revoke_membership"].includes(I)&&!await f("Suspend/revoke membership"))){xe(!0);try{const{data:G,error:Z}=await p.functions.invoke("rbac-admin",{body:{action:I,workspace_id:g,membership_id:o.id}});if(Z)throw Z;if(G!=null&&G.error)throw new Error(G.error);S({title:{suspend_membership:"Member suspended",reactivate_membership:"Member reactivated",revoke_membership:"Member removed"}[I]??"Action completed"}),B(null),I==="revoke_membership"?v("/firm/team"):await he()}catch(G){S({title:"Error",description:G.message,variant:"destructive"})}xe(!1)}};if(k)return e.jsx(Vs,{});if($||!o)return e.jsxs("div",{className:"space-y-4",children:[e.jsx(L,{variant:"ghost",size:"sm",className:"gap-1.5",asChild:!0,children:e.jsxs(cs,{to:"/firm/team",children:[e.jsx(os,{className:"h-3.5 w-3.5"})," Back to Team"]})}),e.jsx(ds,{icon:Hs,title:"Member not found",description:"This membership may have been revoked or you do not have access to view it.",actionLabel:"Return to Team",onAction:()=>v("/firm/team")})]});const F=o.profiles??{},re=F.full_name??"Unknown",Q=o.user_id===(b==null?void 0:b.id),ve=Ws(o.role),le=x;return e.jsxs("div",{className:"space-y-6 pb-12",children:[e.jsx("div",{children:e.jsx(L,{variant:"ghost",size:"sm",className:"gap-1.5 -ml-2 h-7 text-xs text-muted-foreground hover:text-foreground",asChild:!0,children:e.jsxs(cs,{to:"/firm/team",children:[e.jsx(os,{className:"h-3.5 w-3.5"})," Back to Team"]})})}),e.jsxs(X,{className:"p-6",children:[e.jsxs("div",{className:"flex flex-col md:flex-row md:items-start md:justify-between gap-4",children:[e.jsxs("div",{className:"space-y-3 min-w-0 flex-1",children:[e.jsx("div",{className:"flex items-start gap-4",children:e.jsx(ts,{fullName:re,avatarUrl:F.avatar_url,variant:"default",showYouBadge:Q})}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx(Ys,{status:o.status==="pending_setup"?"pending_activation":o.status}),e.jsx(U,{variant:"outline",className:"text-xs",children:je[o.role]??o.role}),ve&&e.jsxs(U,{variant:"default",className:"text-xs gap-1",title:"Owner accounts cannot be suspended or removed.",children:[e.jsx(xt,{className:"h-3 w-3"})," Owner"]}),F.job_title&&e.jsx("span",{className:"text-xs text-muted-foreground",children:F.job_title}),F.department&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-muted-foreground
1/50",children:"·"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:F.department})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-x-4 gap-y-1.5 text-xs text-muted-foreground",children:[F.email&&e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(Js,{className:"h-3 w-3"}),F.email]}),F.mobile&&e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(ut,{className:"h-3 w-3"}),F.mobile]}),F.office_phone_extension&&e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(we,{className:"h-3 w-3"}),"Ext. ",F.office_phone_extension]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(Ie,{className:"h-3 w-3"}),"Joined ",$e(new Date(o.created_at),"dd MMM yyyy")]}),((Ae=q==null?void 0:q.inviter)==null?void 0:Ae.full_name)&&e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(js,{className:"h-3 w-3"}),"Invited by ",q.inviter.full_name]})]})]}),le&&!Q&&!ve&&e.jsxs("div",{className:"flex flex-wrap items-center gap-2 shrink-0",children:[o.status==="active"&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-8 gap-1.5 text-xs",onClick:()=>B("suspend"),children:[e.jsx(pt,{className:"h-3.5 w-3.5"})," Suspend"]}),o.status==="suspended"&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-8 gap-1.5 text-xs",onClick:()=>B("reactivate"),children:[e.jsx(Zs,{className:"h-3.5 w-3.5"})," Reactivate"]}),(o.status==="active"||o.status==="suspended")&&e.jsxs(L,{variant:"outline",size:"sm",className:"h-8 gap-1.5 text-xs text-destructive border-destructive/30 hover:bg-destructive/5 hover:text-destructive",onClick:()=>B("revoke"),children:[e.jsx(Ss,{className:"h-3.5 w-3.5"})," Revoke"]})]})]}),(Q||ve)&&e.jsxs("div",{className:"mt-4 flex items-start gap-2 rounded-md border bg-muted/30 px-3 py-2 text-xs text-muted-foreground",children:[e.jsx(Ze,{className:"h-3.5 w-3.5 shrink-0 mt-0.5"}),e.jsxs("span",{children:[Q&&"You are viewing your own profile. Lifecycle actions (suspend, revoke) are hidden — ask another admin to make those changes.",!Q&&ve&&"This is the firm owner account. Owner accounts cannot be suspended, revoked, or have their role downgraded."]})]})]}),e.jsxs(Ks,{value:s,onValueChange:i,className:"w-full",children:[e.jsx("div",{className:"border-b overflow-x-auto",children:e.jsx(Xs,{className:"h-auto p-0 bg-transparent rounded-none w-max",children:Rt.map(({value:I,label:se,icon:G})=>e.jsxs(Qs,{value:I,className:"text-xs gap-1.5 rounded-none border-b-2 border-transparent data-[state=active]:border-primary data-[state=active]:bg-transparent data-[state=active]:shadow-none px-4 py-2.5",children:[e.jsx(G,{className:"h-3.5 w-3.5"})," ",se]},I))})}),e.jsxs(oe,{value:"info",className:"mt-6 space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-4",children:[e.jsxs(X,{className:"p-4 space-y-3",children:[e.jsx("p",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wider",children:"Workload"}),e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-semibold",children:j}),e.jsx("button",{className:"text-[11px] text-muted-foreground hover:text-foreground hover:underline",onClick:()=>v(`/firm/clients?owner=${o.user_id}`),children:"Clients"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-semibold",children:d}),e.jsx("button",{className:"text-[11px] text-muted-foreground hover:text-foreground hover:underline",onClick:()=>v(`/firm/tasks?assignee=${o.user_id}`),children:"Open tasks"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-semibold",children:n}),e.jsx("button",{className:"text-[11px] text-muted-foreground hover:text-foreground hover:underline",onClick:()=>v(`/firm/compliance?assignee=${o.user_id}`),children:"Compliance"})]})]})]}),e.jsxs(X,{className:"p-4 space-y-3",children:[e.jsx("p",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wider",children:"Routing"}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Reviewer"}),e.jsx(U,{variant:r!=null&&r.is_reviewer_eligible?"default":"outline",className:"text-[10px]",children:r!=null&&r.is_reviewer_eligible?"Eligible":"—"})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Escalation owner"}),e.jsx(U,{variant:r!=null&&r.is_escalation_owner?"default":"outline",className:"text-[10px]",children:r!=null&&r.is_escalation_owner?"Yes":"—"})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Receives tasks"}),e.jsx(U,{variant:r!=null&&r.can_receive_task_assignments?"secondary":"outline",className:"text-[10px]",children:r!=null&&r.can_receive_task_assignments?"Yes":"No"})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Receives compliance"}),e.jsx(U,{variant:r!=null&&r.can_receive_compliance_assignments?"secondary":"outline",className:"text-[10px]",children:r!=null&&r.can_receive_compliance_assignments?"Yes":"No"})]})]})]}),e.jsxs(X,{className:"p-4 space-y-3",children:[e.jsx("p",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wider",children:"Availability"}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Status"}),e.jsx(U,{variant:"outline",className:"text-[10px] capitalize",children:((ze=h==null?void 0:h.status)==null?void 0:ze.replace(/_/g," "))??"Available"})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Timezone"}),e.jsx("span",{className:"text-xs",children:(h==null?void 0:h.timezone)??"Europe/London"})]}),(q==null?void 0:q.created_at)&&e.jsxs("div",{className:"flex items-center justify-between text-xs pt-1",children:[e.jsx("span",{className:"text-muted-foreground",children:"Invited"}),e.jsx("span",{className:"text-xs",children:$e(new Date(q.created_at),"dd MMM yyyy")})]}),(q==null?void 0:q.accepted_at)&&e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsx("span",{className:"text-muted-foreground",children:"Accepted"}),e.jsx("span",{className:"text-xs",children:$e(new Date(q.accepted_at),"dd MMM yyyy")})]})]})]})]}),e.jsxs(X,{className:"p-4 space-y-3",children:[e.jsx("p",{className:"text-[11px] font-medium text-muted-foreground uppercase tracking-wider",children:"Identity"}),e.jsx(W,{}),e.jsxs("dl",{className:"grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-3 text-xs",children:[F.first_name&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"First name"}),e.jsx("dd",{children:F.first_name})]}),F.last_name&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Last name"}),e.jsx("dd",{children:F.last_name})]}),F.job_title&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Job title"}),e.jsx("dd",{children:F.job_title})]}),F.department&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Department"}),e.jsxs("dd",{className:"flex items-center gap-1.5",children:[e.jsx(ht,{className:"h-3 w-3 text-muted-foreground"}),F.department]})]}),F.email&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Email"}),e.jsx("dd",{children:F.email})]}),F.mobile&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Mobile"}),e.jsx("dd",{children:F.mobile})]}),F.office_phone_extension&&e.jsxs("div",{className:"flex justify-between",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Extension"}),e.jsx("dd",{children:F.office_phone_extension})]})]})]})]}),e.jsx(oe,{value:"access",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(yt,{membership:o,canManage:le,isSelf:Q,onRefresh:he,onOpenCopyDialog:()=>pe(!0),allMembers:R})})}),e.jsx(oe,{value:"availability",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(bt,{membership:o,canManage:le,isSelf:Q})})}),e.jsx(oe,{value:"calendar",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(kt,{membership:o,canManage:le,isSelf:Q})})}),e.jsx(oe,{value:"notifications",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(Ct,{membership:o,canManage:le,isSelf:Q})})}),e.jsx(oe,{value:"communications",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(St,{membership:o,canManage:le,isSelf:Q})})}),e.jsx(oe,{value:"voice",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(Et,{membership:o,canManage:le,isSelf:Q})})}),e.jsx(oe,{value:"assignments",className:"mt-6",children:e.jsx(X,{className:"p-6",children:e.jsx(Tt,{membership:o,canManage:le,isSelf:Q})})}),e.jsx(oe,{value:"rates",className:"mt-6",children:e.jsx(Dt,{membershipId:o.id,workspaceId:g,canManage:le})}),e.jsx(oe,{value:"audit",className:"mt-6",children:e.jsx(X,{className:"p-6",children:P.length===0?e.jsx(ds,{icon:as,title:"No audit events",description:"Access control and settings changes for this member will appear here."}):e.jsx("div",{className:"border rounded-lg overflow-x-auto",children:e.jsxs(st,{children:[e.jsx(tt,{children:e.jsxs(ms,{children:[e.jsx(Pe,{className:"text-xs",children:"Event"}),e.jsx(Pe,{className:"text-xs",children:"Actor"}),e.jsx(Pe,{className:"text-xs",children:"Details"}),e.jsx(Pe,{className:"text-xs",children:"When"})]})}),e.jsx(at,{children:P.map(I=>{var G,Z;const se=dt(I.metadata);
1return e.jsxs(ms,{children:[e.jsx(Ue,{className:"align-top",children:e.jsxs("div",{className:"flex flex-col gap-0.5",children:[e.jsx(U,{variant:"outline",className:"text-[10px] w-fit",children:mt(I.action)}),e.jsx("span",{className:"text-[10px] text-muted-foreground capitalize",children:(G=I.event_category)==null?void 0:G.replace(/_/g," ")})]})}),e.jsx(Ue,{className:"align-top text-xs",children:((Z=I.actor)==null?void 0:Z.full_name)??e.jsx("span",{className:"text-muted-foreground",children:"System"})}),e.jsx(Ue,{className:"align-top text-xs",children:e.jsxs("div",{className:"flex flex-col gap-0.5",children:[e.jsx("span",{children:I.summary??"—"}),se&&e.jsx("span",{className:"text-[10px] text-muted-foreground",children:se})]})}),e.jsx(Ue,{className:"align-top text-xs text-muted-foreground",title:$e(new Date(I.created_at),"dd MMM yyyy HH:mm"),children:ft(new Date(I.created_at),{addSuffix:!0})})]},I.id)})})]})})})})]}),e.jsx(vs,{open:!!T,onOpenChange:I=>{I||B(null)},children:e.jsxs(_s,{children:[e.jsxs(Ns,{children:[e.jsxs(ys,{children:[T==="suspend"&&`Suspend ${re}?`,T==="reactivate"&&`Reactivate ${re}?`,T==="revoke"&&`Remove ${re}?`]}),e.jsxs(ws,{children:[T==="suspend"&&"This will immediately block their access to the workspace. They can be reactivated later.",T==="reactivate"&&"This will restore their access to the workspace with their existing role and scope.",T==="revoke"&&"This will permanently revoke their membership. This action cannot be undone."]})]}),e.jsxs(bs,{children:[e.jsx(ks,{children:"Cancel"}),e.jsx(Cs,{className:T==="revoke"?"bg-destructive text-destructive-foreground hover:bg-destructive/90":"",onClick:()=>{T==="suspend"&&Ee("suspend_membership"),T==="reactivate"&&Ee("reactivate_membership"),T==="revoke"&&Ee("revoke_membership")},disabled:ie,children:ie?"Processing…":T==="revoke"?"Remove":T==="suspend"?"Suspend":"Reactivate"})]})]})}),e.jsx(Ot,{open:ue,onOpenChange:pe,targetMembership:o,allMembers:R,onCopied:he})]})}
1export{la 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.