import{r as N,j as t}from"./react-vendor-MPLM8hiW.js";import{u as E}from"./tanstack-vendor-Dk_RmDT8.js";import{a as $,u as B,i as O,k as o,r as Y,m as L,C as _,e as k,ay as G,y as K,F as P}from"./index-saDuzM6I.js";import{f as U,b as z,c as H,a as J}from"./activity-format-CSLEHg8d.js";import{az as V,m as W,L as X,ay as Z}from"./icons-vendor-H2PpUqT3.js";import{L as I}from"./vendor-CwCtuXvS.js";const tt={content:"primary",schema:"info",media:"success",settings:"warning",auth:"muted",collaboration:"info"},et={content:"activity.categoryContent",schema:"activity.categorySchema",media:"activity.categoryMedia",settings:"activity.categorySettings",auth:"activity.categoryAuth",collaboration:"activity.categoryCollaboration"};function ot({entityType:r,entityId:s,titleKey:f="activity.title",limit:c=5,className:C,showViewAll:S=!0,showActivity:R=!0,showAudit:D=!1}){const{t:a}=$(),n=B(i=>i.user),v=O(),x=v.data,q=o(n,"*")||o(n,"activity:read")||o(n,"admin:activity:list"),F=o(n,"*")||o(n,"audit-logs:read")||o(n,"admin:audit-logs:list"),p=Y(x,"compliance.audit-log"),h=!!(p!=null&&p.enabled||(x==null?void 0:x.license.enforcement)==="observe"),l=R&&q&&h,d=D&&F&&h,u=E({queryKey:["entity-activity",r,s,c],queryFn:()=>K.list({entityType:r,entityId:s!==void 0?String(s):void 0,limit:c,offset:0}),enabled:l}),g=E({queryKey:["entity-audit-logs",r,s,c],queryFn:()=>P.list({targetEntityType:r,targetEntityId:s!==void 0?String(s):void 0,limit:c,offset:0}),enabled:d}),j=N.useMemo(()=>{var A,b;const i=(((A=u.data)==null?void 0:A.data)??[]).map(e=>{const m=tt[e.category]??"default";return{item:{id:`activity-${e.id}`,tone:m,title:t.jsx("span",{className:"text-sm",children:U(e,a)}),description:t.jsx("div",{className:"flex flex-wrap items-center gap-1.5 text-xs text-text-secondary",children:t.jsx(L,{variant:"info",children:a(et[e.category])})}),meta:t.jsx("span",{className:"text-xs text-text-tertiary whitespace-nowrap",children:new Date(e.createdAt).toLocaleString()})},createdAt:e.createdAt}}),T=(((b=g.data)==null?void 0:b.data)??[]).map(e=>{var w;const m=e.outcome==="success"?"muted":e.outcome==="denied"?"warning":"error";return{item:{id:`audit-${e.id}`,tone:m,title:t.jsx("span",{className:"text-sm",children:J(e,a)}),description:t.jsxs("div",{className:"flex flex-wrap items-center gap-1.5 text-xs text-text-secondary",children:[t.jsx(L,{variant:e.outcome==="success"?"outline":"warning",children:a("auditLogs.immutable")}),t.jsx("span",{children:z(e.target,a)??H(((w=e.target)==null?void 0:w.entityType)??r,a)})]}),meta:t.jsx("span",{className:"text-xs text-text-tertiary whitespace-nowrap",children:new Date(e.createdAt).toLocaleString()})},createdAt:e.createdAt}});return[...i,...T].sort((e,m)=>Date.parse(m.createdAt)-Date.parse(e.createdAt)).slice(0,c).map(e=>e.item)},[u.data,g.data,r,c,a]),M=N.useMemo(()=>{const i=new URLSearchParams;return d&&!l?(i.set("targetEntityType",r),s!==void 0&&i.set("targetEntityId",String(s)),`/audit-logs?${i.toString()}`):(i.set("entityType",r),s!==void 0&&i.set("entityId",String(s)),`/activity?${i.toString()}`)},[l,d,r,s]),Q=l&&u.isLoading||d&&g.isLoading,y=u.error??g.error;return v.isLoading||!l&&!d?null:t.jsx(_,{className:C,children:t.jsxs(k,{className:"py-4 space-y-3",children:[t.jsxs("div",{className:"flex items-center justify-between gap-2",children:[t.jsxs("h3",{className:"text-sm font-semibold flex items-center gap-1.5",children:[t.jsx(V,{className:"h-4 w-4"}),a(f)]}),S&&t.jsxs(I,{to:M,className:"inline-flex items-center gap-0.5 text-xs text-primary hover:underline",children:[a("dashboard.viewAllActivity"),t.jsx(W,{className:"h-3 w-3"})]})]}),Q?t.jsx("div",{className:"flex items-center justify-center py-6",children:t.jsx(X,{className:"h-5 w-5 animate-spin text-text-secondary"})}):y?t.jsxs("div",{className:"flex items-center gap-2 py-3 text-sm text-error",children:[t.jsx(Z,{className:"h-4 w-4"}),t.jsx("span",{children:y instanceof Error?y.message:a("errors.serverError")})]}):j.length===0?t.jsx("p",{className:"text-sm text-text-tertiary py-2",children:a("activity.empty")}):t.jsx(G,{items:j,ariaLabel:a(f)})]})})}export{ot as E};
