define(function(require){"use strict";var $=require("jquery");var ccConstants=require("ccConstants");var ccRestClient=require("ccRestClient");var DEFAULT_COMPARATOR="EQ";var DEFAULT_NO_OF_LOCATIONS=10;function Inventory(){}Inventory.prototype.getLocationInventoryForUserQuery=function(requestConfig,successCallBack,errorCallBack){var self=this;if(requestConfig.searchText&&null!==requestConfig.searchText){self.getLocations(requestConfig,function(locations){var locationIds="";if(locations.items&&locations.items.length>0){$.each(locations.items,function(index,location){locationIds=locationIds+location.locationId;if(index!==locations.items.length-1){locationIds=locationIds+","}})}else{errorCallBack({storeLookupStatus:-2})}requestConfig.locationIds=locationIds;self.getStockStatuses(requestConfig,function(stockStatuses){var storeInventoryInfos=[];var storeLocationInventoryMap=stockStatuses.reduce(function(targetMap,inventoryObj){if(!targetMap[inventoryObj.locationId]||null===targetMap[inventoryObj.locationId]){var storeInventoryObjs=new Array;storeInventoryObjs.push(inventoryObj);targetMap[inventoryObj.locationId]=storeInventoryObjs}else{var storeInventoryObjs=targetMap[inventoryObj.locationId];storeInventoryObjs.push(inventoryObj)}return targetMap},{});$.each(locations.items,function(index,location){var storeObj={};storeObj.store=location;storeObj.locationId=location.locationId;storeObj.inventoryDetails=storeLocationInventoryMap[location.locationId];var inventoryObj=self.getInventoryInfo(storeObj.inventoryDetails,requestConfig);if(null!==inventoryObj){storeObj.availableQuantity=inventoryObj.availableQuantity;storeObj.availabilityStatusMsg=inventoryObj.availabilityStatusMsg}storeInventoryInfos.push(storeObj)});successCallBack(storeInventoryInfos)},function(){errorCallBack({storeLookupStatus:-1})})},function(data){errorCallBack({storeLookupStatus:-1})})}};Inventory.prototype.getLocations=function(requestConfig,successCallBack,errorCallBack){var inputParams={};if(requestConfig&&null!==requestConfig.searchText&&""!==requestConfig.searchText){inputParams["q"]=buildLocationSearchQuery(requestConfig)}if(requestConfig.noOfStoresToDisplay&&null!==requestConfig.noOfStoresToDisplay){inputParams["limit"]=requestConfig.noOfStoresToDisplay}else{inputParams["limit"]=DEFAULT_NO_OF_LOCATIONS}var url=ccConstants.ENDPOINT_LOCATIONS_LIST_LOCATIONS;ccRestClient.request(url,inputParams,successCallBack,errorCallBack)};Inventory.prototype.getStockStatuses=function(requestConfig,successCallBack,errorCallBack){var stockStatusInfo=null;var inputParams={};if(requestConfig.productSkuIds&&null!=requestConfig.productSkuIds&&""!==requestConfig.productSkuIds){inputParams["products"]=requestConfig.productSkuIds}inputParams["catalogId"]=requestConfig.catalogId&&null!==requestConfig.catalogId?requestConfig.catalogId:"";if(requestConfig.locationIds&&null!==requestConfig.locationIds&&""!==requestConfig.locationIds){inputParams["locationIds"]=requestConfig.locationIds}inputParams["actualStockStatus"]=true;var url=ccConstants.ENDPOINT_PRODUCTS_AVAILABILITY;ccRestClient.request(url,inputParams,successCallBack,errorCallBack)};function buildLocationSearchQuery(requestConfig){var locationSearchQuery=null;if(requestConfig.locationSearchQuery&&null!==requestConfig.locationSearchQuery&&""!==requestConfig.locationSearchQuery){locationSearchQuery=requestConfig.locationSearchQuery}else{var locationSearchContext=null;if(requestConfig.siteId&&null!==requestConfig.siteId&&""!==requestConfig.siteId){locationSearchContext='sites EQ "'+requestConfig.siteId+'"'}if(requestConfig.locationType&&null!==requestConfig.locationType&&""!==requestConfig.locationType){if(null!==locationSearchContext){locationSearchContext=locationSearchContext+" AND "}locationSearchContext=locationSearchContext+'type EQ "'+requestConfig.locationType+'"'}if(requestConfig.pickUp&&null!==requestConfig.pickUp&&""!==requestConfig.pickUp){if(null!==locationSearchContext){locationSearchContext=locationSearchContext+" AND "}locationSearchContext=locationSearchContext+"pickUp EQ "+requestConfig.pickUp}if(requestConfig.searchableFields&&null!==requestConfig.searchableFields&&""!==requestConfig.searchableFields.length>0){var searchFields=[];for(var searchFieldsIndex=0;searchFieldsIndex0){for(var i=0;i0){var isSelectedAddOn=false;for(var selectedAddOnIndex=0;selectedAddOnIndexitemQuantity){storeInventoryInfo.availableQuantity=itemQuantity}}}}return storeInventoryInfo};Inventory.prototype.getProductSkuIdsInCartItem=function(cartItem){var prodSkuIds=new Array;prodSkuIds.push(cartItem.productId+":"+cartItem.catRefId);var getProductSkuFromChild=function(cartItem){for(var i=0;i0){getProductSkuFromChild(childItem)}}};if(cartItem.childItems&&null!==cartItem.childItems&&cartItem.childItems.length>0){getProductSkuFromChild(cartItem)}$.unique(prodSkuIds);return prodSkuIds.join(",")};return Inventory});