/* Minification failed. Returning unminified contents.
(2,18): run-time error CSS1031: Expected selector, found '='
(2,18): run-time error CSS1025: Expected comma or open brace, found '='
(3,4): run-time error CSS1031: Expected selector, found '('
(3,4): run-time error CSS1025: Expected comma or open brace, found '('
(10,1): run-time error CSS1019: Unexpected token, found '$'
(10,2): run-time error CSS1019: Unexpected token, found '('
(10,3): run-time error CSS1019: Unexpected token, found ''#ranking-select, #day-select''
(10,33): run-time error CSS1019: Unexpected token, found ')'
(10,35): run-time error CSS1030: Expected identifier, found 'on('
(10,35): run-time error CSS1019: Unexpected token, found 'on('
(10,38): run-time error CSS1019: Unexpected token, found ''change''
(10,46): run-time error CSS1019: Unexpected token, found ','
(10,57): run-time error CSS1031: Expected selector, found '('
(10,57): run-time error CSS1025: Expected comma or open brace, found '('
(18,2): run-time error CSS1019: Unexpected token, found ')'
(20,1): run-time error CSS1019: Unexpected token, found '$'
(20,2): run-time error CSS1019: Unexpected token, found '('
(20,3): run-time error CSS1019: Unexpected token, found '".all-product-list"'
(20,22): run-time error CSS1019: Unexpected token, found ')'
(20,24): run-time error CSS1030: Expected identifier, found 'on('
(20,24): run-time error CSS1019: Unexpected token, found 'on('
(20,27): run-time error CSS1019: Unexpected token, found '"click"'
(20,34): run-time error CSS1019: Unexpected token, found ','
(20,45): run-time error CSS1031: Expected selector, found '('
(20,45): run-time error CSS1025: Expected comma or open brace, found '('
(25,2): run-time error CSS1019: Unexpected token, found ')'
(27,10): run-time error CSS1031: Expected selector, found 'getUrlParameter('
(27,10): run-time error CSS1025: Expected comma or open brace, found 'getUrlParameter('
(38,10): run-time error CSS1031: Expected selector, found 'changeUrlParam('
(38,10): run-time error CSS1025: Expected comma or open brace, found 'changeUrlParam('
(52,1): run-time error CSS1019: Unexpected token, found '$'
(52,20): run-time error CSS1031: Expected selector, found '='
(52,20): run-time error CSS1025: Expected comma or open brace, found '='
(291,1): run-time error CSS1019: Unexpected token, found '$'
(291,2): run-time error CSS1019: Unexpected token, found '('
(291,3): run-time error CSS1019: Unexpected token, found '".paging-container"'
(291,22): run-time error CSS1019: Unexpected token, found ')'
(291,24): run-time error CSS1030: Expected identifier, found 'ProductPaging('
(291,24): run-time error CSS1019: Unexpected token, found 'ProductPaging('
(291,38): run-time error CSS1019: Unexpected token, found '{'
(292,17): run-time error CSS1030: Expected identifier, found ' '
(292,18): run-time error CSS1031: Expected selector, found '"sayfa"'
(292,18): run-time error CSS1025: Expected comma or open brace, found '"sayfa"'
(299,15): run-time error CSS1031: Expected selector, found '='
(299,15): run-time error CSS1025: Expected comma or open brace, found '='
(313,10): run-time error CSS1031: Expected selector, found 'buildUrlFromChecked('
(313,10): run-time error CSS1025: Expected comma or open brace, found 'buildUrlFromChecked('
(343,10): run-time error CSS1031: Expected selector, found 'reloadWithCurrentSelections('
(343,10): run-time error CSS1025: Expected comma or open brace, found 'reloadWithCurrentSelections('
(349,10): run-time error CSS1031: Expected selector, found 'toggleSelectedFiltersVisibility('
(349,10): run-time error CSS1025: Expected comma or open brace, found 'toggleSelectedFiltersVisibility('
(357,10): run-time error CSS1031: Expected selector, found 'renderSelectedFiltersOnce('
(357,10): run-time error CSS1025: Expected comma or open brace, found 'renderSelectedFiltersOnce('
(373,1): run-time error CSS1019: Unexpected token, found '('
(373,11): run-time error CSS1031: Expected selector, found 'initFromUrl('
(373,11): run-time error CSS1025: Expected comma or open brace, found 'initFromUrl('
(385,2): run-time error CSS1019: Unexpected token, found ')'
(385,3): run-time error CSS1019: Unexpected token, found '('
(385,4): run-time error CSS1019: Unexpected token, found ')'
(387,1): run-time error CSS1019: Unexpected token, found '$'
(387,2): run-time error CSS1019: Unexpected token, found '('
(387,11): run-time error CSS1031: Expected selector, found ')'
(387,11): run-time error CSS1025: Expected comma or open brace, found ')'
(401,2): run-time error CSS1019: Unexpected token, found ')'
(403,1): run-time error CSS1019: Unexpected token, found '$'
(403,2): run-time error CSS1019: Unexpected token, found '('
(403,11): run-time error CSS1031: Expected selector, found ')'
(403,11): run-time error CSS1025: Expected comma or open brace, found ')'
(408,2): run-time error CSS1019: Unexpected token, found ')'
(410,1): run-time error CSS1019: Unexpected token, found '$'
(410,2): run-time error CSS1019: Unexpected token, found '('
(410,11): run-time error CSS1031: Expected selector, found ')'
(410,11): run-time error CSS1025: Expected comma or open brace, found ')'
(414,2): run-time error CSS1019: Unexpected token, found ')'
(416,14): run-time error CSS1031: Expected selector, found '='
(416,14): run-time error CSS1025: Expected comma or open brace, found '='
(417,14): run-time error CSS1031: Expected selector, found '='
(417,14): run-time error CSS1025: Expected comma or open brace, found '='
(418,4): run-time error CSS1031: Expected selector, found '('
(418,4): run-time error CSS1025: Expected comma or open brace, found '('
(421,1): run-time error CSS1019: Unexpected token, found '$'
(421,2): run-time error CSS1019: Unexpected token, found '('
(421,3): run-time error CSS1019: Unexpected token, found '".left-category"'
(421,19): run-time error CSS1019: Unexpected token, found ')'
(421,21): run-time error CSS1030: Expected identifier, found 'click('
(421,21): run-time error CSS1019: Unexpected token, found 'click('
(421,36): run-time error CSS1031: Expected selector, found '('
(421,36): run-time error CSS1025: Expected comma or open brace, found '('
(423,2): run-time error CSS1019: Unexpected token, found ')'
 */

