export function initContactCopy() { const btn = document.querySelector("[data-contact-copy]"); if (!btn) return; const hintEl = btn.querySelector("[data-contact-copy-hint]"); const defaultHint = btn.getAttribute("data-hint-default") || hintEl?.textContent?.trim() || ""; let resetTimer = 0; function fallbackCopyText(text) { const ta = document.createElement("textarea"); ta.value = text; ta.setAttribute("readonly", ""); ta.style.position = "fixed"; ta.style.left = "-9999px"; document.body.appendChild(ta); ta.select(); try { document.execCommand("copy"); } finally { document.body.removeChild(ta); } } btn.addEventListener("click", async () => { const b64 = btn.getAttribute("data-email-b64"); if (!b64 || !hintEl) return; let email = ""; try { email = atob(b64); } catch { return; } let ok = false; try { if (navigator.clipboard?.writeText) { await navigator.clipboard.writeText(email); ok = true; } } catch { ok = false; } if (!ok) { try { fallbackCopyText(email); ok = true; } catch { ok = false; } } if (resetTimer) window.clearTimeout(resetTimer); hintEl.textContent = ok ? "Скопировано" : "Не удалось скопировать"; btn.setAttribute("aria-label", ok ? "Адрес скопирован в буфер обмена" : "Не удалось скопировать адрес"); resetTimer = window.setTimeout(() => { hintEl.textContent = defaultHint; btn.setAttribute("aria-label", "Скопировать адрес электронной почты в буфер обмена"); resetTimer = 0; }, 2500); }); }