import{get as e}from"@clevercloud/client/esm/api/v2/organisation.js";import{notifyError as t}from"./lib/notifications.js";import{sendToApi as o}from"./lib/send-to-api.js";import{sendToAuthBridge as n}from"./lib/send-to-auth-bridge.js";import{defineSmartComponent as a}from"./lib/smart/define-smart-component.js";import{i18n as i}from"./translation.js";import"./cc-smart-container.js";import{CcTokenApiCreationForm as r}from"./cc-token-api-creation-form.js";a({selector:"cc-token-api-creation-form",params:{apiConfig:{type:Object}},onContextUpdate({component:e,context:o,onEvent:n,updateComponent:a}){const{apiConfig:p}=o,c=new s(p);a("state",{type:"loading"}),c.getUserInfo().then((({isMfaEnabled:e})=>{const t={type:"loaded",activeStep:"configuration",isMfaEnabled:e,values:r.DEFAULT_FORM_VALUES};a("state",t)})).catch((e=>{console.error(e),a("state",{type:"error"})})),n("cc-token-create",(({name:o,description:n,expirationDate:r,password:s,mfaCode:p})=>{a("state",(e=>{e.type="creating"}));const d=e.state;c.createApiToken({name:o,description:n,expirationDate:r,password:s,mfaCode:p}).then((e=>{const t={...d,type:"loaded",activeStep:"copy",token:e};a("state",t)})).catch((e=>{const o="responseBody"in e&&"object"==typeof e.responseBody&&"code"in e.responseBody?e.responseBody.code:null;let n;"invalid-credential"===o&&(n="password"),"invalid-mfa-code"===o&&(n="mfaCode");const r={...d,type:"loaded",credentialsError:n};a("state",r),null==n&&t(i("cc-token-api-creation-form.validation-step.error.generic"))}))}))}});class s{constructor(e){this._authAndApiConfig=e,this._userEmail=null}_prepareCreateApiTokenRequest({name:e,description:t,expirationDate:o,password:n,mfaCode:a}){return Promise.resolve({method:"post",url:"/api-tokens",headers:{Accept:"application/json","Content-Type":"application/json"},body:{email:this._userEmail,password:n,mfaCode:a,name:e,description:t,expirationDate:o}})}createApiToken({name:e,description:t,expirationDate:o,password:a,mfaCode:i}){return this._prepareCreateApiTokenRequest({password:a,mfaCode:i,name:e,description:t,expirationDate:o}).then(n({authBridgeConfig:this._authAndApiConfig})).then((({apiToken:e})=>e))}getUserInfo(){return e({}).then(o({apiConfig:this._authAndApiConfig})).then((e=>(this._userEmail=e.email,{isMfaEnabled:"TOTP"===e.preferredMFA})))}}
//# sourceMappingURL=cc-token-api-creation-form.smart.js.map
