define(["jquery","underscore"],function(t,e){"use strict";return t.widget("mage.amShowLabel",{options:{},textElement:null,image:null,imageWidth:null,imageHeight:null,parent:null,imageLabel:[".amasty-label-container",".amasty-label-image",".amlabel-text"],fotoramaStage:'[data-fotorama-stage="fotorama__stage"]',galleryPlaceholder:'[data-gallery-role="gallery-placeholder"]',positionWrapper:"amlabel-position-wrapper",mainContainer:'[data-gallery-role="amasty-main-container"]',slickSlider:{selectors:{slider:".slick-slider",slide:".slick-slide"},classes:{cloned:"slick-cloned",initialized:"slick-initialized"}},_create:function(){var e,i,s,n;if(this.parentResizeListener=new ResizeObserver(function(){this.setLabelStyle(),this._refreshSLickSlider()}.bind(this)),this.element=t(this.element),!this.element.attr("amlabel-js-observed")){if(this.element.attr("amlabel-js-observed",1),1===this.options.move){if(e=this.element.closest(".page-wrapper").find(this.getPriceSelector()),!e.length)return void this.element.hide();if(e=this._filterItems(e),i=e.first(),!(s=this.getNewParent(i))||!s.length)return void this.element.hide();i.attr("label-observered-"+this.options.label,"1"),s.append(this.element)}this.image=this.element.find(".amasty-label-image"),this.textElement=this.element.find(".amlabel-text"),this.parent=this.element.parent(),this.image.length||this.setStyleIfNotExist(this.element,{width:"100px",height:"50px"}),!this.image.length&&this.options.position.includes("center")&&this.textElement.addClass("-am-centered"),this.options.path&&""!==this.options.path&&(s=this.parent.find(this.options.path),s.length&&(this.parent=s,s.append(this.element))),"absolute"!==this.parent.css("position")&&"relative"!==this.parent.css("position")&&this.parent.css("position","relative"),"A"!==this.parent.prop("tagName")||this.parent.closest(".block.widget").hasClass("list")||this.parent.css("display","block"),this.element.closest(".sidebar").length||this.element.closest(".product-item-info").css("zIndex","9"),this.imageLoaded(this.image)?(this.element.fadeIn(),this.image[0]&&(this.imageWidth=this.image[0].naturalWidth,this.imageHeight=this.image[0].naturalHeight)):(n=this,this.image.on("load",function(){n.element.fadeIn(),n.imageWidth=this.naturalWidth,n.imageHeight=this.naturalHeight,n.setLabelStyle()})),this.setLabelPosition(),this.updateStyles(),this.productPageZoomEvent(),this.createResizeEvent(),this.createFotoramaEvent(),this.createRedirectEvent(),this._refreshSLickSlider()}},_refreshSLickSlider:function(){var t=this.element.closest(this.slickSlider.selectors.slider);t.length&&t.hasClass(this.slickSlider.classes.initialized)&&t.slick("refresh")},_filterItems:function(e){return e.filter(function(e,i){return!t(i).closest(this.slickSlider.selectors.slide).hasClass(this.slickSlider.classes.cloned)&&!t(i).closest(".amrelated-pack-item").length&&!t(i).closest(".product-item-info").find(".amasty-label-container-"+this.options.label+"-"+this.options.product+"-cat").length}.bind(this))},createRedirectEvent:function(){this.element.on("click",function(t){this.openLink(t)}.bind(this))},openLink:function(t){var e=this.options.redirect_url;e&&!this.isEmpty(e)&&(t.preventDefault(),window.open(e,"_blank"))},createResizeEvent:function(){t(window).on("resize",e.debounce(function(){this.reloadParentSize()}.bind(this),300)),t(window).on("orientationchange",function(){this.reloadParentSize()}.bind(this))},createFotoramaEvent:function(){t(this.galleryPlaceholder).on("fotorama:load",this.updatePositionInFotorama.bind(this))},updatePositionInFotorama:function(){var t=this,e=this.parent.find(this.options.path),i=null;this&&this.options.path&&"prod"===this.options.mode&&e.length&&e!==this.parent&&(this.parent.css("position",""),this.parent=e,i=this.element.parent().hasClass(this.positionWrapper)?this.element.parent():this.element,e.append(i),e.css({position:"relative"}),t.setLabelsEventOnFotorama())},setLabelsEventOnFotorama:function(){var e=this,i=t(this.fotoramaStage),s=t._data(i[0],"events").mousedown,n=t._data(i[0],"events").pointerdown;i[0].eventsUpdated||(i.on("mousedown pointerdown",function(i){1===i.which&&t(i.target).is(e.imageLabel.join(","))&&(t(this).trigger("focusout"),e.openLink(i),i.stopImmediatePropagation())}),i.length&&s&&s.unshift(s.pop()),i.length&&n&&n.unshift(n.pop()),i[0].eventsUpdated=!0)},imageLoaded:function(t){return!(!t.complete||void 0!==t.naturalWidth&&0===t.naturalWidth)},productPageZoomEvent:function(){var e=t(this.mainContainerSelector);"prod"===this.options.mode&&e.length&&(this.parent=e,e.append(this.element.parent()),e.css("position","relative"))},updateStyles:function(){this.setLabelStyle(),this.setLabelPosition()},setStyleIfNotExist:function(t,e){for(var i in e){var s=t.attr("style");(!s||-1===s.indexOf("; "+i)&&-1===s.indexOf(";"+i))&&t.css(i,e[i])}},setLabelStyle:function(){var t,e,i,s,n=this.options.redirect_url,a=1===this.options.alignment?"inline-block":"block";if(this.setStyleIfNotExist(this.textElement,{padding:"0 3px",position:"absolute","box-sizing":"border-box","white-space":"nowrap",width:"100%"}),this.image.length){if(this.image.css({width:"100%"}),this.options.size>0){if(!(t=Math.round(this.parent.css("width").replace(/[^\d.]/g,""))))return void this.parentResizeListener.observe(this.parent[0]);t&&(this.parentResizeListener.disconnect(),this.imageWidth=Math.round(t*this.options.size/100))}else this.imageWidth+="px";this.setStyleIfNotExist(this.element,{width:this.imageWidth}),this.imageHeight=this.image.height(),!this.imageHeight&&this.image[0]&&0!==this.image[0].naturalWidth&&(e=this.image[0].naturalWidth,i=this.image[0].naturalHeight,this.imageHeight=Math.round(this.imageWidth*(i/e))),s=this.textElement.html().split("<br>").length,s=s>=1?s:1,this.textElement.css("lineHeight",Math.round(this.imageHeight/s)+"px"),this.imageHeight&&this.setStyleIfNotExist(this.element,{height:this.imageHeight+"px"}),this._fitLabelText()}this.image.attr("width",this.image.width()+"px").attr("height",this.image.height()+"px"),this.element.parent().css({"line-height":"normal",position:"absolute"}),this.element.hasClass("amlabel-swatch")||this.setStyleIfNotExist(this.element,{position:"relative",display:a}),n&&!this.isEmpty(n)&&this.element.addClass("-link"),this.reloadParentSize()},_fitLabelText:function(){if(this.options.size){var t=1;for(this.textElement.css({width:"auto"}),this.textElement.parent().css("display","block");this.textElement.width()>.9*this.textElement.parent().width()&&t++<15;)this.textElement.css({fontSize:100-5*t+"%"});this.textElement.parent().css("display","none"),this.textElement.css({width:"100%"})}},isEmpty:function(t){return null===t||null!==t.match(/^ *$/)},setPosition:function(t){this.options.position=t,this.setLabelPosition(),this.reloadParentSize()},setStyle:function(){this.setLabelStyle()},reloadParentSize:function(){var e=this.element.parent(),i=null,s=5;e.css({position:"relative",display:"inline-block",width:"auto",height:"auto"}),i=e.height(),1===this.options.alignment?e.children().each(function(e,i){s+=t(i).width()+parseInt(t(i).css("margin-left"),10)+parseInt(t(i).css("margin-right"),10)}):s=e.width(),e.css({position:"absolute",display:"block",height:i?i+"px":"",width:s?s+"px":""})},getWidgetLabelCode:function(){var t="";return this.element.parents(".widget-product-grid, .widget").length&&(t="widget"),t},setLabelPosition:function(){var e,i,s="amlabel-position-"+this.options.position+"-"+this.options.product+"-"+this.options.mode+this.getWidgetLabelCode(),n=this.parent.find("."+s);switch(n.length?(i=!1,t.each(n.find(".amasty-label-container"),function(e,s){var n=t(s).next(),a=parseInt(this.options.order,10),o=null,h=null;if(t(s).length&&t(s).data("mageAmShowLabel")&&(o=parseInt(t(s).data("mageAmShowLabel").options.order,10)),n.length&&t(n).data("mageAmShowLabel")&&(h=parseInt(n.data("mageAmShowLabel").options.order,10)),a>=o&&(!h||a<=h))return i=!0,t(s).after(this.element),!1}.bind(this)),i||n.prepend(this.element)):(e=this.element.parent(),e.hasClass(this.positionWrapper)&&e.parent().html(this.element),this.element.wrap('<div class="'+s+" "+this.positionWrapper+'"></div>'),n=this.element.parent()),1===this.options.alignment?n.children(":not(:first-child)").each(function(e,i){this.setStyleIfNotExist(t(i),{marginLeft:this.options.margin+"px"})}.bind(this)):n.children(":not(:first-child)").each(function(e,i){this.setStyleIfNotExist(t(i),{marginTop:this.options.margin+"px"})}.bind(this)),n.css({top:"",left:"",right:"",bottom:"","margin-top":"","margin-bottom":"","margin-left":"","margin-right":""}),this.options.position){case"top-left":n.css({top:0,left:0});break;case"top-center":n.css({top:0,left:0,right:0,"margin-left":"auto","margin-right":"auto"});break;case"top-right":n.css({top:0,right:0,"text-align":"right"});break;case"middle-left":n.css({left:0,top:0,bottom:0,"margin-top":"auto","margin-bottom":"auto"});break;case"middle-center":n.css({top:0,bottom:0,"margin-top":"auto","margin-bottom":"auto",left:0,right:0,"margin-left":"auto","margin-right":"auto"});break;case"middle-right":n.css({top:0,bottom:0,"margin-top":"auto","margin-bottom":"auto",right:0,"text-align":"right"});break;case"bottom-left":n.css({bottom:0,left:0});break;case"bottom-center":n.css({bottom:0,left:0,right:0,"margin-left":"auto","margin-right":"auto"});break;case"bottom-right":n.css({bottom:0,right:0,"text-align":"right"})}},getNewParent:function(t){var e=null,i=t.closest(".item.product");return i.length||(i=t.closest(".product-item")),i&&i.length&&(e=i.find(this.options.path).first()),e},getPriceSelector:function(){var t=":not([label-observered-"+this.options.label+"])";return'[data-product-id="'+this.options.product+'"]'+t+', [id="product-price-'+this.options.product+'"]'+t+', [name="product"][value="'+this.options.product+'"]'+t}}),t.mage.amShowLabel});
//# sourceMappingURL=label.js.map