import{i as e,o as t,r as n,t as r}from"./button-DJ30HgDO.js";import"./use-runtime-config-CkwqDzno.js";import"./use-api-CtLXpHJw.js";import{C as i,_ as a}from"./free-solid-svg-icons-CttZC1b4.js";import{n as o,t as s}from"./user-service-Dl9A7L06.js";import{t as c}from"./use-current-user-CIO9Sf8-.js";import"./toast-context-val-Z-lwvYkJ.js";import{i as l,n as u,r as d,t as f}from"./page-status-CNIWXN7q.js";import{t as p}from"./page-container-DN56Bi3o.js";import{t as m}from"./row-action-button-m2Rwnof1.js";import{t as h}from"./icon-button-DFhNpcsD.js";import{n as g,t as _}from"./use-toast-BWmLhWRD.js";import{t as v}from"./input-BbiN5lj4.js";import{t as y}from"./use-all-accounts-B8jcgmIk.js";var b=t(e(),1),x=t(n(),1);const S=({isOpen:e,onClose:t,onSave:n})=>{let[i,a]=(0,b.useState)(``),[o,s]=(0,b.useState)(``),[c,l]=(0,b.useState)(!1),[u,d]=(0,b.useState)(!1),f=(0,b.useRef)(null);(0,b.useEffect)(()=>{if(e){let e=setTimeout(()=>f.current?.focus(),0);return()=>clearTimeout(e)}},[e]);let p=e=>{let t=e.target.value;a(t),u||s(t)},m=e=>{s(e.target.value),d(!0)},h=async()=>{!i||!o||(await n({name:i,display_name:o,is_admin:c}),t())},_=i.trim()!==``&&o.trim()!==``;return(0,x.jsxs)(g,{isOpen:e,onClose:t,title:`Create Service Account`,children:[(0,x.jsxs)(`div`,{className:`space-y-4`,children:[(0,x.jsx)(v,{ref:f,id:`service-account-name`,label:`Service Account Name*`,type:`text`,value:i,onChange:p,placeholder:`my-service-account`,required:!0}),(0,x.jsx)(v,{id:`display-name`,label:`Display Name*`,type:`text`,value:o,onChange:m,placeholder:`My Service Account`,required:!0})]}),(0,x.jsxs)(`div`,{className:`flex items-center space-x-2`,children:[(0,x.jsx)(`input`,{id:`grant-admin`,type:`checkbox`,checked:c,onChange:e=>l(e.target.checked),className:`h-4 w-4 rounded border-gray-300 text-btn-primary focus:ring-btn-primary`}),(0,x.jsx)(`label`,{htmlFor:`grant-admin`,className:`text-sm font-medium text-text-primary dark:text-text-primary-dark cursor-pointer`,children:`Grant Admin Privileges`})]}),(0,x.jsxs)(`div`,{className:`flex justify-end space-x-3 pt-4 border-t border-ui-secondary-bg dark:border-ui-secondary-bg-dark`,children:[(0,x.jsx)(r,{onClick:t,variant:`ghost`,children:`Cancel`}),(0,x.jsx)(r,{onClick:()=>{h()},variant:`primary`,disabled:!_,children:`Save`})]})]})};function C(){let[e,t]=(0,b.useState)(!1),{searchTerm:n,submittedTerm:g,handleInputChange:v,handleSearchSubmit:C,handleClearSearch:w}=l(),{isLoading:T,error:E,refresh:D,allServiceAccounts:O}=y(),{currentUser:k}=c(),{showToast:A}=_(),j=(O||[]).filter(e=>e.toLowerCase().includes(g.toLowerCase())),M=async e=>{try{await s({username:e.name,display_name:e.display_name,is_admin:e.is_admin,is_service_account:!0}),A(`Service account ${e.name} created successfully`,`success`),D(),t(!1)}catch(e){console.error(`Failed to create service account:`,e),A(`Failed to create service account`,`error`)}},N=async e=>{try{await o(e),A(`Service account ${e} removed successfully`,`success`),D()}catch(e){console.error(`Failed to remove service account:`,e),A(`Failed to remove service account`,`error`)}},P=k?.is_admin===!0,F=j.map(e=>({id:e,username:e})),I=[{header:`Service Account Name`,render:({username:e})=>(0,x.jsx)(`span`,{className:`truncate block`,title:e,children:e})},{header:`Permissions`,render:({username:e})=>(0,x.jsx)(`div`,{className:`invisible group-hover:visible`,children:(0,x.jsx)(m,{entityId:e,suffix:`/experiments`,route:`/service-accounts`,buttonText:`Manage permissions`})}),className:`w-48`},...P?[{header:`Actions`,render:({username:e})=>(0,x.jsx)(h,{icon:i,title:`Remove service account`,onClick:()=>{N(e)}}),className:`w-24`}]:[]];return(0,x.jsxs)(p,{title:`Service Accounts`,children:[(0,x.jsx)(f,{isLoading:T,loadingText:`Loading service accounts list...`,error:E,onRetry:D}),!T&&!E&&(0,x.jsxs)(x.Fragment,{children:[P&&(0,x.jsx)(`div`,{className:`mb-2`,children:(0,x.jsx)(r,{variant:`secondary`,onClick:()=>t(!0),icon:a,children:`Create Service Account`})}),(0,x.jsx)(`div`,{className:`mb-2`,children:(0,x.jsx)(d,{value:n,onInputChange:v,onSubmit:C,onClear:w,placeholder:`Search service accounts...`})}),(0,x.jsx)(u,{data:F,columns:I,searchTerm:g}),(0,x.jsx)(S,{isOpen:e,onClose:()=>t(!1),onSave:e=>{M(e)}},e?`open`:`closed`)]})]})}export{C as default};