import{i as e}from"./chunk-aKtaBQYM.js";import{Vt as t,wt as n}from"./mui-B6lh3XSP.js";import{t as r}from"./Autocomplete-BugREI_2.js";import{t as i}from"./TextField-DOdnjCrL.js";import{Mt as a,f as o,ht as s,pt as c,w as l,yt as u}from"./index-KVuxnkWj.js";var d=e(t(),1),f=n(),p=e=>e.name,m=({label:e,endpointAll:t,endpointLinked:n,baseId:m,keyBase:h,keyLink:g,titleGetter:_=p})=>{let v=u(),[y,b]=(0,d.useState)(!1),[x,S]=(0,d.useState)(),[C,w]=(0,d.useState)(),[T,E]=(0,d.useState)(!1);c(async({signal:e})=>{y&&S(await(await l(t,{signal:e})).json())},[y,t]),c(async({signal:e})=>{y&&w(await(await l(n,{signal:e})).json())},[y,n]);let D=s(async e=>{let t=e=>({[h]:m,[g]:e}),n=C.map(e=>e.id),r=e.map(e=>e.id);if(!r.find(e=>e<0)){let i=[];r.filter(e=>!n.includes(e)).forEach(e=>{i.push(l(`/api/permissions`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t(e))}))}),n.filter(e=>!r.includes(e)).forEach(e=>{i.push(l(`/api/permissions`,{method:`DELETE`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t(e))}))}),await Promise.all(i),E(i.length>0),w(e)}},[C,m,h,g]);return(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(r,{size:`small`,loading:y&&!x,isOptionEqualToValue:(e,t)=>e.id===t.id,options:x||[],getOptionLabel:e=>_(e),slotProps:{chip:{size:`small`}},renderInput:t=>(0,f.jsx)(i,{...t,label:e,placeholder:y?null:v(`reportShow`),onFocus:()=>b(!0),slotProps:{...t.slotProps,inputLabel:{...t.slotProps?.inputLabel,shrink:!y||t.slotProps?.inputLabel?.shrink}}}),value:x&&C||[],onChange:(e,t)=>D(t),multiple:!0}),(0,f.jsx)(a,{open:!!T,onClose:()=>E(!1),autoHideDuration:o,message:v(`sharedSaved`)})]})};export{m as t};