let searchParams = new URLSearchParams(window.location.search);
if (searchParams.has("siralama") == true) {
    var SparamV = getUrlParameter("siralama").split('-');

    $("#ranking-select").val(SparamV[0]).change();
    $("#day-select").val(SparamV[1]).change();
}

$('#ranking-select, #day-select').on('change', function () {
    LoadingBar(true, "body", null);

    var value_fiyat = $('#ranking-select option:selected').val();
    var value_zaman = $('#day-select option:selected').val();
    var prm = value_fiyat + "-" + value_zaman;

    location.href = removeURLParameter(changeUrlParam("siralama", prm), "sayfa")
});

$(".all-product-list").on("click", function () {
    var value_fiyat = $('#ranking-select option:selected').val();
    var value_zaman = $('#day-select option:selected').val();
    var prm = value_fiyat + "-" + value_zaman + "-9999";
    location.href = removeURLParameter(changeUrlParam("siralama", prm), "sayfa")
});

function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}

function changeUrlParam(paramName, value) {
    let searchParams = new URLSearchParams(window.location.search);
    var currentUrl = window.location.href;
    if (searchParams.has(paramName) == false) {
        if (window.location.href.indexOf("?") > -1) {
            currentUrl = window.location.href + "&" + paramName + "=" + value;
        } else {
            currentUrl = window.location.href + "?" + paramName + "=" + value;;
        }
    }
    var url = new URL(currentUrl);
    url.searchParams.set(paramName, value);
    return url.href;
}
$.fn.ProductPaging = function (options) {
    this.each(function () {
        var element = $(this);
        var settings = $.extend({
            ViewItem: parseInt(element.attr("data-pagecount")),
            ActivPageIndex: 0,
            PageUrlName: "page",
            ViewNumber: 5,
            IsInput: false,
            PrevIcon: "<",
            NextIcon: ">"
        }, options);

        var ActivePageNumber = 1;
        if (settings.ActivPageIndex != 0) {
            ActivePageNumber = getUrlParameter2(settings.ActivPageIndex);
        }
        else {
            ActivePageNumber = parseInt(getUrlParameter(settings.PageUrlName));
        }
        if (isNaN(ActivePageNumber)) {
            ActivePageNumber = 1;
        }
        ActivePageNumber = parseInt(ActivePageNumber);

        var PageNumberCount = parseInt(settings.ViewItem);  //Math.ceil(settings.TotalItem / settings.ViewItem);
        settings.ViewNumber = settings.ViewNumber - (PageNumberCount > settings.ViewNumber ? 1 : 0);

        if (PageNumberCount > 1) {

            var navItem = $("<nav class=\"paging-nav\"></nav>");
            var ulItems = $("<ul class=\"pagination\"></ul>");

            if (ActivePageNumber != 1) {
                var liPrevious = $("<li class=\"pag-previous\"></li>");
                var liPreviousA = $("<a>" + settings.PrevIcon + "</a>");
                if (settings.ActivPageIndex != 0) {
                    liPreviousA.attr("href", ChaneUrl(settings.ActivPageIndex, (ActivePageNumber - 1)));
                }
                else {
                    liPreviousA.attr("href", ChangeUrlParam(settings.PageUrlName, (ActivePageNumber - 1)));
                }
                liPrevious.html(liPreviousA);
                ulItems.append(liPrevious);
            }

            if (PageNumberCount > settings.ViewNumber) {

                var halfViewNumber = Math.round(settings.ViewNumber / 2);
                var start = ActivePageNumber - halfViewNumber;
                var finsh = ActivePageNumber + halfViewNumber;

                if (start <= 0) {
                    start = 1;
                    finsh = ActivePageNumber <= halfViewNumber ? start + halfViewNumber : (start + settings.ViewNumber);
                }

                if (finsh > PageNumberCount) {
                    finsh = PageNumberCount;
                    start = (PageNumberCount - halfViewNumber) <= ActivePageNumber ? finsh - halfViewNumber : finsh - settings.ViewNumber;
                }

                if (PageNumberCount > settings.ViewNumber) {
                    if (ActivePageNumber > (halfViewNumber + 2)) {
                        var finItem = $("<li class=\"pg-page\"></li>");
                        var finItemA = $("<a data-page=\"1\">1</a>");
                        if (settings.ActivPageIndex != 0) {
                            finItemA.attr("href", ChaneUrl(settings.ActivPageIndex, finItemA.attr("data-page")));
                        }
                        else {
                            finItemA.attr("href", ChangeUrlParam(settings.PageUrlName, finItemA.attr("data-page")));
                        }
                        finItem.html(finItemA);
                        ulItems.append(finItem);
                        var emty = $("<li>...</li>");
                        ulItems.append(emty);
                    }
                }

                for (var i = 0; i < PageNumberCount; i++) {
                    var index = i + 1;
                    if (index >= start && index <= finsh) {
                        var liActive = "";
                        if (index == ActivePageNumber) {
                            liActive = "active";
                        }
                        var liItem = $("<li class=\"pg-page " + liActive + "\"></li>");
                        var liItemA = $("<a data-page=\"" + index + "\">" + index + "</a>");

                        if (index != ActivePageNumber) {
                            if (settings.ActivPageIndex != 0) {
                                liItemA.attr("href", ChaneUrl(settings.ActivPageIndex, liItemA.attr("data-page")));
                            }
                            else {
                                liItemA.attr("href", ChangeUrlParam(settings.PageUrlName, liItemA.attr("data-page")));
                            }
                        }
                        liItem.html(liItemA);
                        ulItems.append(liItem);
                    }
                }

                if (PageNumberCount > settings.ViewNumber) {
                    if ((ActivePageNumber + halfViewNumber + 1) < PageNumberCount) {
                        var emty = $("<li>...</li>");
                        ulItems.append(emty);
                        var finItem = $("<li class=\"pg-page\"></li>");
                        var finItemA = $("<a data-page=\"" + PageNumberCount + "\">" + PageNumberCount + "</a>");
                        if (settings.ActivPageIndex != 0) {
                            finItemA.attr("href", ChaneUrl(settings.ActivPageIndex, finItemA.attr("data-page")));
                        }
                        else {
                            finItemA.attr("href", ChangeUrlParam(settings.PageUrlName, finItemA.attr("data-page")));
                        }
                        finItem.html(finItemA);
                        ulItems.append(finItem);
                    }
                }
            }
            else {
                for (var i = 0; i < PageNumberCount; i++) {
                    var index = i + 1;
                    var liActive = "";
                    if (index == ActivePageNumber) {
                        liActive = "active";
                    }
                    var liItem = $("<li class=\"pg-page " + liActive + "\"></li>");
                    var liItemA = $("<a data-page=\"" + index + "\">" + index + "</a>");

                    if (index != ActivePageNumber) {
                        if (settings.ActivPageIndex != 0) {
                            liItemA.attr("href", ChaneUrl(settings.ActivPageIndex, liItemA.attr("data-page")));
                        }
                        else {
                            liItemA.attr("href", ChangeUrlParam(settings.PageUrlName, liItemA.attr("data-page")));
                        }
                    }
                    liItem.html(liItemA);
                    ulItems.append(liItem);
                }
            }

            if (ActivePageNumber != PageNumberCount) {
                var liNext = $("<li class=\"pag-next\"></li>");
                var liNextA = $("<a>" + settings.NextIcon + "</a>");
                if (settings.ActivPageIndex != 0) {
                    liNextA.attr("href", ChaneUrl(settings.ActivPageIndex, (ActivePageNumber + 1)));
                }
                else {
                    liNextA.attr("href", ChangeUrlParam(settings.PageUrlName, (ActivePageNumber + 1)));
                }
                liNext.html(liNextA);
                ulItems.append(liNext);
            }

            navItem.append(ulItems);

            if (settings.IsInput == true) {
                if (PageNumberCount > settings.ViewNumber) {

                    var input = $("<input class=\"pg-in-number\" type=\"text\"></input>");
                    input.val(ActivePageNumber);
                    var timerid;
                    input.on('keypress', function (key) {
                        if (key.charCode < 48 || key.charCode > 57) return false;
                    }).on('input', function () {
                        var tsd = this;
                        var vall = parseInt($(this).val());
                        if (vall != "") {
                            if (vall >= 1 && vall <= PageNumberCount) {
                                clearTimeout(timerid);
                                timerid = setTimeout(function () {
                                    if (settings.ActivPageIndex != 0) {
                                        window.location.href = ChaneUrl(settings.ActivPageIndex, vall);
                                    }
                                    else {
                                        window.location.href = ChangeUrlParam(settings.PageUrlName, vall);
                                    }
                                    //settings.onClick.call(tsd, vall, "page");

                                }, 1000);
                            }
                        }
                    });
                    navItem.append(input);
                }
            }

            element.html(navItem);
        }

        function getUrlParameter(sParam) {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.split('&');
            for (var i = 0; i < sURLVariables.length; i++) {
                var sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] == sParam) {
                    return sParameterName[1];
                }
            }
        }

        function getUrlParameter2(index) {
            var sPageURL = window.location.href.split('//');
            var sParameterName = sPageURL[1].split('/');
            return sParameterName[index]
        }

        function ChaneUrl(replaceindex, replacevalue) {
            var sPageURL = window.location.href.split('//');
            var sParameterName = sPageURL[1].split('/');
            var url = "";
            for (var i = 1; i < sParameterName.length; i++) {
                if (replaceindex == (i)) {
                    url += replacevalue + "/";
                }
                else {
                    url += sParameterName[i] + (i != (sParameterName.length - 1) ? "/" : "");
                }
            }
            return "/" + url;
        }

        function ChangeUrlParam(paramName, value) {
            let searchParams = new URLSearchParams(window.location.search);
            var currentUrl = window.location.href;
            if (searchParams.has(paramName) == false) {
                if (window.location.href.indexOf("?") > -1) {
                    currentUrl = window.location.href + "&" + paramName + "=" + value;
                } else {
                    currentUrl = window.location.href + "?" + paramName + "=" + value;;
                }
            }
            var url = new URL(currentUrl);
            url.searchParams.set(paramName, value);
            return url.href;
        }
    });
}
$(".paging-container").ProductPaging({
    PageUrlName: "sayfa",
    ViewNumber: 5,
    IsInput: false,
    PrevIcon: "<i class=\"fas fa-chevron-left\"></i>",
    NextIcon: "<i class=\"fas fa-chevron-right\"></i>"
});

