import{al as v,r as h,j as d,b as t,q as i,C as D,B as p,S as b,an as E,E as P,H as A,a4 as k,b$ as z,a6 as R,ak as _,c0 as j}from"./index-ktJf6c6X.js";import{d as w}from"./EditOutlined-yP8PUlNu.js";import{d as I}from"./ExpandLess-D1GvYHTD.js";import{d as L}from"./PersonOutline-CqOBkkml.js";import{u as C,r as O}from"./useUserNameMap-CSUTJJdj.js";const $=v.injectEndpoints({endpoints:e=>({getAuditHistory:e.query({query:({entityName:a,entityId:f,actor:o,action:n,from:c,to:l,page:s,size:g})=>{const r=new URLSearchParams;return r.set("entityName",a),f!=null&&r.set("entityId",String(f)),o&&r.set("actor",o),n&&r.set("action",n),c&&r.set("from",c),l&&r.set("to",l),r.set("page",String(s)),r.set("size",String(g)),`audit?${r.toString()}`},providesTags:["Audit"]})}),overrideExisting:!1}),{useGetAuditHistoryQuery:W,useLazyGetAuditHistoryQuery:Y}=$,N=25,x={CREATE:{bg:"#22bb3418",fg:"#1b8a2a",label:"Created"},UPDATE:{bg:"#196ae518",fg:"#196ae5",label:"Updated"},DELETE:{bg:"#e5393518",fg:"#c62828",label:"Deleted"}};function U({action:e}){const a={fontSize:18,color:x[e].fg};return e==="CREATE"?t(_,{sx:a}):e==="DELETE"?t(j,{sx:a}):t(w,{sx:a})}function B(e){return e.replace(/_/g," ").replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/\b\w/g,a=>a.toUpperCase())}const H=/^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}/;function y(e){return e==null||e===""?"—":typeof e=="string"&&H.test(e)?A(e)||e:typeof e=="object"?JSON.stringify(e):String(e)}function m(e){if(e)try{return JSON.parse(e)}catch{return}}function M(e){const a=m(e.changedFields)??[],f=m(e.oldData)??{},o=m(e.newData)??{};return a.map(n=>({field:n,oldValue:f[n],newValue:o[n]}))}function V({row:e}){const[a,f]=h.useState(!1),o=e.action??"UPDATE",n=x[o]??x.UPDATE,c=h.useMemo(()=>M(e),[e]),l=c.length>0,s=C(),g=r=>O(s,void 0,r);return d(p,{sx:{p:1.5,borderRadius:2,border:"1px solid",borderColor:"divider",bgcolor:"background.paper"},children:[d(p,{display:"flex",alignItems:"center",flexWrap:"wrap",gap:1,children:[t(U,{action:o}),t(P,{size:"small",label:n.label,sx:{bgcolor:n.bg,color:n.fg,fontWeight:700}}),o!=="CREATE"&&l&&d(i,{variant:"caption",color:"text.secondary",children:[c.length," field",c.length>1?"s":""," changed"]}),d(p,{display:"flex",alignItems:"center",gap:.5,ml:"auto",children:[t(L,{sx:{fontSize:15,color:"text.disabled"}}),t(i,{variant:"caption",color:"text.secondary",children:g(e.actor)||"system"}),t(i,{variant:"caption",color:"text.disabled",ml:1,children:e.occurredAt?A(e.occurredAt):"—"}),l&&t(E,{size:"small",onClick:()=>f(r=>!r),endIcon:a?t(I,{}):t(k,{}),sx:{ml:.5,minWidth:0,textTransform:"none"},children:a?"Hide":"Details"})]})]}),o==="CREATE"&&!l&&t(i,{variant:"caption",color:"text.secondary",children:"Record created."}),l&&d(z,{in:a,unmountOnExit:!0,children:[t(R,{sx:{my:1}}),d(b,{spacing:.75,children:[d(p,{display:"grid",gridTemplateColumns:"minmax(120px, 1fr) 1fr 1fr",gap:1,children:[t(i,{variant:"caption",fontWeight:700,color:"text.secondary",children:"Field"}),t(i,{variant:"caption",fontWeight:700,color:"text.secondary",children:"Old"}),t(i,{variant:"caption",fontWeight:700,color:"text.secondary",children:"New"})]}),c.map(r=>d(p,{display:"grid",gridTemplateColumns:"minmax(120px, 1fr) 1fr 1fr",gap:1,children:[t(i,{variant:"caption",fontWeight:600,color:"text.primary",sx:{wordBreak:"break-word"},children:B(r.field)}),t(i,{variant:"caption",sx:{color:"#c62828",wordBreak:"break-word"},children:y(r.oldValue)}),t(i,{variant:"caption",sx:{color:"#1b8a2a",wordBreak:"break-word"},children:y(r.newValue)})]},r.field))]})]})]})}function K({entityName:e,entityId:a,title:f="Change History"}){const[o,n]=h.useState(0),[c,l]=h.useState([]);C(),h.useEffect(()=>{n(0),l([])},[e,a]);const{data:s,isFetching:g,isError:r}=W({entityName:e,entityId:a,page:o,size:N},{skip:!a});h.useEffect(()=>{s&&l(u=>s.currentPage===0?s.data:[...u,...s.data])},[s]);const T=!!s&&s.currentPage<s.totalPages-1;return d(p,{pt:2,children:[t(i,{variant:"subtitle1",fontWeight:700,color:"text.primary",mb:2,children:f}),g&&o===0?t(p,{display:"flex",justifyContent:"center",py:4,children:t(D,{size:32})}):r&&c.length===0?t(p,{py:4,textAlign:"center",children:t(i,{variant:"body2",color:"error",children:"Couldn't load change history. Please try again."})}):c.length===0?t(p,{py:4,textAlign:"center",children:t(i,{variant:"body2",color:"text.secondary",children:"No changes recorded yet."})}):d(b,{spacing:1.5,children:[c.map((u,S)=>t(V,{row:u},u.id??S)),T&&t(p,{display:"flex",justifyContent:"center",pt:1,children:t(E,{size:"small",variant:"outlined",disabled:g,onClick:()=>n(u=>u+1),sx:{textTransform:"none",borderRadius:2},children:g?"Loading…":"Load more"})})]})]})}export{K as A};
