class PaywallManager {
    constructor() {
        
    }    

    _performUnlock(articleId, source) {

        const primaryArticle = document.querySelector('.article-page-content:not(#secondary_article_content)');
        
        if (!primaryArticle) {
            console.warn('PaywallManager: Primary article container not found');
            return false;
        }

        const paywall = primaryArticle.querySelector('.prompt-login-wrapper');
        if (paywall) {
            paywall.style.display = 'none';
        }

        const premiumContent = primaryArticle.querySelector('.premium-content');
        if (premiumContent) {
            premiumContent.style.display = 'block';
        }

        if (document.body) {
            document.body.classList.remove('locked');
            document.body.classList.add('all-lock-released');
        }

        if (typeof jQuery !== 'undefined') {
            const $primaryArticle = jQuery('.article-page-content').not('#secondary_article_content');
            $primaryArticle.find(".open-unlock-modal").closest(".tipping-button").hide(); 
            $primaryArticle.find(".prompt-login-buttons-wrapper .login-button").hide(); 
        }

        if (typeof updateDonationButtonVisibility === 'function') {
            updateDonationButtonVisibility(false, true);
        }

        console.log(`PaywallManager v2: PRIMARY article ${articleId} unlocked via ${source}`);
        this.dispatchPaywallEvent('paywallRemoved', articleId);
        
        return true;
    }

    removePaywallIfApiAllows(articleId) {
        return this._performUnlock(articleId, 'API');
    }

    removePaywallIfBrowserAllows(articleId) {
        return this._performUnlock(articleId, 'Local Browser Data');
    }



    _performUnlockSecondary(articleId, source) {
        const secondaryArticle = document.querySelector('#secondary_article_content');        
        if (!secondaryArticle) {
            console.warn('PaywallManager: Secondary article not found');
            return false;
        }

        const paywall = secondaryArticle.querySelector('.prompt-login-wrapper');
        if (paywall) {
            paywall.style.display = 'none';
        }

        const premiumContent = secondaryArticle.querySelector('.premium-content');
        if (premiumContent) {
            premiumContent.style.display = 'block';
        }

        const $secondaryArticle = jQuery('#secondary_article_content');
        $secondaryArticle.find(".open-unlock-modal").closest(".tipping-button").hide(); 
        $secondaryArticle.find(".prompt-login-buttons-wrapper .login-button").hide();     

        console.log(`PaywallManager v2: SECONDARY article ${articleId} unlocked via ${source}`);
        this.dispatchPaywallEvent('secondaryPaywallRemoved', articleId);
        
        return true;
    }
    
    removeSecondaryPaywallIfApiAllows(articleId) {
        return this._performUnlockSecondary(articleId, 'API');
    }

    removeSecondaryPaywallIfBrowserAllows(articleId) {
        return this._performUnlockSecondary(articleId, 'Local Browser Data');
    }



    dispatchPaywallEvent(eventName, articleId = null) {
        const event = new CustomEvent(`paywall:${eventName}`, {
            detail: { articleId: articleId }
        });
        window.dispatchEvent(event);
    }

    wallCheck(articleId) {
        if($('body').hasClass("page-template-video-baige")) {
            return;
        }

        if (!articleId) {
            console.log('PaywallManager v2: No article ID provided to wallCheck().');
            return;
        }
        
        console.log('PaywallManager v2: Ready for article ID:', articleId);
    }

    addPaidArticle(articleId) {
        if (typeof(localStorage) !== "undefined") {
            localStorage.setItem('paid_article_' + articleId, "Y");
        }
    }
}

window.paywallManager = new PaywallManager();