/**
 * Returns the price of a single line item. Will include sales tax if includeTax is true
 * @param {object} lineItem line item to price
 * @returns {number} price of the line item
 */
export function getPriceOfLineItem(lineItem) {
    let price = (lineItem.kitPrice ?? 0) + (lineItem.laborAmount ?? 0) + (lineItem.sellingPrice ?? 0);
    if (lineItem.childParts && lineItem.childParts.length !== 0) {
        // eslint-disable-next-line no-use-before-define
        price += getPriceOfLineItems(lineItem.childParts);
    }
    return price;
}

export function getSalesTaxOfLineItem(lineItem) {
    let price = lineItem.salesTax ?? 0;
    if (lineItem.childParts && lineItem.childParts.length !== 0) {
        // eslint-disable-next-line no-use-before-define
        price += getTaxOfLineItems(lineItem.childParts);
    }
    return price;
}

/**
 * Returns the price for the given array of line items. Will include sales tax if includeTax is true
 * @param {Array} lineItems array of line items to be priced
 * @returns {number} price of the lines items
 */
export function getPriceOfLineItems(lineItems) {
    return lineItems?.reduce((accumulator, lineItem) => accumulator + getPriceOfLineItem(lineItem), 0) ?? 0;
}

/**
 * Returns the sales tax for the given array of line items
 * @param {Array} lineItems array of line items to get the sales tax for
 * @returns {number} sales tax of the line items
 */
export function getTaxOfLineItems(lineItems) {
    return lineItems?.reduce((accumulator, lineItem) => accumulator + getSalesTaxOfLineItem(lineItem), 0) ?? 0;
}
