/* ==================================================
   Nuclear Fancy Pants Edition™ -- NUCLEAR+++
   File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/assets/js/amai-install-onboarding.js
   Description: JS for post-purchase one-click install onboarding page - NUCLEAR+++
   Last Edited: 2026-05-11
   ================================================== */

(function () {
    'use strict';

    var generateBtn = document.getElementById('amai-generate-btn');
    var regenBtn    = document.getElementById('amai-regen-btn');
    var siteInput   = document.getElementById('amai-site-url');
    var errorEl     = document.getElementById('amai-onboard-error');
    var formEl      = document.getElementById('amai-onboard-form');
    var resultEl    = document.getElementById('amai-onboard-result');
    var installLink = document.getElementById('amai-install-link');
    var resultSite  = document.getElementById('amai-result-site');
    var btnLabel    = generateBtn ? generateBtn.querySelector('.amai-onboard-btn-label') : null;
    var btnSpinner  = generateBtn ? generateBtn.querySelector('.amai-onboard-btn-spinner') : null;

    if (!generateBtn || !siteInput) {
        return;
    }

    function showError(msg) {
        if (!errorEl) { return; }
        errorEl.textContent = msg;
        errorEl.style.display = 'block';
    }

    function clearError() {
        if (!errorEl) { return; }
        errorEl.textContent = '';
        errorEl.style.display = 'none';
    }

    function setLoading(loading) {
        generateBtn.disabled = loading;
        if (btnLabel)   { btnLabel.style.display  = loading ? 'none'         : ''; }
        if (btnSpinner) { btnSpinner.style.display = loading ? 'inline-block' : 'none'; }
    }

    function isValidUrl(val) {
        try {
            var u = new URL(val);
            return u.protocol === 'https:' || u.protocol === 'http:';
        } catch (e) {
            return false;
        }
    }

    function doGenerate() {
        clearError();

        var siteUrl = siteInput.value.trim();
        if (!isValidUrl(siteUrl)) {
            showError('Please enter a valid site URL, for example https://yoursite.com');
            return;
        }

        setLoading(true);

        var data = new FormData();
        data.append('action', 'amai_generate_install_token');
        data.append('nonce', amaiInstall.nonce);
        data.append('site_url', siteUrl);

        fetch(amaiInstall.ajaxUrl, {
            method: 'POST',
            credentials: 'same-origin',
            body: data,
        })
        .then(function (res) { return res.json(); })
        .then(function (json) {
            setLoading(false);
            if (!json.success) {
                showError(
                    json.data && json.data.message
                        ? json.data.message
                        : 'Something went wrong. Please try again.'
                );
                return;
            }
            var connectUrl = json.data.connect_url;
            if (installLink) { installLink.href = connectUrl; }
            if (resultSite)  { resultSite.textContent = siteUrl; }
            if (formEl)      { formEl.style.display = 'none'; }
            if (resultEl)    { resultEl.style.display = 'block'; }
        })
        .catch(function () {
            setLoading(false);
            showError('Network error. Please check your connection and try again.');
        });
    }

    generateBtn.addEventListener('click', doGenerate);

    if (regenBtn) {
        regenBtn.addEventListener('click', function () {
            if (resultEl) { resultEl.style.display = 'none'; }
            if (formEl)   { formEl.style.display = 'block'; }
            siteInput.focus();
        });
    }

    siteInput.addEventListener('keydown', function (e) {
        if (e.key === 'Enter') { doGenerate(); }
    });
}());