import{get as e}from"@clevercloud/client/esm/api/v2/organisation.js";import{notifySuccess as t,notifyError as o,notify as s}from"./lib/notifications.js";import{sendToApi as i}from"./lib/send-to-api.js";import{sendToAuthBridge as r}from"./lib/send-to-auth-bridge.js";import{defineSmartComponent as n}from"./lib/smart/define-smart-component.js";import{i18n as a}from"./translation.js";import"./cc-smart-container.js";import"./cc-token-api-list.js";n({selector:"cc-token-api-list",params:{apiConfig:{type:Object}},onContextUpdate({context:e,onEvent:i,updateComponent:r}){const{apiConfig:n}=e,c=new p(n);function d(e,t){r("state",(o=>{const s=o.apiTokens.find((t=>t.id===e));null!=s&&t(s)}))}r("state",{type:"loading"}),c.getApiTokens().then((([{hasPassword:e},t])=>{if(!e)return void r("state",{type:"no-password"});const o=t.map((e=>({type:"idle",...e})));r("state",{type:"loaded",apiTokens:o})})).catch((e=>{console.error(e),r("state",{type:"error"})})),i("cc-token-revoke",(e=>{d(e,(e=>{e.type="revoking"})),c.revokeApiToken(e).then((()=>{r("state",(t=>{t.apiTokens=t.apiTokens.filter((t=>t.id!==e))})),t(a("cc-token-api-list.revoke-token.success"))})).catch((t=>{console.error(t),d(e,(e=>{e.type="idle"})),o(a("cc-token-api-list.revoke-token.error"))}))})),i("cc-password-reset",(()=>{r("state",{type:"resetting-password"}),c.resetPassword().then((e=>{r("state",{type:"no-password"}),s({intent:"info",message:a("cc-token-api-list.no-password.reset-password-successful",{email:e}),options:{timeout:0,closeable:!0}})})).catch((e=>{console.error(e),o(a("cc-token-api-list.no-password.reset-password-error")),r("state",{type:"no-password"})}))}))}});class p{constructor(e){this._authAndApiConfig=e}_getUserInfo(){return e({}).then(i({apiConfig:this._authAndApiConfig})).then((({email:e,partnerId:t,hasPassword:o})=>(this._userEmail=e,this._userPartnerId=t,{hasPassword:o})))}_listApiTokens(){return Promise.resolve({method:"get",url:"/api-tokens",headers:{Accept:"application/json"}})}_deleteApiToken(e){return Promise.resolve({method:"delete",url:`/api-tokens/${e}`,headers:{Accept:"application/json"}})}getApiTokens(){return Promise.all([this._getUserInfo(),this._listApiTokens().then(r({authBridgeConfig:this._authAndApiConfig})).then((e=>e.map((e=>({id:e.apiTokenId,creationDate:new Date(e.creationDate),expirationDate:new Date(e.expirationDate),name:e.name,description:e.description,isExpired:"EXPIRED"===e.state})))))])}_resetPasswordRequest(e){return Promise.resolve({method:"post",url:"/v2/password_forgotten",headers:{Accept:"application/json","Content-Type":"application/x-www-form-urlencoded"},body:e})}revokeApiToken(e){return this._deleteApiToken(e).then(r({authBridgeConfig:this._authAndApiConfig}))}resetPassword(){return this._resetPasswordRequest({partner_id:this._userPartnerId,login:this._userEmail,drop_tokens:"off"}).then(i({apiConfig:this._authAndApiConfig})).then((()=>this._userEmail))}}
//# sourceMappingURL=cc-token-api-list.smart.js.map
