/*!! * VtexCatalog.js v1.3.0 * https://github.com/zeindelf/vtex-catalog * * Copyright (c) 2017-2018 Zeindelf * Released under the MIT license * * Date: 2018-09-15T19:27:30.647Z */ !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e.VTEX=e.VTEX||{},e.VTEX.VtexCatalog=r())}(this,function(){"use strict";var e="1.17.0",p={SEARCH_URL:"/api/catalog_system/pub/products/search/",FACETS_URL:"/api/catalog_system/pub/facets/search/",ERRORS:{searchParamsNotDefined:"Search parameters is not defined.",searchParamsNotAnObject:"Search parameters is not a valid Object.",searchRangeNotArray:"'range' is not an Array",productIdNotDefined:"Product ID is not defined.",skuIdNotDefined:"Sku ID is not defined.",productIdArrayNotAnArray:"'productIdArray' is not an Array.",skuIdArrayNotAnArray:"'skuIdArray' is not an Array.",productIdArrayNotDefined:"'productIdArray' is not an defined.",skuIdArrayNotDefined:"'skuIdArray' is not an defined.",fqPropertyNotFound:"The property 'fq' was not found.",itemsIdNotDefined:"'itemsId' is not defined.",itemsIdNotAnArray:"'itemsId' is not an Array.",searchItemsNotDefined:"Search items is not defined. Use 'fq' or 'ft' to search.",shelfIdNotDefined:"'shelfId' is not defined.",shelfIdNotAString:"'shelfId' is not a String.",callbackNotAFunction:"'callback' must be a Function"},MESSAGES:{vtexUtils:'VtexUtils.js is required and must be an instance. Download it from https://www.npmjs.com/package/vtex-utils and use "new VtexCatalog(new VtexUtils())"',vtexUtilsVersion:e,vtexUtilsVersionMessage:"'VtexUtils.js' version must be "+e+" or higher. Download last version on https://www.npmjs.com/package/vtex-utils"}},t=function(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")},r=function(){function n(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(e,r,t){return r&&n(e.prototype,r),t&&n(e,t),e}}(),m=function(e,r){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,r){var t=[],n=!0,s=!1,a=void 0;try{for(var o,i=e[Symbol.iterator]();!(n=(o=i.next()).done)&&(t.push(o.value),!r||t.length!==r);n=!0);}catch(e){s=!0,a=e}finally{try{!n&&i.return&&i.return()}finally{if(s)throw a}}return t}(e,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")},f=new(function(){function e(){t(this,e),this._maxParamsPerRequest=50,this._emptyFetchedParams=[],this._pendingParamsToFetch=[],this._fetchedParams=[],this._pendingFetchArray=[],this._camelizeItems=!1,this._camelizeProps=!1,this._priceInfo=!1,this._sortSku=!1,this._sortSkuItems=[],this._sortSkuName="",this._installmentGroup=!1,this._setCustomFilter=null}return r(e,[{key:"_getInstance",value:function(e,r){this._globalHelpers=e.globalHelpers,this._vtexHelpers=e.vtexHelpers,this._catalog=r}},{key:"_error",value:function(e){throw new Error(p.ERRORS[e])}},{key:"_setCache",value:function(e){var t=this,n=e.productId,r=e.items;this._catalog.productCache[n]=e,r.forEach(function(e){var r=e.itemId;t._catalog.skusProductIds[r]=n})}},{key:"_search",value:function(s){var e,i=this,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},a=this,o=$.extend({},s),l=this._pendingFetchArray,u=[];for(var r in s)"map"!==r&&(o[r]=s[r].filter(function(e){return!~i._emptyFetchedParams.indexOf(e)&&(!~i._fetchedParams.indexOf(e)&&(!~i._pendingParamsToFetch.indexOf(e)&&(i._pendingParamsToFetch.push(e),!0)))}));var t=1;this._globalHelpers.isArray(s.fq)&&(t=o.fq.length);for(var c=Math.ceil(t/this._maxParamsPerRequest),f=function(e){var t=e*i._maxParamsPerRequest+"-"+((e+1)*i._maxParamsPerRequest-1),r=$.Deferred();$.ajax({url:p.SEARCH_URL,data:$.param(o,!0),beforeSend:function(e){for(var r in a._requestStartEvent(),n)({}).hasOwnProperty.call(n,r)&&e.setRequestHeader(r,n[r]);e.setRequestHeader("resources",t)},success:function(e){r.resolve(e)}}),l.push(r.promise())},h=0;h<c;h+=1)f(h);var d=$.Deferred();return(e=$).when.apply(e,function(e){if(Array.isArray(e)){for(var r=0,t=Array(e.length);r<e.length;r++)t[r]=e[r];return t}return Array.from(e)}(l)).done(function(){for(var e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];for(var n in r.forEach(function(e,r){e.forEach(function(e){e=i._parseCamelize(e),e=i._setPriceInfo(e),e=i._setSortSku(e),e=i._setInstallmentsGroup(e),i._globalHelpers.isNull(i._setCustomFilter)||i._setCustomFilter.apply(i,[e]),i._setCache(e)}),l.splice(r,1)}),s)({}).hasOwnProperty.call(s,n)&&s[n].forEach(function(e){var r=e.split(":"),t=m(r,2),n=t[0],s=t[1],a=void 0;switch(i._fetchedParams.push(e),n){case"skuId":var o=i._catalog.skusProductIds[s];a=i._catalog.productCache[o];break;case"productId":a=i._catalog.productCache[s]}i._globalHelpers.isUndefined(a)?i._emptyFetchedParams.push(e):u.push(a)});d.resolve(u)}).fail(function(){for(var e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];return d.reject(r)}),d.promise()}},{key:"_searchPage",value:function(e,t){var n=this,r=$.extend({},e),s=this._maxParamsPerRequest+"-"+(this._maxParamsPerRequest-1),a=$.Deferred();return $.ajax({url:"/buscapagina/",data:$.param(r,!0),beforeSend:function(e){for(var r in n._requestStartEvent(),t)({}).hasOwnProperty.call(t,r)&&e.setRequestHeader(r,t[r]);e.setRequestHeader("resources",s)}}).then(function(e){return a.resolve(e)}),a.promise()}},{key:"_searchFacets",value:function(){for(var e=window.location.pathname,r=globalHelpers.arrayCompact(e.split("/")).length,t="?map=c",n=0;n<r-1;n+=1)t+=",c";return window.console.log(r),window.console.log(t),$.Deferred(function(r){return $.ajax({url:p.FACETS_URL+"/"+e+t}).then(function(e){return r.resolve(e)}).fail(function(e){return r.reject(e)})}).promise()}},{key:"_setPriceInfo",value:function(e){if(this._priceInfo){var r=this._vtexHelpers.getFirstAvailableSku(e);for(var t in e.available=!!r,e.items)if({}.hasOwnProperty.call(e.items,t)){var n=e.items[t],s=this._globalHelpers.objectSearch(n,{sellerDefault:!0});this._globalHelpers.extend(e.items[t],this._vtexHelpers.getProductPriceInfo(s))}}return e}},{key:"_parseCamelize",value:function(e){var t=this;if(this._camelizeItems){if((e=this._globalHelpers.camelize(e)).hasOwnProperty("allSpecifications")&&(e.allSpecifications=e.allSpecifications.map(function(e){return t._globalHelpers.camelize(e)})),this._camelizeProps)for(var r in e)({}).hasOwnProperty.call(e,r)&&this._globalHelpers.contains(r,this._camelizeProps)&&this._globalHelpers.isArray(e[r])&&(e[r]=e[r].map(function(e,r){return t._globalHelpers.camelize(e)}));e.isCamelized=!0}return e}},{key:"_setSortSku",value:function(e){if(this._sortSku){var r=this._vtexHelpers.sortProductSearch(e,this._sortSkuItems,this._sortSkuName);e.items=r}return e}},{key:"_setInstallmentsGroup",value:function(e){if(this._installmentGroup)for(var r in e.items)if({}.hasOwnProperty.call(e.items,r)){var t=e.items[r],n=this._globalHelpers.objectSearch(t,{sellerDefault:!0}),s=this._vtexHelpers.getGroupInstallments(n);e.items[r].installmentsGrouped=this._globalHelpers.isObjectEmpty(s)?null:s}return e}},{key:"_requestStartEvent",value:function(){var e=$.Event("requestStart.vtexCatalog");$(document).trigger(e)}},{key:"_requestEndEvent",value:function(e){var r=$.Event("request"+e+"End.vtexCatalog");$(document).trigger(r)}}]),e}()),n={_setInstance:function(e){f._getInstance(e,this)},setCamelize:function(e,r){f._camelizeItems=e,f._camelizeProps=r},setPriceInfo:function(e){f._priceInfo=e},setSortSku:function(e,r,t){f._sortSku=e,f._sortSkuItems=r,f._sortSkuName=t},setInstallmentsGroup:function(e){f._installmentGroup=e},setShelfClass:function(e){f._className=this.globalHelpers.isString(e)?e:""},setCustomFilter:function(e){if(!this.globalHelpers.isFunction(e))return f._error("callbackNotAFunction");f._setCustomFilter=e},searchProduct:function(e){if(this.globalHelpers.isUndefined(e))return f._error("productIdNotDefined");var n=$.Deferred();if(this.productCache[e])n.resolve(this.productCache[e]);else{var r={fq:["productId:"+e]};f._search(r).done(function(e){return n.resolve(!!e.length&&e[0])}).fail(function(){for(var e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];return n.reject(r)})}return n.then(function(){return f._requestEndEvent("Product")}),n.promise()},searchSku:function(e){if(this.globalHelpers.isUndefined(e))return f._error("skuIdNotDefined");var n=$.Deferred();if(this.skusProductIds[e])n.resolve(this.productCache[this.skusProductIds[e]]);else{var r={fq:["skuId:"+e]};f._search(r).done(function(e){return n.resolve(!!e.length&&e[0])}).fail(function(){for(var e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];return n.reject(r)})}return n.then(function(){return f._requestEndEvent("Sku")}),n.promise()},searchProductArray:function(e){var n=this;if(this.globalHelpers.isUndefined(e))return f._error("productIdArrayNotDefined");if(!this.globalHelpers.isArray(e))return f._error("productIdArrayNotAnArray");for(var s=$.Deferred(),a={},r={fq:[]},t=0,o=e.length;t<o;t+=1)this.globalHelpers.isUndefined(this.productCache[e[t]])?r.fq.push("productId:"+e[t]):a[e[t]]=this.productCache[e[t]];r.fq.length?f._search(r).done(function(e){for(var r=0,t=e.length;r<t;r+=1)a[e[r].productId]=e[r];s.resolve(!!n.globalHelpers.length(a)&&a)}).fail(function(){for(var e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];return s.reject(r)}):s.resolve(a);return s.then(function(){return f._requestEndEvent("ProductArray")}),s.promise()},searchSkuArray:function(e){var n=this;if(this.globalHelpers.isUndefined(e))return f._error("skuIdArrayNotDefined");if(!this.globalHelpers.isArray(e))return f._error("skuIdArrayNotAnArray");for(var s=$.Deferred(),a={},r={fq:[]},t=0,o=e.length;t<o;t+=1)if(this.skusProductIds[e[t]]){var i=this.skusProductIds[e[t]];a[i]=this.productCache[i]}else r.fq.push("skuId:"+e[t]);r.fq.length?f._search(r).done(function(e){for(var r=0,t=e.length;r<t;r+=1)a[e[r].productId]=e[r];s.resolve(!!n.globalHelpers.length(a)&&a)}).fail(function(){for(var e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];return s.reject(r)}):s.resolve(a);return s.then(function(){return f._requestEndEvent("SkuArray")}),s.promise()},searchDefault:function(e){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[];if(this.globalHelpers.isUndefined(e))return f._error("searchParamsNotDefined");if(!this.globalHelpers.isObject(e))return f._error("searchParamsNotAnObject");if(!e.hasOwnProperty("fq")&&!e.hasOwnProperty("ft"))return f._error("searchItemsNotDefined");if(!this.globalHelpers.isArray(r))return f._error("searchRangeNotArray");var t={map:[]};if(e.hasOwnProperty("fq")){for(var n=0,s=e.fq.length;n<s;n+=1){var a=e.fq[n];if(a.match("C:"))for(var o=a.split("/"),i=0,l=o.length;i<l;i+=1)o[i].match(/\d.+/gi)&&t.map.push("c");a.match(/P\[.+[\d\w\s]?\]/g)&&t.map.push("priceFrom")}t.map=t.map.join(",")}var u={_from:r[0]<0?1:r[0]||1,_to:r[1]<0?50:r[1]||50};$.extend(e,t,u);var c=$.Deferred();if(49<u._to-u._from)throw new RangeError("Range must be a max value between 50 items");return $.ajax({url:p.SEARCH_URL,data:$.param(e,!0),beforeSend:function(e){f._requestStartEvent()}}).then(function(e,r,t){return e=e.map(function(e){return f._parseCamelize(e)}).map(function(e){return f._setPriceInfo(e)}),$.Deferred().resolve(e,r,t).promise()}).done(function(){return c.resolve.apply(c,arguments)}).fail(function(e){return c.reject(e)}),c.then(function(){return f._requestEndEvent("SearchDefault")}),c.promise()},searchPage:function(t){var n=1<arguments.length&&void 0!==arguments[1]&&arguments[1],e=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(this.globalHelpers.isUndefined(t))return f._error("searchParamsNotDefined");if(!this.globalHelpers.isPlainObject(t))return f._error("searchParamsNotAnObject");if(!t.hasOwnProperty("fq")&&!t.hasOwnProperty("ft"))return f._error("searchItemsNotDefined");if(this.globalHelpers.isUndefined(t.shelfId))return f._error("shelfIdNotDefined");if(!this.globalHelpers.isString(t.shelfId))return f._error("shelfIdNotAString");var s=$.Deferred(),r={},a={sl:t.shelfId,cc:t.columns||100,sm:t.sm||0,O:t.order||"",PageNumber:t.page||1};if(t.hasOwnProperty("fq")){r={fq:[],ps:t.quantity||this.globalHelpers.length(t.fq)};for(var o=0,i=t.fq.length;o<i;o+=1)r.fq.push("productId:"+t.fq[o])}t.hasOwnProperty("ft")&&(r={ft:t.ft,ps:t.quantity||10});var l=$.extend({},r,a);return f._searchPage(l,e).done(function(e){if(n){var r=$(e).find("li[layout="+t.shelfId+"]").removeClass("first last").removeAttr("layout").addClass(f._className);s.resolve(r)}else s.resolve(e)}).fail(function(e){return s.reject(e)}),s.then(function(){return f._requestEndEvent("SearchPage")}),s.promise()},searchFacets:function(){var r=$.Deferred();return f._searchFacets().then(function(e){return r.resolve(e)}).fail(function(e){return r.reject(e)}),r.promise()}};return function e(r){if(t(this,e),this.version="1.3.0",this.name="@VtexCatalog",void 0===r)throw new TypeError(p.MESSAGES.vtexUtils);if("@VtexUtils"!==r.name)throw new TypeError(p.MESSAGES.vtexUtils);if(this.globalHelpers=r.globalHelpers,this.globalHelpers.semverCompare(r.version,p.MESSAGES.vtexUtilsVersion)<0)throw new Error(p.MESSAGES.vtexUtilsVersionMessage);this.vtexHelpers=r.vtexHelpers,this.productCache={},this.skusProductIds={},this.globalHelpers.extend(e.prototype,n),this._setInstance(r)}});