/**
 * Handles the click event to copy text and toggle the button icon.
 * @param {PointerEvent} event
 */
async function handleCopyClick(event) {
    const btn = event.currentTarget;
    const url = btn.dataset.shareCopy;

    try {
        await globalThis.navigator.clipboard.writeText(url);

        // Swap UI state
        const original = btn.innerHTML;
        btn.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>';

        setTimeout(() => {
            btn.innerHTML = original;
        }, 1500);
    } catch {
        // Clipboard API not available
    }
}

export function initShare() {
    const shareButtons = document.querySelectorAll('[data-share-copy]');
    shareButtons.forEach(btn => {
        btn.addEventListener('click', handleCopyClick);
    });
}
