UNPKG

@shopgate/engage

Version:
12 lines 2.27 kB
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{availabilityTypes,AVAILABILITY_TYPE_NOT_AVAILABLE,AVAILABILITY_TYPE_AVAILABLE,AVAILABILITY_TYPE_COMING_SOON}from"../../product/constants";/** * Takes the location stock info settings and finds the matching settings for the given inventory. * @param {Object} settings The settings find a match in. * @param {Object|null} location The location object. * @param {Object|null} productInventory The product inventory. * @returns {Object} */export default(function(settings,location,productInventory){if(location===null||location===void 0?void 0:location.isComingSoon){return settings[AVAILABILITY_TYPE_COMING_SOON];}if(!productInventory){return{};}var _productInventory$isA=productInventory.isAvailable,isAvailable=_productInventory$isA===void 0?false:_productInventory$isA,_productInventory$vis=productInventory.visible,visible=_productInventory$vis===void 0?0:_productInventory$vis;if(isAvailable===false){return settings[AVAILABILITY_TYPE_NOT_AVAILABLE];}if(visible===null){return _extends({},settings[AVAILABILITY_TYPE_AVAILABLE],{availabilityText:'locations.stock_info.available'});}// Filter by inventory blind and visible inventory (must match both). var matchingTypes=availabilityTypes.filter(function(type){return!settings[type].comingSoon;}).filter(function(type){return(// When inventory blind is set in the current availability setting, then this should also // account for the inventory blind from the given store. Ignore inventory blind otherwise. !settings[type].includeInventoryBlind||settings[type].includeInventoryBlind&&!!visible);}).filter(function(type){return(// Don't filter, when inventoryBlind filter is already active settings[type].includeInventoryBlind||// Also, inventory must be truthy or 0, or otherwise there is nothing to search for. visible>=settings[type].visibleInventoryFrom&&(settings[type].visibleInventoryTo===null||visible<=settings[type].visibleInventoryTo));});if(matchingTypes.length>0){return settings[matchingTypes[0]];}return{};});