import{bx as Z,aA as Y,aB as K,r as a,b as L,bD as D,j as e,f as ee,q as l,aH as I,aY as N,bl as _,bf as $,aJ as u,c as A,a4 as q,a5 as H,aZ as O,be as se,cv as te,x as w,ci as re,Z as ce,as as G,$ as U,k as oe,dt as ne,bX as le,bW as ae,bG as ie,bH as de}from"./index-C6OVXshM.js";import{b as ue}from"./secrets.selectors-CBieCzvZ.js";import{S as i}from"./secrets-Bl4_kNye.js";const x=9e3,xe=ce().shape({name:ne.label("Name"),secret:G().required().max(x).label("Secret"),project:U.when("visibility",{is:i.project,then:oe(U.required().label("Project"))}),visibility:G()}),pe=r=>{const s=r.indexOf(",");return s>=0?r.slice(s+1):r},me=({isLoading:r,initialData:s,onSubmit:d})=>{var P;const j=Z(),{control:p,handleSubmit:g,formState:{errors:n},register:m,watch:y,setValue:S}=Y({defaultValues:{name:s==null?void 0:s.name,secret:s==null?void 0:s.secret,visibility:s==null?void 0:s.visibility,project:(P=s==null?void 0:s.project)==null?void 0:P.projectName},resolver:K(xe),shouldUnregister:!1}),k=y("visibility"),[F,z]=a.useState("Text"),[b,v]=a.useState(null),[E,h]=a.useState(""),V=a.useRef(null),{currentData:o=[]}=L(void 0,{skip:k!==i.project}),M=a.useMemo(()=>o==null?void 0:o.reduce((t,c)=>D(c.projectName,c.projectId,t),{}),[o]),B=t=>{var R;const c=(R=t.target.files)==null?void 0:R[0];if(!c)return;h("");const C=new FileReader;C.onload=W=>{var T;const f=pe((T=W.target)==null?void 0:T.result);if(f.length>x){h(`File is too large after base64 encoding (${f.length} characters, max ${x}). Pick a smaller file.`),S("secret","",{shouldValidate:!1}),v(null);return}S("secret",f,{shouldValidate:!0}),v({name:c.name,size:f.length})},C.onerror=()=>{h("Error reading file. Please try again.")},C.readAsDataURL(c);const Q=t.target;Q.value=""},X=t=>{z(t),h(""),v(null),S("secret","",{shouldValidate:!1})},J=t=>{const c={...t,project:M[t.project]};d(c)};return e.jsx(ee,{actions:e.jsx(u,{mr:"-15px",href:re("/user_guides/projects/secrets/create_secret/"),external:!0,target:"_blank",intent:"inline",children:"documentation ↗"}),title:"Create new secret",children:e.jsxs(l,{flexDirection:"column",children:[e.jsx(l,{mb:"20px",children:e.jsx(I,{...m("name"),label:"Name",disabled:r,placeholder:"name of the secret",...N("name",n)})}),e.jsx(_,{mb:"8px",children:"Secret"}),e.jsx($,{flexDirection:"row",mr:"20px",mb:"8px",disabled:r,value:F,options:["Text","File"],onChange:X}),F==="Text"?e.jsx(I,{...m("secret"),type:"textarea",width:"100%",disabled:r,placeholder:"value of the secret",...N("secret",n)}):e.jsxs(l,{flexDirection:"column",children:[e.jsxs(l,{alignItems:"center",children:[e.jsx(u,{type:"button",intent:"secondary",disabled:r,onClick:()=>{var t;return(t=V.current)==null?void 0:t.click()},children:b?"Replace file":"Choose file"}),b&&e.jsxs(A,{ml:"12px",gray:!0,children:[b.name," (",b.size," chars base64)"]})]}),e.jsx("input",{ref:V,type:"file",hidden:!0,onChange:B}),e.jsxs(A,{mt:"8px",gray:!0,children:["The file is base64-encoded in the browser. Max"," ",x," characters after encoding (about"," ",Math.floor(x*3/4)," bytes raw). Reads return the base64 string; decode client-side."]}),E&&e.jsx(l,{mt:"8px",children:e.jsx(q,{content:E,type:H.error})})]}),e.jsx(_,{mt:"20px",mb:"8px",children:"Visibility"}),e.jsx(O,{control:p,name:"visibility",defaultValue:(s==null?void 0:s.visibility)||i.private,render:({field:{onChange:t,value:c}})=>e.jsx($,{flexDirection:"row",mr:"20px",disabled:r,value:c,options:Object.values(i),onChange:t})}),k===i.project&&e.jsxs(e.Fragment,{children:[e.jsx(O,{control:p,name:"project",render:({field:{onChange:t,value:c}})=>e.jsx(se,{my:"20px",isDisabled:r||!((o==null?void 0:o.length)>0),noDataMessage:"no projects available",placeholder:"project",options:Object.keys(M),value:c,onChange:t,...te("project",n)})}),e.jsx(q,{content:"Members of the project will be able to access the secrets",type:H.warning})]}),e.jsxs(l,{mt:"20px",justifyContent:"flex-end",children:[e.jsx(u,{mr:"20px",type:"button",disabled:r,intent:"secondary",onClick:()=>j(w.account.secrets.list),children:"Back"}),e.jsx(u,{disabled:r,intent:"primary",onClick:g(J),children:"Create secret"})]})]})})},je=()=>{const r=le(ue),s=ae(),d=Z();ie(de.createSecret);const j=a.useCallback(async({name:p,secret:g,visibility:n,project:m})=>{const y={name:p,secret:g,visibility:n.toUpperCase(),...n===i.project&&{scope:m}};await s.secrets.create({data:y}),d(w.account.secrets.list)},[s.secrets,d]);return e.jsxs(e.Fragment,{children:[e.jsx(u,{mb:"15px",ml:"-12px",intent:"inline",alignSelf:"flex-start",onClick:()=>d(w.account.secrets.list),children:"⇽ all secrets"}),e.jsx(me,{isLoading:r,onSubmit:j})]})};export{je as default};
