import{getKeys as e}from"@clevercloud/client/esm/api/v2/github.js";import{get as t}from"@clevercloud/client/esm/api/v2/organisation.js";import{todo_addSshKey as i,todo_removeSshKey as n,todo_getSshKeys as a}from"@clevercloud/client/esm/api/v2/user.js";import{ONE_DAY as s}from"@clevercloud/client/esm/with-cache.js";import{defineSmartComponent as o}from"./lib/define-smart-component.js";import{notifySuccess as r,notifyError as c}from"./lib/notifications.js";import{sendToApi as l}from"./lib/send-to-api.js";import{i18n as m}from"./translation.js";import"./cc-smart-container.js";import"./cc-ssh-key-list.js";async function y({apiConfig:e,key:t}){const n=encodeURIComponent(t.name),a=t.key;return i({key:n},JSON.stringify(a)).then(l({apiConfig:e}))}o({selector:"cc-ssh-key-list",params:{apiConfig:{type:Object}},onContextUpdate({component:i,context:o,onEvent:h,updateComponent:f,signal:g}){const{apiConfig:d}=o;function k(){return async function({apiConfig:i,signal:n,cacheDelay:o}){const[r,c]=await Promise.all([t({}).then(l({apiConfig:i,signal:n,cacheDelay:s})),a().then(l({apiConfig:i,signal:n,cacheDelay:o}))]),m=r.oauthApps.includes("github");let y;m&&(y=await e().then(l({apiConfig:i,signal:n,cacheDelay:o})));return{isGithubLinked:m,personalKeys:c,githubKeys:y}}({apiConfig:d,signal:g,cacheDelay:0}).then((({isGithubLinked:e,personalKeys:t,githubKeys:i})=>{f("keyData",{state:"loaded",isGithubLinked:e,personalKeys:t.map((e=>({...e,state:"idle"}))),githubKeys:i?.map((e=>({...e,state:"idle"})))})})).catch((e=>{console.error(e),f("keyData",{state:"error"})}))}h("cc-ssh-key-list:create",(({name:e,publicKey:t})=>{i.createKeyFormState={type:"creating"},y({apiConfig:d,key:{name:e.trim(),key:t.trim()}}).then((()=>{k().then((()=>{r(m("cc-ssh-key-list.success.add",{name:e})),i.resetCreateKeyForm()}))})).catch((t=>{console.error(t),c(t,m("cc-ssh-key-list.error.add",{name:e}))})).finally((()=>{i.createKeyFormState={type:"idle"}}))})),h("cc-ssh-key-list:delete",(({name:e})=>{f("keyData",(t=>{t.personalKeys.find((t=>t.name===e)).state="deleting"})),async function({apiConfig:e,key:t}){const i=encodeURIComponent(t.name);return n({key:i}).then(l({apiConfig:e}))}({apiConfig:d,key:{name:e}}).then((()=>{k().then((()=>r(m("cc-ssh-key-list.success.delete",{name:e}))))})).catch((t=>{console.error(t),c(t,m("cc-ssh-key-list.error.delete",{name:e})),f("keyData",(t=>{t.personalKeys.find((t=>t.name===e)).state="idle"}))}))})),h("cc-ssh-key-list:import",(({name:e,key:t,fingerprint:i})=>{f("keyData",(t=>{t.githubKeys.find((t=>t.name===e)).state="importing"})),p({apiConfig:d,key:{name:e,key:t}}).then((()=>{r(m("cc-ssh-key-list.success.import",{name:e})),f("keyData",(t=>{t.personalKeys.push({state:"idle",name:e,fingerprint:i}),t.githubKeys=t.githubKeys.filter((t=>t.name!==e))}))})).catch((t=>{console.error(t),c(t,m("cc-ssh-key-list.error.import",{name:e})),f("keyData",(t=>{t.githubKeys.find((t=>t.name===e)).state="idle"}))}))})),f("keyData",{state:"loading"}),i.createKeyFormState={type:"idle"},i.resetCreateKeyForm(),k()}});const p=y;
//# sourceMappingURL=cc-ssh-key-list.smart.js.map
