define(["jquery","lodash","global/all"],function(t,e,a){"use strict";function i(t){a.Logger.info("Selections -> getProduct"),a.Logger.info(t),a.EventManager.notify(a.Events.products.SELECTIONS_UPDATE,t)}function r(t){a.Data.getAsync({url:h+"/api/product/"+t+"/",callback:i})}function u(t){var a=e.filter(g,function(a){var i=!0;return e.forEach(t,function(t){e.some(a.selections,t)||(i=!1)}),i}),i={};return e.forEach(V,function(t){i[t.attributeId]=[]}),e.forEach(a,function(t){e.forEach(t.selections,function(t){-1===i[t.attributeId].indexOf(t.attributeValueId)&&i[t.attributeId].push(t.attributeValueId)})}),i}function n(){var a=l();e.each(V,function(i){var r=e.filter(a,function(t){return t.attributeId!=i.attributeId}),n={};r.length>0?n=u(r):(n[i.attributeId]=[],e.each(i.options,function(t){n[i.attributeId].push(t.attributeValueId)}));var l=n[i.attributeId];switch(i.selectType.toLowerCase()){case"dropdown":t("#"+i.attributeId+" option").each(function(){t(this).val().length>0&&(l.indexOf(t(this).val())>-1?t(this).removeClass("disabled"):t(this).addClass("disabled"))});break;case"swatch":case"box":t("#"+i.attributeId+" li").each(function(){l.indexOf(t(this).attr("id"))>-1?t(this).removeClass("disabled"):t(this).addClass("disabled")})}})}function l(){var e=[],a=0;return t("input[id^='currentValue_']").each(function(){if(""!==t(this).val()){var i=t(this).attr("id").replace("currentValue_",""),r={attributeValueId:t(this).val(),attributeId:i};e[a]=r,a++}}),e}function d(){var e=l();if(v.selections.length===e.length){var a=null;t.each(g,function(){for(var t=!0,i=0;i<this.selections.length;i++){for(var r=!1,u=0;u<e.length;u++)e[u].attributeValueId===this.selections[i].attributeValueId&&(r=!0);r===!1&&(t=!1)}t===!0&&(a=this)}),a&&(r(a.id),I.val(a.id))}else r(f),I.val("")}function c(a,i){var r=E[a];if(null!==r){var c=V[r.attributeId];if(i){var s=l(),b=u(s);e.each(s,function(t){t.attributeId===r.attributeId&&(t.attributeValueId=a)});var f=u(s);for(var v in b)for(var h in b[v])f[v].indexOf(h)<0&&v!==r.attributeId&&o(v,!1)}t("#selected_"+c.attributeId).html(r.value),t("#deselect_"+c.attributeId).show(),t("#currentValue_"+c.attributeId).val(r.attributeValueId),t("#"+r.attributeValueId).addClass("active selected"),n(),d()}}function o(e){var a=V[e];t("#selected_"+a.attributeId).html(""),t("#deselect_"+a.attributeId).hide(),"dropdown"===a.selectType.toLowerCase()&&t("#"+a.attributeId).val(t("#"+a.attributeId+" option:first").val()),t("#currentValue_"+a.attributeId).val(""),a.options.forEach(function(e){t("#"+e.attributeValueId).removeClass("active selected").attr("aria-checked","false").closest("ul").attr("aria-activedescendant","")}),n(),d()}function s(t){var e=E[t];if(null!==e){var i=V[e.attributeId],r=l(),u="";for(var n in r){var d=E[r[n].attributeValueId];d.attributeId!==e.attributeId&&(u+=0===u.length?d.value:" & "+d.value)}a.UI.Modal.alert(e.value+" is unavailable in "+u+".","Please Select a different "+i.name)}}function b(a,i,r){h=a,f=i.id,v=i,r=r||{},g=e.indexBy(v.items,"id"),e.forEach(v.selections,function(t){var a=t.attributeId;V[a]=t,e.forEach(t.options,function(e){e.attributeId=t.attributeId,E[e.attributeValueId]=e,p&&r[a]===e.attributeValueId?e.selected=!0:1===t.options.length&&(e.selected=!0)})}),I=t("#hdnProductId"),t('input[id^="currentValue_"]').each(function(){t(this).val("");var e=t(this).attr("id").split("_").pop();t("#"+e).val("")}),p&&(e.forEach(V,function(a){var i=e.find(a.options,{selected:!0});if(i){switch(a.selectType.toLowerCase()){case"dropdown":t("#"+a.attributeId).val(i.attributeValueId).trigger("change");break;case"swatch":case"box":t("#"+a.attributeId+" li#"+i.attributeValueId).addClass("active selected").attr("aria-checked","true").trigger("click")}c(i.attributeValueId)}}),n())}var f,v,h,I,p=!0,g={},V={},E={},C={init:b,GetProduct:r,GetAvailableOptions:u,UpdateAvailableOptions:n,GetCurrentOptions:l,SelectAttributeValue:c,DeselectAttributeValue:o,UnavailableSelection:s};return a.Logger.debug("Selections.js Loaded:",C),C});
//# sourceMappingURL=Selections.js.map