"use strict";
function r(e, t) {
    for (var n = 0; n < t.length; n++) {
        var i = t[n];
        i.enumerable = i.enumerable || !1,
        i.configurable = !0,
        "value" in i && (i.writable = !0),
        Object.defineProperty(e, i.key, i)
    }
}
function i(e, t) {
    var n = document.querySelectorAll("." + e);
    0 < n.length && n.forEach(function (e) {
        e.addEventListener("click", function (e) {
            e.preventDefault(),
            this.classList.toggle(t)
        })
    })
}
var s = function () {
    var e = document.querySelector(".preloader");
    e && e.classList.add("animation-start")
}, c = function () {
    var e = document.querySelector(".preloader");
    e && e.classList.add("animation-end"),
    setTimeout(function () {
        e && e.classList.add("preloader-hide")
    }, 500)
}, o = function () {
    function i(e, t, n) {
		var el=e;
        switch (function (e, t) {
            if (!(e instanceof t))
                throw new TypeError("Cannot call a class as a function")
            }
                (this, i),this.ed_min = e.querySelector("#ed_min"), this.type = e.getAttribute("data-item-type"), this.price = e.getAttribute("data-item-price"), this.json = e.getAttribute("data-item-json") ? JSON.parse(e.getAttribute("data-item-json")) : [], this.inp = e.querySelector(".calculator-num"), this.unitPriceBlock = e.querySelector(".item-content_info-calculator__price .price_num"), n) {
            case "click":
                this.btn = t.classList.contains("input-btn__minus") ? "minus" : "plus",
                this.eventClick(e);
                break;
			case "keyup":
			console.log(event.keyCode);
			if ( event.keyCode == 40 ||event.keyCode == 38 ||event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || (event.keyCode == 65 && event.ctrlKey === true)||(event.keyCode > 47 && event.keyCode < 58) || (event.keyCode > 95 && event.keyCode < 106 ) /* ||
                 // Разрешаем: home, end, влево, вправо
                (event.keyCode >= 35 && event.keyCode <= 39) */) {
                 this.eventBlur(e);
    } else {
    //  if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
        event.preventDefault();
    //  }
    }
                
             break;
            case "blur":
               // this.eventBlur();
                break;
            case "select":
                this.optionPrice = t.getAttribute("data-option-price") ? t.getAttribute("data-option-price") : this.price,
                this.eventSelect(e)
            }
			
    }
    return function (e, t, n) {
        t && r(e.prototype, t),
        n && r(e, n)
    }
    (i, [{
                key: "eventSelect",
                value: function (e) {
                    this.price = this.optionPrice,
                    e.setAttribute("data-item-price", this.optionPrice),
                    this.getCurrPrice(0 == this.type ? 3 : 0,e)
                }
            }, {
                key: "eventBlur",
                value: function (el) {
                   // this.getCurrPrice(0 == this.type ? 3 : 0)
				   //console.log(el);
				   0 == this.type ? this.setValueInput(this.inp.min, parseFloat(this.inp.step), 2,el) : this.setValueInput(this.inp.min, 1, 0,el)
                }
            }, {
                key: "eventClick",
                value: function (el) {
                    0 == this.type ? this.setValue(this.btn, this.inp.min, parseFloat(this.inp.step), 2,el) : this.setValue(this.btn, this.inp.min, 1, 0,el)
                }
            }, {
                key: "getCurrUnit",
                value: function (e) {
                    var t;
                    t = parseFloat(this.inp.value).toFixed(2),
                    this.inp.value = parseFloat(t).toFixed(e)
                }
            },{
                key: "setValueInput",
                value: function (t, n, i,el) {
                    var r = 0;
					console.log(parseFloat(this.inp.value)+'/'+parseFloat(t));
                    if (parseFloat(this.inp.value) < parseFloat(t)){
						 $(el).find("#fakeinput").css("border-color","red");
						 $(el).find(".ed_min").show();
                         $(el).find(".item-content_info-calculator__submit").prop("disabled",true);
                         return !1;
					}else{
						$(el).find(".item-content_info-calculator__submit").prop("disabled",false);
					    $(el).find("#fakeinput").css("border-color","#CCC");
                        $(el).find(".ed_min").hide();						 
					}
                     //if (this.inp.value !==this.inp.step){
						this.inp.value=((Math.round(this.inp.value / this.inp.step)) * this.inp.step);
						this.getCurrPrice(i,el)
                        
				//	 }							
					
                    //r = "minus" === e ? 1 * this.inp.value - n : 1 * this.inp.value + n,
                    //this.inp.value = parseFloat(r).toFixed(i),
                    
                }
            }, {
                key: "setValue",
                value: function (e, t, n, i,el) {
                    var r = 0;
                    if ("minus" === e && this.inp.value <= t){
						$(el).find(".ed_min").show(500);
                        setTimeout(function() { $(el).find(".ed_min").hide(500); }, 2000);
                        return !1;
					}
                    r = "minus" === e ? 1 * this.inp.value - n : 1 * this.inp.value + n,
                    this.inp.value = parseFloat(r).toFixed(i),
                    this.getCurrPrice(i,el)
                }
            }, {
                key: "getCurrPrice",
                value: function (e,el) {
                    this.getCurrUnit(e);
                    var t = this.inp.value,
                    n = this.price;
					if (el.querySelector('#price')){
					var pr = el.querySelector('#price');
					
					var ds = el.querySelector('#discount');
					pr.classList.remove("old_price");
					if (ds) {ds.style.display = "none";}
                    this.json.forEach(function (e) {
						t >= parseFloat(e.quantity) && (n = e.price)
						if (t >= parseFloat(e.quantity)) {
							pr.classList.add("old_price");
							if (ds) {ds.style.display = "inline-block";}
							
							var arr_pr=pr.textContent.split(' ');
							console.log(e);
							console.log(el);
							if (ds) {ds.textContent = e.price+' '+arr_pr.pop();}
						}
						
                    }),
                    this.unitPriceBlock.textContent = (n * t).toFixed(2)
					}
                }
            }
        ]),
    i
}
(), a = function (e) {
    var t = document.querySelector("select#npWarehouse");
    if (t) {
        var n = t.parentNode.querySelector(".select-selected"),
        i = t.parentNode.querySelector(".select-items");
        n || i ? (t.options.length = 0, t.options[0] = new Option("Выберите отделение", "0"), n.remove(), i.remove(), e.forEach(function (e) {
                t.options[t.options.length] = new Option(e)
            })) : e.forEach(function (e) {
            t.options[t.options.length] = new Option(e)
        }),
        l(".checkout__select")
    }
}, l = function (e) {
    var n,
    i,
    r,
    s,
    t = document.querySelectorAll(e);
    function c(e) {
        var t,
        n,
        i,
        r = [];
        for (t = document.getElementsByClassName("select-items"), n = document.getElementsByClassName("select-selected"), i = 0; i < n.length; i++)
            e == n[i] ? r.push(i) : n[i].classList.remove("select-arrow-active");
        for (i = 0; i < t.length; i++)
            r.indexOf(i) && t[i].classList.add("select-hide")
    }
    0 < t.length && t.forEach(function (e) {
        var t = e.querySelector("select");
        for ((i = document.createElement("DIV")).setAttribute("class", "select-selected"), i.innerHTML = t.options[t.selectedIndex].innerHTML, e.appendChild(i), (r = document.createElement("DIV")).setAttribute("class", "select-items select-hide"), n = 1; n < t.length; n++)
            (s = document.createElement("DIV")).innerHTML = t.options[n].innerHTML, s.addEventListener("click", function (e) {
                var t,
                n,
                i,
                r,
                s;
                for (r = this.parentNode.parentNode.getElementsByTagName("select")[0], s = this.parentNode.previousSibling, n = 0; n < r.length; n++)
                    if (r.options[n].innerHTML == this.innerHTML) {
                        for (r.selectedIndex = n, s.innerHTML = this.innerHTML, t = this.parentNode.getElementsByClassName("same-as-selected"), i = 0; i < t.length; i++)
                            t[i].removeAttribute("class");
                        this.setAttribute("class", "same-as-selected");
                        break
                    }
                s.click()
            }), r.appendChild(s);
        e.appendChild(r),
        i.addEventListener("click", function (e) {
            e.stopPropagation(),
            c(this),
            this.nextSibling.classList.toggle("select-hide"),
            this.classList.toggle("select-arrow-active")
        })
    }),
    document.addEventListener("click", c)
}, u = function (e, t) {
    var n = e.getAttribute("id");
    e.checked && t.forEach(function (e) {
        return e.getAttribute("data-checkout-type") === n ? e.classList.add("opened") : e.classList.remove("opened")
    })
};
window.onload = function () {
    s(),
    setTimeout(function () {
        c()
    }, 2e3),
    $("#newItemsSlider .slider__wrap").slick({
        infinite: !1,
        draggable: !1,
        prevArrow: $("#newItemsSlider .slider__arrow-prev"),
        nextArrow: $("#newItemsSlider .slider__arrow-next"),
        slidesToShow: 5,
        slidesToScroll: 1,
        useTransform: !0,
        cssEase: "cubic-bezier(0.645, 0.045, 0.355, 1.000)",
        responsive: [{
                breakpoint: 1401,
                settings: {
                    slidesToShow: 4
                }
            }, {
                breakpoint: 1250,
                settings: {
                    slidesToShow: 3
                }
            }, {
                breakpoint: 641,
                settings: {
                    slidesToShow: 2
                }
            }
        ]
    }),
    $("#popularSlider .slider__wrap").slick({
        infinite: !1,
        draggable: !1,
        prevArrow: $("#popularSlider .slider__arrow-prev"),
        nextArrow: $("#popularSlider .slider__arrow-next"),
        slidesToShow: 5,
        slidesToScroll: 2,
        useTransform: !0,
        cssEase: "cubic-bezier(0.645, 0.045, 0.355, 1.000)",
        responsive: [{
                breakpoint: 1401,
                settings: {
                    slidesToShow: 4
                }
            }, {
                breakpoint: 1250,
                settings: {
                    slidesToShow: 3,
                    slidesToScroll: 1
                }
            }, {
                breakpoint: 641,
                settings: {
                    slidesToShow: 2
                }
            }
        ]
    }),
    $("#itemCartSlider").slick({
        arrows: !1,
        slidesToShow: 1,
        slidesToScroll: 1,
        useTransform: !0,
        draggable: !1,
        cssEase: "cubic-bezier(0.645, 0.045, 0.355, 1.000)"
    }),
    //i("one-click__btn", "one-click__btn-hide"),
    i("drop__btn", "drop__btn_show"),
    i("btn-like", "liked"),
    i("custom-select", "show"),
    i("filter__burger", "opened"),
    i("bag__link", "bag__link-opened"),
    d("#burgerOpen", "#burgerClose", ".page", "burger-opened"),
    function (e, t, n, i, r) {
        var s = document.querySelectorAll("#" + e + " ." + t);
        0 < s.length && s.forEach(function (e) {
            e.addEventListener("click", function () {
                var e = this.getAttribute("data-" + r);
                s.forEach(function (e) {
                    e.classList.contains(i) && e.classList.remove(i)
                }),
                this.classList.add(i),
                $("#" + n).slick("slickGoTo", e)
            })
        })
    }
    ("itemPreview", "preview__item", "itemCartSlider", "preview__item-active", "slide"),
    function (e) {
        var n,
        i,
        r,
        s,
        c,
        t = document.querySelectorAll(e);
        function o(e) {
            var t,
            n,
            i,
            r = [];
            for (t = document.getElementsByClassName("select-items"), n = document.getElementsByClassName("select-selected"), i = 0; i < n.length; i++)
                e == n[i] ? r.push(i) : n[i].classList.remove("select-arrow-active");
            for (i = 0; i < t.length; i++)
                r.indexOf(i) && t[i].classList.add("select-hide")
        }
        0 < t.length && t.forEach(function (e) {

            var t = e.querySelector("select");
            for ((i = document.createElement("DIV")).setAttribute("class", "select-selected"), i.innerHTML = t.options[t.selectedIndex].innerHTML, e.appendChild(i), (r = document.createElement("DIV")).setAttribute("class", "select-items select-hide"), n = 1; n < t.length; n++)
                (s = document.createElement("DIV")).innerHTML = t.options[n].innerHTML+(t[n].getAttribute("data-item-quantity")>0 ? "":"<span class='emtyq'>НЕТ В НАЛИЧИИ</span>"), c = t[n].getAttribute("data-item-price"), s.setAttribute("data-option-price", c),c = t[n].getAttribute("data-item-quantity"),console.log(c), s.setAttribute("data-option-quantity", c),c = t[n].getAttribute("data-item-image"), s.setAttribute("data-option-image", c), s.addEventListener("click", function (e) {
                    var t,
                    n,
                    i,
                    r,
                    s;
                    for (r = this.parentNode.parentNode.getElementsByTagName("select")[0], s = this.parentNode.previousSibling, n = 0; n < r.length; n++)
                        if (r.options[n].innerHTML == this.innerHTML) {
                            for (r.selectedIndex = n, s.innerHTML = this.innerHTML, t = this.parentNode.getElementsByClassName("same-as-selected"), i = 0; i < t.length; i++)
                                t[i].removeAttribute("class");
                            this.setAttribute("class", "same-as-selected");
							this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("img")[0].src=this.getAttribute("data-option-image");
							$('.slick-current>img').attr("src",this.getAttribute("data-option-image"));
							//im.src=this.getAttribute("data-option-image");
							//console.log();
                            break
                        }
                    s.click()
                }), r.appendChild(s);
            e.appendChild(r),
            i.addEventListener("click", function (e) {
                e.stopPropagation(),
                o(this),
                this.nextSibling.classList.toggle("select-hide"),
                this.classList.toggle("select-arrow-active")
            })
        }),
        document.addEventListener("click", o)
    }

    (".pack-select"),
    function (e) {
        var t = document.querySelectorAll(e);
        0 < t.length && t.forEach(function (t) {
            i("sort__btn", "sort__btn-opened");
            var e = t.querySelector(".sort__dropdown").querySelectorAll(".sort__dropdown-item"),
            n = t.querySelector(".sort__btn-tx");
            window.addEventListener("click", function (e) {
                !t.contains(e.target) && t.querySelector(".sort__btn").classList.contains("sort__btn-opened") && t.querySelector(".sort__btn").classList.remove("sort__btn-opened")
            }),
            0 < e.length && e.forEach(function (e) {
                e.hasAttribute("active") && (n.innerText = e.innerText),
                e.addEventListener("click", function (e) {
                    e.preventDefault(),
                    window.location = this.getAttribute("data-href")
                })
            })
        })
    }
    (".sort"),
    function (e) {
        var t = document.querySelector(e);
        if (t) {
            var n = t.querySelector(".filter__btn"),
            i = t.querySelector(".dropdown__cls");
            n.addEventListener("click", function (e) {
                e.preventDefault(),
                this.classList.toggle("filter__btn-opened")
            }),
            i.addEventListener("click", function (e) {
                e.preventDefault(),
                n.classList.contains("filter__btn-opened") && n.classList.remove("filter__btn-opened")
            }),
            window.addEventListener("click", function (e) {
                !t.contains(e.target) && n.classList.contains("filter__btn-opened") && n.classList.remove("filter__btn-opened")
            })
        }
    }
    ("#categoryFilter"),
    function (e) {
        var t = document.querySelector(e);
        if (t) {
            var n = t.querySelector(".search__select-btn"),
            i = n.querySelector(".search__select-btn_tx"),
            r = t.querySelector(".search__dropdown"),
            s = t.querySelector('input[name="category_id"]'),
            c = document.querySelector('#content input[name="sub_category"]'),
            o = r.querySelectorAll(".search__option");
            o && o.forEach(function (e) {
                e.hasAttribute("selected") && (i.textContent = e.textContent, s.value = parseFloat(e.getAttribute("data-value"))),
                e.addEventListener("click", function () {
                    if (n.classList.contains("search__select-btn-active")) {
                        var e = this.textContent,
                        t = parseFloat(this.getAttribute("data-value"));
                        n.classList.remove("search__select-btn-active"),
                        i.textContent = e,
                        0 == t ? (c.disabled = !0, c.checked = !1) : c.disabled = !1,
                        s.value = t
                    }
                })
            }),
            n.addEventListener("click", function (e) {
                e.preventDefault(),
                this.classList.toggle("search__select-btn-active")
            }),
            window.addEventListener("click", function (e) {
                !t.contains(e.target) && t.querySelector(".search__select-btn").classList.contains("search__select-btn-active") && t.querySelector(".search__select-btn").classList.remove("search__select-btn-active")
            })
        }
    }
    ("#searchSelect"),
    function (e) {
        if (e = document.querySelector(e)) {
            var t = e.querySelectorAll(".rating__item"),
            i = e.querySelectorAll("input[name='rating']");
            t && t.forEach(function (e) {
                e.addEventListener("click", function (e) {
                    e.preventDefault();
                    var t = this.querySelector("input");
                    t.setAttribute("checked", !0);
                    var n = t.value;
                    i.forEach(function (e) {
                        e.value <= n ? e.parentNode.querySelector(".rating__icon").classList.add("rating__icon-checked") : e.parentNode.querySelector(".rating__icon").classList.remove("rating__icon-checked")
                    })
                })
            })
        }
    }
    (".feedback__rating"),
    function (e) {
        var t = document.querySelector(e);
        if (t) {
            var n = t.querySelectorAll(".bag__link");
            0 < n.length && n.forEach(function (e) {
                e.addEventListener("click", function (e) {
                    e.preventDefault(),
                    this.classList.add("bag__link-opened")
                })
            });
            var i = t.querySelectorAll(".bag__cls");
            0 < i.length && i.forEach(function (e) {
                e.addEventListener("click", function () {
                    var e = this.parentNode.parentNode.parentNode.querySelector(".bag__link");
                    e.classList.contains("bag__link-opened") && e.classList.remove("bag__link-opened")
                })
            }),
            window.addEventListener("click", function (e) {
                !t.contains(e.target) && t.querySelector(".bag__link").classList.contains("bag__link-opened") && t.querySelector(".bag__link").classList.remove("bag__link-opened")
            })
        }
    }
    ("#cart"),
    function (e) {
        var t = document.querySelectorAll(e);
        0 < t.length && t.forEach(function (e) {
            if (e.querySelector(".nav__dropdown")) {
                var t = !1;
                e.addEventListener("click", function (e) {
                    window.innerWidth <= 990 && 1 != t && (e.preventDefault(), t = !0)
                })
            }
        })
    }
    (".nav__item1"),
    function () {
        var e = document.querySelectorAll('input[name="dostavka"]'),
        t = document.querySelectorAll(".checkout__info-block");
        0 < e.length && e.forEach(function (e) {
            u(e, t),
            e.addEventListener("click", function (e) {
                u(e.target, t)
            })
        })
    }
    ();
    var t,
    e = document.querySelector("#npCity");
    e && e.addEventListener("blur", function (e) {
        e.target.value !== t && "" !== e.target.value && (function (e) {
            var t = {
                modelName: "AddressGeneral",
                calledMethod: "getWarehouses",
                methodProperties: {
                    Language: "ru",
                    CityName: e
                },
                apiKey: "08c7a950e7cacafdc09b8929fb15aaf5"
            };
            $.ajax({
                url: "https://api.novaposhta.ua/v2.0/json/Address/searchSettlements/",
                type: "post",
                data: JSON.stringify(t),
                dataType: "json",
                success: function (e) {
                    var t = e.data.map(function (e) {
                            return e.Description
                        });
                    a(t)
                },
                error: function (e, t, n) {
                    alert(n + "\r\n" + e.statusText + "\r\n" + e.responseText)
                }
            })
        }
            (e.target.value), t = e.target.value)
    });
    var n = document.querySelectorAll(".calculated-item");
	console.log(n.length);
    0 < n.length && n.forEach(function (t) {
        var e = t.querySelectorAll(".calculator-btn");
		 var el = t.querySelector(".calculator-num");
        0 < e.length && e.forEach(function (e) {
            return e.addEventListener("click", function (e) {
                e.preventDefault(),
                new o(t, e.target, "click")
            })
        }),
       
		el && el.addEventListener("blur", function (e) {
            new o(t, e.target, "blur")
        });
		el && el.addEventListener("keyup", function (e) {
            new o(t, e.target, "keyup")
        });
        var n = t.querySelectorAll(".pack-select .select-items div");
        0 < n.length && n.forEach(function (e) {
            e.addEventListener("click", function (e) {
                new o(t, e.target, "select")
            })
        })
    })
};
var d = function (e, t, n, i) {
    var r = document.querySelector(e),
    s = document.querySelector(t),
    c = document.querySelector(n);
    r && r.addEventListener("click", function (e) {
        e.preventDefault(),
        c.classList.add(i)
    }),
    s && s.addEventListener("click", function (e) {
        e.preventDefault(),
        c.classList.contains(i) && c.classList.remove(i)
    })
};