import{l as r,e as s}from"./main-few-lands-burn.js";const h=e=>r({headers:{Accept:"application/json"},method:"GET",url:`https://api.telegram.org/bot${e}/getUpdates`}),u=async e=>{e.setComponent("chat-id",o=>{o.loading=!0});try{const o=await e.getConnection();if(!o||!o.auth_details?.value){e.setComponent("chat-id",t=>{t.loading=!1,e.util.isSelectComponent(t)&&(t.options=[],t.status=void 0,t.invalidMessage=void 0)});return}const{data:n,status:a}=await h(o.auth_details.value);if(a==="error"||!n?.ok){e.setComponent("chat-id",t=>{if(!e.util.isSelectComponent(t))return;t.loading=!1,t.status="error";const i=n?.description??"",l=i.toLowerCase().includes("webhook is active");t.invalidMessage=l?s("Cannot fetch chat list while a webhook is active. Please map or enter the Chat ID manually."):i||s("Failed to fetch chat list")}),console.error("Failed to fetch telegram chat list");return}const c=new Map;n?.result?.forEach(t=>{const i=t?.my_chat_member?.chat??t?.channel_post?.chat;i&&c.set(String(i.id),{label:i.title??String(i.id),value:String(i.id)})}),e.setComponent("chat-id",t=>{e.util.isSelectComponent(t)&&(t.options=[...c.values()],t.loading=!1,t.status=void 0)})}catch(o){console.error("Failed to fetch telegram chat list",o),e.setComponent("chat-id",n=>{n.loading=!1,e.util.isSelectComponent(n)&&(n.status="error",n.invalidMessage=s("Failed to fetch chat list"))})}},C=e=>({addConnectionHelpingText:s('For API token setup instructions <a target="_blank" class="text-blue-600 underline" rel="noopener noreferrer nofollow" href="https://core.telegram.org/bots/features#botfather">Click Here</a>'),componentName:e.componentName.connection,connectionsTypes:[{encryptKeys:[],label:s("API Key"),showKey:!1,type:"api_key",verifyConnection:{headers:{"Content-Type":"application/json"},method:"GET",responseKeyMatch:["result.is_bot","result.first_name"],url:""}}],fieldType:"config",id:"connection-id",label:s("Select Connection"),onConnectionAddChange:"CONNECTION_ADD_CHANGE",onConnectionChange:"SET_CONNECTION",render:!0,value:void 0}),f=({$:e,e:o})=>{e.setThisComponent(n=>{e.util.isConnectionComponent(n)&&n.connectionsTypes.forEach(a=>{a.type==="api_key"&&a.verifyConnection?.headers&&(a.verifyConnection.url="https://api.telegram.org/bot"+o.value+"/getMe")})})},p=e=>({$:o,e:n})=>{o.setThisComponent(a=>{a.value=n}),o.setDb(a=>{a[e]=n})};export{p as a,C as c,u as f,f as h};
