import { athenaConfigOptions } from '../../global/athena-config.js';
import { handleHtml } from '../../global/helpers/athena-global-helper.js';

/**
 * Append products per page
 *
 * @param orders
 */
function appendProductsPerPage(orders) {
    if(athenaConfigOptions.productsPerPage.innerHTML === '') {
        athenaConfigOptions.productsPerPage.append(
            handleHtml(`
                <select class="products-per-page"></select>
            `, '.products-per-page')
        );

        let prodPerPage = document.querySelector('.products-per-page');
        orders.forEach(element => {
            const urlParams = encodeURIComponent(JSON.stringify(element.url_params));
            prodPerPage.append(
                handleHtml(`
                      <option class="products-per-page-option" value="${urlParams !== 'undefined' && urlParams}" ${element.selected ? `selected="${element.selected}"` : ''}>${element.option_label}</option>
                `, '.products-per-page-option')
            )
        });

        let prodPerPageEvent = new Event('products-per-page-ready');
        window.dispatchEvent(prodPerPageEvent);
    }
}

// Exporting all function
export {
    appendProductsPerPage
}
