import { fetchPriceRangeData } from '../../ajax/search/fetch-price-range-data.js';
/**
 * Change Price Function
 *
 * @param min
 * @param max
 */
function changePrice(min, max) {
    let windowUrlParams = window.location.href.split('?');
    let priceFilter = '';

    let urlParams = windowUrlParams[1].split('&');
    let formattedUrl = '';

    //Remove Price from URL if it contains Price
    if(windowUrlParams[1].includes('price')) {

        urlParams.forEach((urlParam, index) => {
            if(index === 0) {
                formattedUrl = `?${urlParam}`;
            }

            if(!urlParam.includes('price=') && index > 0 && !urlParam.includes('page=')) {
                formattedUrl += `&${urlParam}`;
            }
        });

        if(isNaN(min) || isNaN(max)) {
            return;
        }

        priceFilter = `${formattedUrl}&price=${min}-${max}`;
    } else {
        urlParams.forEach((urlParam, index) => {
            if(index === 0) {
                formattedUrl = `?${urlParam}`;
            }

            if(index > 0 && !urlParam.includes('page=')) {
                formattedUrl += `&${urlParam}`;
            }
        });

        if(isNaN(min) || isNaN(max)) {
            return;
        }

        priceFilter = `${formattedUrl}&price=${min}-${max}`;
    }

    fetchPriceRangeData(priceFilter);
}


export {
    changePrice
}