jQuery(document).ready(function ($) {
    $(document).on("click", ".wtc-copy-coupon", function () {
        var code = $(this).data("code");
        navigator.clipboard.writeText(code);
        var $this = $(this);
        var $copyIcon = $this.find('.wtc-icon-copy');
        var $checkIcon = $this.find('.wtc-icon-check');
        if ($copyIcon.length && $checkIcon.length) {
            $copyIcon.hide();
            $checkIcon.show();
            setTimeout(function () {
                $checkIcon.hide();
                $copyIcon.show();
            }, 2000);
        }
    });
    $(document).on('click', '.wtc-info-trigger', function (e) {
        e.stopPropagation();
        $('.wtc-modern-popover').remove();
        const title = $(this).data('title');
        let listData = $(this).data('list');
        if (typeof listData === 'string') {
            try { listData = JSON.parse(listData); } catch (e) { return; }
        }
        if (!Array.isArray(listData)) return;
        const listHtml = listData.map(item => {
            return `<li><a href="${item.url}" target="_blank" class="wtc-popover-link">${item.name}</a></li>`;
        }).join('');
        const popover = $(`
            <div class="wtc-modern-popover">
                <div class="wtc-popover-header">${title}</div>
                <ul class="wtc-popover-list">${listHtml}</ul>
                <div class="wtc-popover-arrow"></div>
            </div>
        `);
        $('body').append(popover);
        const trigger = $(this);
        const offset = trigger.offset();
        popover.css({
            top: offset.top + (trigger.height() / 2) - (popover.outerHeight() / 2),
            left: offset.left - popover.outerWidth() - 15,
            opacity: 1,
            pointerEvents: 'auto'
        });
    });
    $(document).on('click', function () { $('.wtc-modern-popover').remove(); });
});