var Wick = Wick || {};
Wick.TextClamp = (function () {
    var rangeListCardDescr = $('.range-list__next-steps .card__descr');
    var categoryHubCardDescr = $('.page_category-hub .card__descr');
    var categoryLandingCards = $('.category-landing .card__title a');
    var productCardHorizontal = $('.product-card-preview .product-card__title');
    var productCardList = $('.products-list .product-card__title');
    var productCardDesc = $('.products-list .product-card__description .card__descr');
    var wismoProductLink = $('.wismo-order__name-link');
    var complementCardHeader = $('.complem__head');
    var orderItemTitle = $('.order-item__title');
    let customSlider = $('.custom-slider.mini-basket');

    function clampText(selector, size) {
        if (selector.length) {
            selector.each(function (index, value) {
                $clamp(value, {clamp: size});
            });
        }
    }



    function clampOrderTitle() {
        if (window.matchMedia('(min-width: 768px)').matches) {
            clampText(orderItemTitle, 2);
        } else {
            clampText(orderItemTitle, 3);
        }
    }

    if (wismoProductLink.length) {
        wismoProductLink.each(function (index, value) {
            $clamp(value, {clamp: 2});
        });
    }

    if (rangeListCardDescr.length) {
        rangeListCardDescr.each(function (index, value) {
            $clamp(value, {clamp: 2});
        });
    }

    if (categoryHubCardDescr.length) {
        categoryHubCardDescr.each(function (index, value) {
            $clamp(value, {clamp: 3});
        });
    }

    if (categoryLandingCards.length) { //TODO: make one function
        categoryLandingCards.each(function (index, value) {
            $clamp(value, {clamp: 3});
        });
    }

    if (productCardHorizontal.length && !customSlider.length) {
        productCardHorizontal.each(function (index, value) {
            $clamp(value, {clamp: 3});
        });
    }

    if (complementCardHeader.length) {
        complementCardHeader.each(function (index, value) {
            $clamp(value, {clamp: 3});
        });
    }

    clampOrderTitle();

    window.onresize = function () {
        if (window.matchMedia('(min-width: 768px)').matches) {
            if (productCardList.length) {
                productCardList.each(function (index, value) {
                    $clamp(value, {clamp: 2});
                });
            }
        } else {
            if (productCardList.length) {
                productCardList.each(function (index, value) {
                    $clamp(value, {clamp: 3});
                });
            }
        }

        if (window.matchMedia('(min-width: 768px)').matches) {
            if (productCardDesc.length) {
                productCardDesc.each(function (index, value) {
                    $clamp(value, {clamp: 2});
                });
            }
        } else {
            if (productCardDesc.length) {
                productCardDesc.each(function (index, value) {
                    $clamp(value, {clamp: 3});
                });
            }
        }

        clampOrderTitle();
    }

})();
