import damageCustomLabels from '@/constants/damage-custom-labels';
import damageLocationsSelected from '@/constants/damage-locations-selected';
import { useMainStore } from '@/store';

export function getDamageString() {
    const mainStore = useMainStore();

    const { isRepair } = mainStore.damage;
    const damageLocations = mainStore.damage.glassToReplace;

    if (isRepair || !damageLocations || !Array.isArray(damageLocations)) {
        return '';
    }

    if (damageLocations.length > 1) {
        return damageCustomLabels.MATCH;
    }

    const { glassLocation } = damageLocations[0];
    if (glassLocation) {
        switch (glassLocation) {
            case damageLocationsSelected.WINDSHIELD:
                return damageCustomLabels.WINDSHIELD;
            case damageLocationsSelected.DRIVER:
            case damageLocationsSelected.PASSENGER:
                return damageCustomLabels.SIDE_WINDOW;
            case damageLocationsSelected.REAR:
                return damageCustomLabels.REAR_WINDOW;
            default:
                return '';
        }
    }

    // No condition is matched
    return '';
}

function hasMatchingReplacementOption(vehicleDamageOptions, selectedGlassToReplace) {
    let result = true;
    const optionsMap = {
        Windshield: 'windshieldOptions',
        Driver: 'driverSideOptions',
        Passenger: 'passengerSideOptions',
        Rear: 'backGlassOptions'
    };

    // eslint-disable-next-line no-restricted-syntax
    for (const glassToReplace of selectedGlassToReplace) {
        const propName = optionsMap[glassToReplace.glassLocation];
        const { availableReplacementOptions } = vehicleDamageOptions[propName];

        if (!availableReplacementOptions.includes(glassToReplace.glassName)) {
            result = false;
            break;
        }
    }

    return result;
}

export async function isGlassAvailableForCarId(carId) {
    const mainStore = useMainStore();

    try {
        const vehicleDamageOptionsResponse = await mainStore.getDamageOptions(carId);
        const selectedGlassToReplace = mainStore.damage.glassToReplace;

        return hasMatchingReplacementOption(vehicleDamageOptionsResponse.data, selectedGlassToReplace);
    } catch (error) {
        return false;
    }
}

/**
 * Commented code are copied directly from DigitalConsumer.FixMyGlass
 * and have not been adjusted for ISS.
 */

// import store from "@/store";
// import baseMixin from "@/mixins/base-mixin.js";
// import { storeActions } from "@/constants/store-actions";

// export function getIsWindshieldOnly() {
//     const damageLocations = store.getters.damage.glassToReplace;
//     const returnString =
//         damageLocations &&
//         damageLocations.length === 1 &&
//         damageLocations[0]?.glassLocation === "Windshield"
//             ? "windshield"
//             : "glass";
//     return returnString;
// }
