(function ($, window) {

    class SliderWidget {
        constructor() {
            this.to = 1000;
            this.from = 0;
            this.fromChange = 0;
            this.toChange = 0;
        }

        init(min = params.min, max = params.max, queryMinPrice = 0, queryMaxPrice = 0) {
            this.minPrice = min || params.min;
            this.maxPrice = max || params.max;

            const buttonApply = $('.price-filter-submit-js');
            const taxonomy = buttonApply.data('taxonomy');
            const currentCategory = buttonApply.data('category-type');

            buttonApply.hide();
            const slider = $(".js-range-slider");

            this.createSlider(slider, buttonApply);
            this.bindInputEvents(buttonApply, slider);
            this.bindApplyButtonClick(buttonApply, taxonomy, currentCategory, queryMinPrice, queryMaxPrice);
        }

        createSlider(slider, buttonApply) {
            slider.ionRangeSlider({
                from: this.minPrice,
                to: this.maxPrice,
                hide_min_max: true,
                hide_from_to: true,
                onStart: this.setInitialValues.bind(this),
                onChange: (data) => this.updateValues(data, buttonApply)
            });
        }

        setInitialValues(data) {
            const minFromUrl = prodigyGetUrlParam('price_min');
            const maxFromUrl = prodigyGetUrlParam('price_max');

            $('.min-js').val(minFromUrl !== null ? minFromUrl : this.minPrice);
            $('.max-js').val(maxFromUrl !== null ? maxFromUrl : this.maxPrice);
        }

        updateValues(data, buttonApply) {
            buttonApply.show();
            this.fromChange = data.from;
            this.toChange = data.to;
            $('.min-js').val(data.from);
            $('.max-js').val(data.to);
        }

        bindInputEvents(buttonApply, slider) {
            const inputMin = $(".min-js");
            const inputMax = $(".max-js");
            const sliderInstance = slider.data("ionRangeSlider");

            inputMin.on("change", () => this.handleInputChange(inputMin, 'min', buttonApply, sliderInstance));
            inputMax.on("change", () => this.handleInputChange(inputMax, 'max', buttonApply, sliderInstance));
        }

        handleInputChange(input, type, buttonApply, sliderInstance) {
            buttonApply.show();
            const inputValue = parseInt(input.val());

            if (this.isValidInputValue(inputValue, type)) {
                this[type === 'min' ? 'minPrice' : 'maxPrice'] = inputValue;
            } else {
                input.val(this[type === 'min' ? 'minPrice' : 'maxPrice']);
            }

            sliderInstance.update({
                [type]: input.val()
            });
        }

        isValidInputValue(inputValue, type) {
            return inputValue > (type === 'min' ? 0 : this.minPrice) && inputValue < this.maxPrice;
        }

        bindApplyButtonClick(buttonApply, taxonomy, currentCategory, queryMinPrice, queryMaxPrice) {
            buttonApply.click((e) => {
                e.preventDefault();
                this.applyFilters(queryMinPrice, queryMaxPrice, taxonomy, currentCategory);
            });
        }

        applyFilters(queryMinPrice, queryMaxPrice, taxonomy, currentCategory) {
            this.from = queryMinPrice || this.minPrice;
            this.to = queryMaxPrice || this.maxPrice;

            if (this.fromChange) this.from = this.fromChange;
            if (this.toChange) this.to = this.toChange;

            const newParams = [['price_min', this.from], ['price_max', this.to],];

            const pathRedirect = (params.is_shop_page === 'false') ? `${document.location.origin}/${params.shop_page}` : (taxonomy === currentCategory ? document.location.pathname : `${document.location.origin}/shop`);
            let newUrl = pathRedirect + prodigyInsertUrlParams(newParams);
			let price_query = '';
			if (this.to && this.from) {
				price_query = 'price_max=' + this.to + '&price_min=' + this.from;
				localStorage.setItem('price-range', price_query);
			}
            window.location.replace(newUrl);
        }

        removeParam(key, sourceURL) {
            const [baseURL, queryString] = sourceURL.split("?");
            if (!queryString) return baseURL;

            const params = queryString.split("&").filter(param => !param.startsWith(`${key}=`));
            return params.length ? `${baseURL}?${params.join("&")}` : baseURL;
        }
    }

    $(document).ready(function () {
        if ($('.price-filter-container-js').length) {
            const sliderWidget = new SliderWidget();
            sliderWidget.init(prodigyGetUrlParam('price_min'), prodigyGetUrlParam('price_max'));
            window.slider_widget = sliderWidget;
        }
    });
})(jQuery, window);
