import{c as g}from"./bootstrap.esm-Bb7dPm-H.js";import{g as u,r as T}from"./api-DZ3cXm1m.js";import{b as d,c as o,d as w}from"./toastr-helper-Wdj0mZjX.js";const f=window.profilePageConfig||{},p=window.profilePageFlags||{},i=f.translations||{},c=f.routes||{};document.addEventListener("DOMContentLoaded",()=>{if(p.hasPasswordErrors){const e=document.querySelector("#password-tab");e&&new g(e).show()}h(),L(),y()});function h(){document.querySelectorAll("[data-channel-test]").forEach(e=>{e.addEventListener("click",async()=>{const t=e.getAttribute("data-channel-test");await k(e,t)})})}async function k(e,t){const n=e.innerHTML;e.disabled=!0,e.innerHTML=`<i class="fas fa-spinner fa-spin me-1"></i>${i.testing||"Testing..."}`;try{const a=await l(`${c.testBase}/${t}/test`,{method:"POST"}),s=await a.json();a.ok&&s.ok?d(s.message||i.test_sent_success||"Test sent"):o(s.message||s.error||"Failed")}catch(a){o(a.message||"Network error")}finally{e.disabled=!1,e.innerHTML=n}}function L(){const e=document.querySelector("[data-telegram-connect]");e&&e.addEventListener("click",async()=>{const t=e.innerHTML;e.disabled=!0,e.innerHTML=`<i class="fas fa-spinner fa-spin me-1"></i>${i.testing||"Loading..."}`;try{const n=await l(c.telegramLink,{method:"POST"}),a=await n.json();if(!n.ok||!a.ok){o(a.message||i.bot_not_configured_telegram||"Telegram not configured");return}window.open(a.url,"_blank","noopener,noreferrer"),w(i.telegram_link_opened||"Telegram opened — press Start to finish."),_(e,a.expires_in_seconds||600)}catch(n){o(n.message||"Network error"),e.innerHTML=t,e.disabled=!1}})}function _(e,t){e.innerHTML=`<i class="fas fa-spinner fa-spin me-1"></i>${i.telegram_waiting_for_start||"Waiting..."}`,e.disabled=!0;const a=Date.now()+t*1e3,s=3e3,r=async()=>{if(Date.now()>a){e.innerHTML=`<i class="fab fa-telegram me-1"></i>${i.connect_telegram||"Connect"}`,e.disabled=!1,o("Timeout — please try again.");return}try{const m=await(await l(c.channelStatus,{method:"GET"})).json();if(m.admin&&m.admin.telegram){d(i.telegram_linked_success||"Telegram connected"),setTimeout(()=>window.location.reload(),800);return}}catch{}setTimeout(r,s)};setTimeout(r,s)}function y(){const e=document.querySelector("[data-telegram-unlink]");e&&e.addEventListener("click",async()=>{if(!confirm(i.disconnect_telegram_confirm||"Disconnect Telegram?"))return;const t=e.innerHTML;e.disabled=!0,e.innerHTML=`<i class="fas fa-spinner fa-spin me-1"></i>${i.testing||"Loading..."}`;try{(await l(c.telegramUnlink,{method:"POST"})).ok?(d("Telegram disconnected"),setTimeout(()=>window.location.reload(),500)):(o("Failed to disconnect"),e.innerHTML=t,e.disabled=!1)}catch(n){o(n.message||"Network error"),e.innerHTML=t,e.disabled=!1}})}async function l(e,t={}){const n=r=>fetch(e,{...t,headers:{...t.headers||{},Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${r}`}});let a=u(),s=await n(a);if(s.status===401){const r=await T();r&&(s=await n(r))}return s}
