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

/**
 * Handle Class to append for pagination
 *
 * @param type
 * @returns {string}
 */
function handleClass(type) {
    let classToAppend = 'pager-item-middle';

    if(type) {
        switch (type) {
            case 'first':
                classToAppend = 'pager-item-first';
                break;
            case 'previous':
                classToAppend = 'pager-item-previous';
                break;
            case 'next':
                classToAppend = 'pager-item-next';
                break;
            case 'last':
                classToAppend = 'pager-item-last';
                break;
            default:
                classToAppend = 'pager-item-middle';
                break;
        }
    }

    return classToAppend;
}

/**
 * Append Pager
 *
 * @param pager
 */
function appendPager(pager) {
    if(pager.length && athenaConfig.pagerStatus) {
        athenaConfigOptions.pagerContainer.innerHTML = '';

        pager.forEach((item, key) => {
            const urlParams = encodeURIComponent(JSON.stringify(item.url_params));
            const classToAppend = handleClass(item?.type);

            if(urlParams !== 'undefined' && urlParams) {
                athenaConfigOptions.pagerContainer.append(
                    handleHtml(`
                        <span class="pager-item ${classToAppend}"  data-value="${urlParams !== 'undefined' && urlParams}">
                            ${item.option_label}
                        </span>
                    `, `.pager-item`)
                );
            } else {
                athenaConfigOptions.pagerContainer.append(
                    handleHtml(`
                        <span class="pager-item ${classToAppend} pager-item-active">
                            ${item.option_label}
                        </span>
                    `, `.pager-item`)
                );
            }
        });
    } else if(pager.length === 0) {
        athenaConfigOptions.pagerContainer.innerHTML = '';
    }
}

//Exporting all Functions
export {
    appendPager
}