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 a,todo_getSshKeys as n}from"@clevercloud/client/esm/api/v2/user.js";import{ONE_DAY as s}from"@clevercloud/client/esm/with-cache.js";import{notifySuccess as o,notifyError as r}from"./lib/notifications.js";import{sendToApi as c}from"./lib/send-to-api.js";import{defineSmartComponent as l}from"./lib/smart/define-smart-component.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 a=encodeURIComponent(t.name),n=t.key;return i({key:a},JSON.stringify(n)).then(c({apiConfig:e}))}l({selector:"cc-ssh-key-list",params:{apiConfig:{type:Object}},onContextUpdate({component:i,context:l,onEvent:h,updateComponent:f,signal:g}){const{apiConfig:d}=l;function k(){return async function({apiConfig:i,signal:a,cacheDelay:o}){const[r,l]=await Promise.all([t({}).then(c({apiConfig:i,signal:a,cacheDelay:s})),n().then(c({apiConfig:i,signal:a,cacheDelay:o}))]),m=r.oauthApps.includes("github");let y;m&&(y=await e().then(c({apiConfig:i,signal:a,cacheDelay:o})));return{isGithubLinked:m,personalKeys:l,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((()=>{o(m("cc-ssh-key-list.success.add",{name:e})),i.resetCreateKeyForm()}))})).catch((t=>{console.error(t),r(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 a({key:i}).then(c({apiConfig:e}))}({apiConfig:d,key:{name:e}}).then((()=>{k().then((()=>o(m("cc-ssh-key-list.success.delete",{name:e}))))})).catch((t=>{console.error(t),r(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((()=>{o(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),r(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
