import{i as e,o as t,r as n,t as r}from"./button-DJ30HgDO.js";import{t as i}from"./use-api-CtLXpHJw.js";import{n as a}from"./use-user-D7emtooi.js";import{_ as o,i as s}from"./free-solid-svg-icons-CttZC1b4.js";import{a as c,r as l}from"./create-api-fetcher-BF8Rcwoh.js";import{_ as u,x as d,y as f}from"./gateway-service-BwyflEJR.js";import{n as p,t as m}from"./use-toast-BWmLhWRD.js";import{t as h}from"./input-BbiN5lj4.js";import{S as g,v as _,x as v}from"./entity-service-KdPsD3WQ.js";var y=t(e(),1),b=t(n(),1),x=async(e,t)=>{let n=await l(c.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 S=({onClose:e,username:t,onTokenGenerated:n})=>{let i=new Date().toISOString().split(`T`)[0],o=new Date(new Date().setFullYear(new Date().getFullYear()+1)).toISOString().split(`T`)[0],{refresh:c}=a(),[l,u]=(0,y.useState)(o),[d,f]=(0,y.useState)(``),[g,_]=(0,y.useState)(!1),[v,S]=(0,y.useState)(null),{showToast:C}=m(),w=(0,y.useRef)(null),T=(0,y.useCallback)(async()=>{_(!0),f(``);try{f(await x(t,new Date(l))),C(`Access token generated successfully`,`success`),c(),n?.()}catch(e){console.error(`Error requesting access token:`,e),C(`Failed to generate access token`,`error`)}finally{_(!1)}},[l,t,c,C,n]),E=(0,y.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,b.jsxs)(p,{isOpen:!0,onClose:e,title:`Generate Access Token for ${t}`,children:[(0,b.jsx)(`p`,{className:`text-left 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,b.jsxs)(`div`,{className:`flex items-end space-x-4`,children:[(0,b.jsx)(h,{id:`expiration-date`,label:`Expiration Date*`,type:`date`,value:l,onChange:e=>u(e.target.value),min:i,max:o,required:!0,containerClassName:`flex-grow`,className:`text-text-primary dark:text-text-primary-dark dark:scheme-dark cursor-pointer`}),(0,b.jsx)(r,{onClick:T,disabled:g||!l,variant:`primary`,className:`h-[42px] ${g?`opacity-70 cursor-not-allowed`:``}`,children:g?`Requesting...`:`Request Token`})]}),(0,b.jsx)(`div`,{className:`relative`,children:(0,b.jsxs)(h,{ref:w,id:`access-key`,label:`Access Key`,type:`text`,readOnly:!0,value:d,placeholder:g?`Generating token...`:`Access Token`,className:`font-mono text-sm pr-12 cursor-default`,children:[(0,b.jsx)(`div`,{className:`absolute right-1 bottom-1`,children:(0,b.jsx)(r,{onClick:E,disabled:!d,title:`Copy Access Token`,variant:`ghost`,icon:s})}),v&&(0,b.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
                ${v===`Copied!`?`opacity-100`:`opacity-0`}`,children:v})]})})]})},C=({username:e,onTokenGenerated:t})=>{let[n,i]=(0,y.useState)(!1),a=(0,y.useCallback)(()=>i(!0),[]),s=(0,y.useCallback)(()=>i(!1),[]);return(0,b.jsxs)(b.Fragment,{children:[(0,b.jsx)(r,{onClick:a,variant:`secondary`,icon:o,children:`Create Access Token`}),n&&(0,b.jsx)(S,{onClose:s,username:e,onTokenGenerated:t})]})},w=({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,b.jsxs)(`div`,{className:`flex flex-col gap-2 items-start mb-2`,children:[(0,b.jsx)(`div`,{children:t==null?(0,b.jsx)(`p`,{className:`font-medium`,children:`No access token created yet`}):(0,b.jsx)(`p`,{className:`font-medium`,children:`Token expires on: ${i} at ${a}`})}),(0,b.jsx)(C,{username:e,onTokenGenerated:n})]})};function T({username:e}){let{data:t,isLoading:n,error:r,refetch:a}=i((0,y.useCallback)(t=>e===null?Promise.resolve([]):_(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:a}}function E({username:e}){let{data:t,isLoading:n,error:r,refetch:a}=i((0,y.useCallback)(t=>e===null?Promise.resolve([]):g(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:a}}function D({username:e}){let{data:t,isLoading:n,error:r,refetch:a}=i((0,y.useCallback)(t=>e===null?Promise.resolve([]):v(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:a}}function O({username:e}){let{data:t,isLoading:n,error:r,refetch:a}=i((0,y.useCallback)(t=>e===null?Promise.resolve([]):u(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:a}}function k({username:e}){let{data:t,isLoading:n,error:r,refetch:a}=i((0,y.useCallback)(t=>e===null?Promise.resolve([]):d(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:a}}function A({username:e}){let{data:t,isLoading:n,error:r,refetch:a}=i((0,y.useCallback)(t=>e===null?Promise.resolve([]):f(e,t),[e]));return{permissions:t??[],isLoading:n,error:r,refresh:a}}export{E as a,D as i,k as n,T as o,O as r,w as s,A as t};