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