import { useMainStore } from '@/store';

function processZipCodeResults(request) {
    return request.then((serviceZipValidationResponse) => ({
        containsMilitaryBase: serviceZipValidationResponse.containsMilitaryBase,
        isValid: serviceZipValidationResponse.isValid,
        isServiceable: serviceZipValidationResponse.isServiceable,
        city: serviceZipValidationResponse.city,
        state: serviceZipValidationResponse.state,
        zipCodeCtu: serviceZipValidationResponse.zipCodeCtu
    })).catch(() => ({
        isValid: false
    }));
}

export async function getZipCodeData(zipCode) {
    return await processZipCodeResults(useMainStore().validateZip({ zip: zipCode }));
}

export async function getMobileZipCodeData(zipCode) {
    return await processZipCodeResults(useMainStore().validateMobileZip({ zip: zipCode }));
}

export async function getPricedMobileFeePart(serviceZipCode) {
    if (!serviceZipCode) {
        return Promise.resolve(null);
    }

    // Get the Mobile Fee Part
    const mobileFeePart = await useMainStore().getMobileFeePart();

    if (mobileFeePart.data == null || mobileFeePart.data === '') {
        return Promise.resolve(null);
    }

    // Get the Mobile Fee Part Price
    const pricingResults = await useMainStore()
        .getCombinedQuote([mobileFeePart.data]);

    return Promise.resolve(pricingResults[0]);
}

export async function getServiceabilityDetails(serviceZipCode, lineItems) {
    const serviceabilityDetails = await useMainStore().getServiceabilityDetails(
        {
            serviceZipCode,
            lineItems
        },
        false
    );
    return Promise.resolve(serviceabilityDetails);
}

export async function getAvailabilityRating(
    startDate,
    endDate,
    shopAppointmentType,
    providerNumber
) {
    // For a given shop provider number and date range, get the appointment time slots available
    const shopTimeSlots = await useMainStore().getShopTimeSlots(startDate, endDate, shopAppointmentType, providerNumber);

    const numberOfDaysToEvaluate = 2;
    const isGoodAvailability =
        shopTimeSlots.data.days.filter((x) => x.timeSlots.length > 0).length
        >= numberOfDaysToEvaluate;

    const shopStatus = isGoodAvailability ? 'high' : 'low';

    return Promise.resolve(shopStatus);
}
