/**
* 2007-2025 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2025 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/


async function callPrestasyncAjax(url, getDataParam = [], type = 'GET', postData = [], successCallBackFunction = ()=>{}, errorCallBackFunction = ()=>{}){



    if(type == 'GET'){
        let queryString = Object.keys(getDataParam).map((key) => {
            return encodeURIComponent(key) + '=' + encodeURIComponent(getDataParam[key])
        }).join('&');

        if(queryString.length>0){
            url = url + '?' + queryString;
        }
    }


    let xhttp = new XMLHttpRequest();
    xhttp.open(type, url, true);
    xhttp.onreadystatechange = function()
    {
        if (this.readyState == XMLHttpRequest.DONE && this.status == 200)
        {
            let responseJsonObj = JSON.parse(this.responseText);

            if (typeof successCallBackFunction === 'function') {
                successCallBackFunction(responseJsonObj);
            } else {
                console.error('Callback function invalide for callKanbanInterface');
            }
        }else if (this.readyState == XMLHttpRequest.DONE){
            let errorMsg = '' ;
            var statusErrorMap = {
                '404' : "Not found",
                '400' : "Server understood the request, but request content was invalid.",
                '401' : "Unauthorized access.",
                '403' : "Forbidden resource can't be accessed.",
                '500' : "Internal server error.",
                '503' : "Service unavailable."
            };
            if (this.status) {
                errorMsg = statusErrorMap[this.status];
                if(!errorMsg){
                    errorMsg = "Unknown Error \n.";
                }
            }

            if (typeof errorCallBackFunction === 'function') {
                errorCallBackFunction(errorMsg);
            } else {
                console.error('Error Callback function invalide for callKanbanInterface');
            }
        }
    };

    xhttp.send( postData );
    return xhttp;
}

document.addEventListener("DOMContentLoaded", function() {
    if (typeof prestashop !== 'undefined') {

        let invoiceBoxLink = document.getElementById('prestasync-order-invoice-box-link');
        if(invoiceBoxLink){
            let url = invoiceBoxLink.getAttribute('data-invoices-controller');
            if(url){
                callPrestasyncAjax(url, {
                    order: invoiceBoxLink.getAttribute('data-order-id'),
                    action: 'getInvoiceFromOrderId',
                    ajax : 1
                }, 'GET', [], (resData)=>{
                    if(parseInt(resData.status) > 0){
                        let invoiceLink = document.getElementById('prestasync-order-invoice-link');
                        if(invoiceLink){
                            invoiceBoxLink.classList.remove('--no-invoice');
                            invoiceLink.href = `${url}?ajax=1&action=download&f=${resData.data.invoiceId}`;
                        }
                    }
                });
            }
        }
    }
});