let isSyncing = false;

//function buildUrlFromChecked() {
//    const params = new URLSearchParams();
//    $(".listInputUrl:checked").each(function () {
//        const name = $(this).data("param-name");
//        const value = $(this).data("param-value");
//        if (name && value !== undefined) params.set(name, value);
//    });
//    const base = window.location.pathname;
//    const qs = params.toString();
//    return qs ? base + "?" + qs : base;
//}

function buildUrlFromChecked() {
    // Mevcut query paramlarını al
    const current = new URLSearchParams(window.location.search);

    // Yeni param seti (q ve qt varsa koruyacağız)
    const params = new URLSearchParams();

    // q ve qt'yi olduğu gibi sakla
    ["q", "qt"].forEach((k) => {
        if (current.has(k)) params.set(k, current.get(k));
    });

    // Seçili input'lardan gelen paramları ekle (q/qt'yi ezme)
    $(".listInputUrl:checked").each(function () {
        const name = $(this).data("param-name");
        const value = $(this).data("param-value");

        if (!name) return;
        if (name === "q" || name === "qt") return; // q/qt'ye dokunma
        if (value !== undefined && value !== null && value !== "") {
            params.set(name, value);
        }
    });

    const base = window.location.pathname;
    const qs = params.toString();
    return qs ? `${base}?${qs}` : base;
}


