{"size": 1668, "ext": "js", "max_stars_repo_path": "cartridges/app_storefront_base/cartridge/models/productLineItem/decorators/quantityOptions.js", "max_stars_repo_name": "yuansfer/plugins-salesforce", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "cartridges/app_storefront_base/cartridge/models/productLineItem/decorators/quantityOptions.js", "max_issues_repo_name": "yuansfer/plugins-salesforce", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "cartridges/app_storefront_base/cartridge/models/productLineItem/decorators/quantityOptions.js", "max_forks_repo_name": "yuansfer/plugins-salesforce", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 39.7142857143, "max_line_length": 105, "alphanum_fraction": 0.7410071942}
'use strict';

var ProductInventoryMgr = require('dw/catalog/ProductInventoryMgr');
var preferences = require('*/cartridge/config/preferences');
var DEFAULT_MAX_ORDER_QUANTITY = preferences.maxOrderQty || 10;

/**
 * get the min and max numbers to display in the quantity drop down.
 * @param {Object} productLineItem - a line item of the basket.
 * @param {number} quantity - number of items for this product
 * @returns {Object} The minOrderQuantity and maxOrderQuantity to display in the quantity drop down.
 */
function getMinMaxQuantityOptions(productLineItem, quantity) {
    var availableToSell = productLineItem.product.availabilityModel.inventoryRecord.ATS.value;
    var perpetual = productLineItem.product.availabilityModel.inventoryRecord.perpetual;
    var max;
    if (productLineItem.productInventoryListID) {
        var inventoryList = ProductInventoryMgr.getInventoryList(productLineItem.productInventoryListID);
        var inventoryRecord = inventoryList.getRecord(productLineItem.product.ID);
        availableToSell = inventoryRecord.ATS.value;
        perpetual = inventoryRecord.perpetual;
    }

    if (perpetual) {
        max = Math.max(DEFAULT_MAX_ORDER_QUANTITY, quantity);
    } else {
        max = Math.max(Math.min(availableToSell, DEFAULT_MAX_ORDER_QUANTITY), quantity);
    }

    return {
        minOrderQuantity: productLineItem.product.minOrderQuantity.value || 1,
        maxOrderQuantity: max
    };
}

module.exports = function (object, productLineItem, quantity) {
    Object.defineProperty(object, 'quantityOptions', {
        enumerable: true,
        value: getMinMaxQuantityOptions(productLineItem, quantity)
    });
};
