import{am as v,r as h,j as l,b as t,q as i,C as D,B as p,S as b,ai as E,E as P,H as A,a2 as k,ag as z,c2 as R}from"./index-DlNZgIG9.js";import{d as _}from"./EditOutlined-HXlBln32.js";import{d as j}from"./ExpandMore-CXy18wMW.js";import{d as w}from"./ExpandLess-BtkvLBIN.js";import{d as I}from"./PersonOutline-Zh9Ye6JU.js";import{u as C,r as L}from"./useUserNameMap-Ce3uBxmz.js";import{C as O}from"./Collapse-DQ47Kooo.js";const W=v.injectEndpoints({endpoints:e=>({getAuditHistory:e.query({query:({entityName:a,entityId:f,actor:o,action:n,from:c,to:d,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),d&&r.set("to",d),r.set("page",String(s)),r.set("size",String(g)),`audit?${r.toString()}`},providesTags:["Audit"]})}),overrideExisting:!1}),{useGetAuditHistoryQuery:$,useLazyGetAuditHistoryQuery:X}=W,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(z,{sx:a}):e==="DELETE"?t(R,{sx:a}):t(_,{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]),d=c.length>0,s=C(),g=r=>L(s,void 0,r);return l(p,{sx:{p:1.5,borderRadius:2,border:"1px solid",borderColor:"divider",bgcolor:"background.paper"},children:[l(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"&&d&&l(i,{variant:"caption",color:"text.secondary",children:[c.length," field",c.length>1?"s":""," changed"]}),l(p,{display:"flex",alignItems:"center",gap:.5,ml:"auto",children:[t(I,{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):"—"}),d&&t(E,{size:"small",onClick:()=>f(r=>!r),endIcon:a?t(w,{}):t(j,{}),sx:{ml:.5,minWidth:0,textTransform:"none"},children:a?"Hide":"Details"})]})]}),o==="CREATE"&&!d&&t(i,{variant:"caption",color:"text.secondary",children:"Record created."}),d&&l(O,{in:a,unmountOnExit:!0,children:[t(k,{sx:{my:1}}),l(b,{spacing:.75,children:[l(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=>l(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 ee({entityName:e,entityId:a,title:f="Change History"}){const[o,n]=h.useState(0),[c,d]=h.useState([]);C(),h.useEffect(()=>{n(0),d([])},[e,a]);const{data:s,isFetching:g,isError:r}=$({entityName:e,entityId:a,page:o,size:N},{skip:!a});h.useEffect(()=>{s&&d(u=>s.currentPage===0?s.data:[...u,...s.data])},[s]);const T=!!s&&s.currentPage<s.totalPages-1;return l(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."})}):l(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{ee as A};
