import { useMainStore } from '@/store';
import { getPriceOfLineItems, getTaxOfLineItems } from '@/helpers/price-calculator';
import partNumberStrings from '@/constants/part-number-strings';
import partTypeStrings from '@/constants/part-type-strings';
import coverageType from '@/constants/coverage-type';

/**
 * Returns the lineItems object from the order
 * @param {object} order order object
 * @returns {object} lineItems object from the order
 */
export function getLineItems(order) {
    return order.lineItems;
}

/**
 * Returns an array containing only the service line items
 * @param {object} order order object
 * @returns {[]} array of service line items
 */
export function getServiceLineItems(order) {
    const { supportingItems, glassParts, otherParts } = getLineItems(order);
    return [
        ...(supportingItems ?? []),
        ...(glassParts ?? []),
        ...(otherParts ?? [])
    ];
}

/**
 * Returns an array containing only the non-service line items (Vaps and Fees)
 * @param {object} order order object
 * @returns {[]} array of non-service line items
 */
export function getNonServiceLineItems(order) {
    const { vaps, feeItems } = getLineItems(order);
    return [
        ...(vaps ?? []),
        ...(feeItems ?? [])
    ];
}

/**
 * Returns an array containing all line items
 * @param {object} order order object
 * @returns {[]} array of all line items
 */
export function getAllLineItems(order) {
    return [
        ...getServiceLineItems(order),
        ...getNonServiceLineItems(order)
    ];
}

/**
 * Returns the recycle fee line item if it exists on the order
 * @param {object} order order object
 * @returns {{}|undefined} recycle fee line item or undefined
 */
export function getRecycleFeeLineItem(order) {
    return getLineItems(order).feeItems?.find((lineItem) => lineItem.partNumber === partNumberStrings.RECYCLE_FEE);
}

/**
 * Returns the mobile fee line item if it exists on the order
 * @param {object} order order object
 * @returns {{}|undefined} mobile fee line item or undefined
 */
export function getMobileFeeLineItem(order) {
    return getLineItems(order).feeItems?.find((lineItem) => lineItem.partType === partTypeStrings.MOBILE_FEE);
}

/**
 * Returns the current deductible on the order
 * @param {object} order order object
 * @returns {number|undefined|null} current deductible
 */
export function getDeductible(order) {
    return order.damage.isRepair ? order.currentDeductible.repair : order.currentDeductible.replace;
}

/**
 * Returns if the order is unverified
 * @param {object} order order object
 * @returns {boolean} order unverified state
 */
export function isOrderUnverified(order) {
    return order.isUnverified ?? useMainStore().isUnverified;
}

/**
 * Returns if the order is NO COMP
 * @param {object} order order object
 * @returns {boolean} order NO COMP flag
 */
export function isOrderNoComp(order) {
    return order.insuranceCoverage?.coverageType === coverageType.NO_COMP ?? false;
}

/**
 * Returns if the order is ITAC
 * @param {object} order order object
 * @returns {boolean} order ITAC flag
 */
export function isOrderITAC(order) {
    return order.insuranceCoverage?.coverageType === coverageType.ITAC ?? false;
}

/**
 * Returns the subtotal for the order
 * @param {object} order order object
 * @returns {number} sub total for the order
 */
export function getSubtotal(order) {
    if (!isOrderUnverified(order) && !isOrderNoComp(order) && !isOrderITAC(order)) {
        return getDeductible(order) + getPriceOfLineItems(getNonServiceLineItems(order));
    }

    return getPriceOfLineItems(getAllLineItems(order));
}

/**
 * Returns the sales tax for the order
 * @param {object} order order object
 * @returns {number} sales tax for the order
 */
export function getSalesTax(order) {
    let tax = 0;
    if (!isOrderUnverified(order) && (isOrderITAC(order) || isOrderNoComp(order))) {
        tax += getTaxOfLineItems(getServiceLineItems(order));
    }

    tax += getTaxOfLineItems(getNonServiceLineItems(order));
    return tax;
}

/**
 * Returns the cart total rounded to two decimal places
 * @param {object} order order object
 * @returns {number} cart total for the order
 */
export function getCartTotal(order) {
    return Math.round((getSubtotal(order) + getSalesTax(order)) * 100) / 100;
}
