import{todo_listSelfTokens as e,todo_revokeSelfToken as t}from"@clevercloud/client/esm/api/v2/user.js";import{notifySuccess as o,notifyError as n}from"./lib/notifications.js";import{sendToApi as r}from"./lib/send-to-api.js";import{defineSmartComponent as a}from"./lib/smart/define-smart-component.js";import{i18n as s}from"./translation.js";import"./cc-smart-container.js";import"./cc-token-oauth-list.js";a({selector:"cc-token-oauth-list",params:{apiConfig:{type:Object},authBridgeConsumerKey:{type:String}},onContextUpdate({component:e,context:t,onEvent:r,updateComponent:a}){const{apiConfig:l,authBridgeConsumerKey:c}=t,k=new i(l,c);function u(e,t){a("state",(o=>{const n=o.oauthTokens.find((t=>t.id===e));null!=n&&t(n)}))}a("state",{type:"loading"}),k.getOauthTokens().then((e=>{const t=e.map((e=>({type:"idle",...e})));a("state",{type:"loaded",oauthTokens:t})})).catch((()=>{a("state",{type:"error"})})),r("cc-token-revoke",(e=>{u(e,(e=>{e.type="revoking"})),k.revokeOauthToken(e).then((()=>{a("state",(t=>{t.oauthTokens=t.oauthTokens.filter((t=>t.id!==e))})),o(s("cc-token-oauth-list.revoke-token.success"))})).catch((t=>{console.error(t),u(e,(e=>{e.type="idle"})),n(s("cc-token-oauth-list.revoke-token.error"))}))})),r("cc-tokens-revoke-all",(()=>{a("state",(e=>({...e,type:"revoking-all",oauthTokens:e.oauthTokens.map((e=>({...e,type:"revoking"})))})));const t=e.state.oauthTokens;k.revokeAllOauthTokens(t).then((({remainingTokens:e,errors:t,revokedTokens:r})=>{a("state",(t=>(t.type="loaded",t.oauthTokens=e.map((e=>({...e,type:"idle"}))),t))),0===t.length?o(s("cc-token-oauth-list.revoke-all-tokens.success")):r.length>0?(t.forEach((e=>console.error(e))),n(s("cc-token-oauth-list.revoke-all-tokens.partial-error"))):(t.forEach((e=>console.error(e))),n(s("cc-token-oauth-list.revoke-all-tokens.error")))}))}))}});class i{constructor(e,t){this._apiConfig=e,this._authBridgeConsumerKey=t}getOauthTokens(){return e().then(r({apiConfig:this._apiConfig})).then((e=>e.filter((e=>e.consumer.key!==this._apiConfig.OAUTH_CONSUMER_KEY&&null==e.employeeId&&e.consumer.key!==this._authBridgeConsumerKey)).map((e=>({id:e.token,consumerName:e.consumer.name,creationDate:new Date(e.creationDate),expirationDate:new Date(e.expirationDate),lastUsedDate:new Date(e.lastUtilisation),imageUrl:e.consumer.picture})))))}revokeOauthToken(e){return t({token:e}).then(r({apiConfig:this._apiConfig}))}revokeAllOauthTokens(e){let t=[],o=[];return Promise.allSettled(e.map((e=>this.revokeOauthToken(e.id).then((()=>e.id))))).then((n=>{o=n.filter((e=>"fulfilled"===e.status)).map((({value:e})=>e)),t=n.filter((e=>"rejected"===e.status));return{remainingTokens:e.filter((e=>!o.includes(e.id))),revokedTokens:o,errors:t}}))}}
//# sourceMappingURL=cc-token-oauth-list.smart.js.map
