import{r as e,t}from"./jsx-runtime-DAs1UGHr.js";import{t as n}from"./react-DxF41JXl.js";import{b as r,d as i,i as a,j as o,o as s,t as c}from"./use-api-BAbm8GVs.js";import{n as l}from"./use-user-BydImGZB.js";import{S as u,v as d,x as f}from"./entity-service-D6-I4F0h.js";import{n as p,r as m,t as h}from"./use-toast-CpnYXdO8.js";var g=e(n(),1),_=e(t(),1),v=async(e,t)=>{let n=await a(s.CREATE_ACCESS_TOKEN,{method:`PATCH`,body:JSON.stringify({username:e,expiration:t.toISOString()})});if(!n.token)throw Error(`API response did not contain an access token (token field).`);return n.token};const y=({onClose:e,username:t,onTokenGenerated:n})=>{let r=new Date().toISOString().split(`T`)[0],a=new Date(new Date().setFullYear(new Date().getFullYear()+1)).toISOString().split(`T`)[0],{refresh:s}=l(),[c,u]=(0,g.useState)(a),[d,f]=(0,g.useState)(``),[y,b]=(0,g.useState)(!1),[x,S]=(0,g.useState)(null),{showToast:C}=h(),w=(0,g.useRef)(null),T=(0,g.useCallback)(async()=>{b(!0),f(``);try{f(await v(t,new Date(c))),C(`Access token generated successfully!`,`success`),s(),n?.()}catch(e){console.error(`Error requesting access token:`,e),C(`Failed to generate access token`,`error`)}finally{b(!1)}},[c,t,s,C,n]),E=(0,g.useCallback)(()=>{d&&w.current&&navigator.clipboard.writeText(d).then(()=>{S(`Copied!`),setTimeout(()=>S(null),2e3)}).catch(e=>{console.error(`Could not copy text: `,e),S(`Failed to copy!`),setTimeout(()=>S(null),2e3)})},[d]);return(0,_.jsxs)(m,{isOpen:!0,onClose:e,title:`Generate Access Token for ${t}`,children:[(0,_.jsx)(`p`,{className:`text-left text-base text-text-primary dark:text-text-primary-dark `,children:`Use the form below to generate a new access token. Select an expiration date (maximum validity: 1 year) and click "Request Token".`}),(0,_.jsxs)(`div`,{className:`flex items-end space-x-4`,children:[(0,_.jsx)(p,{id:`expiration-date`,label:`Expiration Date*`,type:`date`,value:c,onChange:e=>u(e.target.value),min:r,max:a,required:!0,containerClassName:`flex-grow`}),(0,_.jsx)(o,{onClick:T,disabled:y||!c,variant:`primary`,className:`h-[42px] ${y?`opacity-70 cursor-not-allowed`:``}`,children:y?`Requesting...`:`Request Token`})]}),(0,_.jsx)(`div`,{className:`relative`,children:(0,_.jsxs)(p,{ref:w,id:`access-key`,label:`Access Key`,type:`text`,readOnly:!0,value:d,placeholder:y?`Generating token...`:`Access Token`,className:`font-mono text-sm pr-12 cursor-default`,children:[(0,_.jsx)(`div`,{className:`absolute right-1 bottom-1`,children:(0,_.jsx)(o,{onClick:E,disabled:!d,title:`Copy Access Token`,variant:`ghost`,icon:i})}),x&&(0,_.jsx)(`span`,{className:`absolute right-10 bottom-1.5 text-xs px-2 py-1 rounded 
                bg-btn-primary dark:bg-btn-primary-dark text-btn-primary-text dark:text-btn-primary-text-dark 
                transition-opacity duration-300 
                ${x===`Copied!`?`opacity-100`:`opacity-0`}`,children:x})]})})]})},b=({username:e,onTokenGenerated:t})=>{let[n,i]=(0,g.useState)(!1),a=(0,g.useCallback)(()=>i(!0),[]),s=(0,g.useCallback)(()=>i(!1),[]);return(0,_.jsxs)(_.Fragment,{children:[(0,_.jsx)(o,{onClick:a,variant:`secondary`,icon:r,children:`Create Access Token`}),n&&(0,_.jsx)(y,{onClose:s,username:e,onTokenGenerated:t})]})},x=({username:e,passwordExpiration:t,onTokenGenerated:n})=>{let r=t?new Date(t):null,i=r?.toLocaleDateString(`en-US`,{year:`numeric`,month:`short`,day:`numeric`}),a=r?.toLocaleTimeString(`en-US`,{hour:`numeric`,minute:`2-digit`,hour12:!0});return(0,_.jsxs)(`div`,{className:`flex flex-col gap-2 items-start mb-2`,children:[(0,_.jsx)(`div`,{children:t==null?(0,_.jsx)(`p`,{className:`font-medium`,children:`No access token created yet`}):(0,_.jsx)(`p`,{className:`font-medium`,children:`Token expires on: ${i} at ${a}`})}),(0,_.jsx)(b,{username:e,onTokenGenerated:n})]})};function S({username:e}){let{data:t,isLoading:n,error:r,refetch:i}=c((0,g.useCallback)(t=>e===null?Promise.resolve([]):d(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:i}}function C({username:e}){let{data:t,isLoading:n,error:r,refetch:i}=c((0,g.useCallback)(t=>e===null?Promise.resolve([]):u(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:i}}function w({username:e}){let{data:t,isLoading:n,error:r,refetch:i}=c((0,g.useCallback)(t=>e===null?Promise.resolve([]):f(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:i}}export{x as i,C as n,S as r,w as t};