function reloadWithCurrentSelections() {
    LoadingBar(true);
    const newUrl = buildUrlFromChecked();    
    window.location = newUrl; // sayfayı yenile
}

function toggleSelectedFiltersVisibility() {
    if ($(".listInputUrl:checked").length > 0) {
        $(".selectedFilters").show();
    } else {
        $(".selectedFilters").hide();
    }
}

function renderSelectedFiltersOnce() {
    const $list = $(".selectedFiltersList");
    if (!$list.length) return;
    $list.empty();
    $(".listInputUrl:checked").each(function () {
        const text = $(this).closest("label").text().trim();
        const id = $(this).attr("id");
        const $li = $("<li>")
            .append($("<span>").text(text))
            .append($("<a>", { href: "#", "data-id": id })
                .html('Sil'));
        $list.append($li);
    });
    toggleSelectedFiltersVisibility();
}

(function initFromUrl() {
    const urlParams = new URLSearchParams(window.location.search);
    $(".listInputUrl").each(function () {
        const name = $(this).data("param-name");
        const value = $(this).data("param-value");
        if (urlParams.has(name) && urlParams.get(name) === String(value)) {
            $(this).prop("checked", true);
        } else {
            $(this).prop("checked", false);
        }
    });
    renderSelectedFiltersOnce();
})();

$(document).on("change", ".listInputUrl", function () {
    if (isSyncing) return;
    isSyncing = true;

    const $this = $(this);
    const group = $this.data("param-name");

    if ($this.is(":checked")) {
        $(".listInputUrl").filter(function () {
            return $(this).data("param-name") === group && this !== $this[0];
        }).prop("checked", false);
    }

    reloadWithCurrentSelections();
});

$(document).on("click", ".selectedFiltersList a", function (e) {
    e.preventDefault();
    const id = $(this).data("id");
    $("#" + id).prop("checked", false);
    reloadWithCurrentSelections();
});

$(document).on("click", ".selectedFiltersLink", function (e) {
    LoadingBar(true);
    e.preventDefault();   
    window.location = window.location.pathname;
});

var kategori = $("#mapName").attr("data-main");
let checkbox = document.querySelector(`.listInput[data-address="${kategori}"]`);
if (checkbox) {
    checkbox.checked = true;
}
$(".left-category").click(function () {    
    window.location.href = "/" + $(this).attr("data-address");
});
