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