// مدیریت کلیک روی لینک‌های با کلاس 'no_follow'
document.querySelectorAll('.no_follow').forEach(link => {
    link.addEventListener('click', (event) => {
        event.preventDefault();
    });
});

let responseData = null;

document.addEventListener('DOMContentLoaded', async function() {
    const loadingElement = document.getElementById('loading');
    const errorMessageElement = document.getElementById('error-message');
    const contentElement = document.getElementById('content');

    if (loadingElement) {
        loadingElement.style.display = 'block';
        loadingElement.innerHTML = 'در حال بارگزاری...';
    }

    try {
        const input_data = JSON.parse(window.Kodular.getWebViewString());
        const base_url = input_data.base_url;
        const access_token = input_data.access_token;
        const page_name = input_data.page_name;
        const other_value = input_data.other_value || ''; 

        const response = await fetch(`${base_url}?page_name=${page_name}${other_value}`, {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${access_token}`,
                'Content-Type': 'application/json',
            }
        });

        if (response.ok) {
            responseData = await response.json();
            handleResponseData();
            if (loadingElement) loadingElement.style.display = 'none';
            if (errorMessageElement) errorMessageElement.style.display = 'none';
            if (contentElement) contentElement.style.display = 'block';
        } else {
            if (errorMessageElement) {
                errorMessageElement.innerHTML = `خطایی رخ داده است <br>${response.status}<br><br><button id="back_to_start_page" class="btn btn-food view-cart-button">بازگشت به صفحه ورود<i class="ri-arrow-left-line"></i></button>`;
                errorMessageElement.style.display = 'block';
            }
            throw new Error('Network response was not ok');
        }

    } catch (error) {
        if (loadingElement) loadingElement.style.display = 'none';
        if (errorMessageElement) errorMessageElement.style.display = 'block';
        console.error(error);
    }

    // مدیریت رویداد کلیک دکمه بازگشت به صفحه ورود
    const backToStartPageButton = document.getElementById('back_to_start_page');
    if (backToStartPageButton) {
        backToStartPageButton.addEventListener('click', function() {
            window.Kodular.setWebViewString('back_to_start_page');
        });
    }
});

function handleResponseData() {
    if (responseData) {
        const defultAddressTitle = document.getElementById('defult_address_title');
        const defultAddressElement = document.getElementById('defult_address_address');

        if (defultAddressTitle && responseData.defult_address) {
            defultAddressTitle.innerHTML = `${responseData.defult_address.title} <i class="ri-arrow-down-s-line"></i>`;
        }

        if (defultAddressElement && responseData.defult_address) {
            const fullAddress = responseData.defult_address.address;
            const maxLength = 40;
            const displayAddress = fullAddress.length > maxLength ? fullAddress.substring(0, maxLength) + '...' : fullAddress;
            defultAddressElement.innerHTML = displayAddress;
        }
    } else {
        console.log('No data available');
    }
}


function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
const getAddressBookPageButton = document.getElementById('get_address_book_page');
if (getAddressBookPageButton) {
    getAddressBookPageButton.addEventListener('click', async (event) => {
        event.preventDefault(); 
        window.Kodular.setWebViewString('address_book');
        await delay(1);
        const url = getAddressBookPageButton.getAttribute('href');
        if (url) {
            window.location.href = url;
        } else {
            console.error('No href found for get_address_book_page');
        }
    });
}



کد بالا رو در نظر بگی و کد \ایین رو هم مرتب و بهینه شده بنویس
document.addEventListener('DOMContentLoaded', async function () {
    const loadingElement = document.getElementById('loading');
    const errorMessageElement = document.getElementById('error-message');
    const contentElement = document.getElementById('content');
    loadingElement.style.display = 'block';
    loadingElement.innerHTML = 'در حال بارگذاری...';

    try {
        const input_data = JSON.parse(window.Kodular.getWebViewString());
        const base_url = input_data.base_url;
        const access_token = input_data.access_token;
        const page_name = input_data.page_name;
        const other_value = input_data.other_value;

        const response = await fetch(base_url + '?page_name=' + page_name + other_value, {
            method: 'GET',
            headers: {
                'Authorization': 'Bearer ' + access_token,
                'Content-Type': 'application/json',
            }
        });

        if (response.ok) {
            responseData = await response.json();
            handleResponseData();
            loadingElement.style.display = 'none';
            errorMessageElement.style.display = 'none';
            contentElement.style.display = 'block';
        } else {
            errorMessageElement.innerHTML = 'خطایی رخ داده است <br>' + response.status.toString() + '<br><br>' + '<button id="back_to_start_page" class="btn btn-food view-cart-button">بازگشت به صفحه ورود<i class="ri-arrow-left-line"></i></button>';
            errorMessageElement.style.display = 'block';
            throw new Error('Network response was not ok');
        }
    } catch (error) {
        loadingElement.style.display = 'none';
        errorMessageElement.style.display = 'block';
        console.error(error);
    }

    const backToStartPageButton = document.getElementById('back_to_start_page');
    if (backToStartPageButton) {
        backToStartPageButton.addEventListener('click', function() {
            window.Kodular.setWebViewString('back_to_start_page');
        });
    }
});

function handleResponseData() {
    if (responseData) {
        // document.getElementById('defult_address_title').innerHTML = responseData.defult_address.title + '<i class="ri-arrow-down-s-line"></i>';
    } else {
        console.log('No data available');
    }
}