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

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

    const zipCodeData = await baseMixin.methods.getZipCodeData(serviceZipCode);

    // Get the Mobile Fee Part
    const mobileFeePart = await baseMixin.methods.dispatchStoreAction(
        storeActions.GET_MOBILE_FEE_PART,
        null,
        false
    );

    // Get the Mobile Fee Part Price
    const pricingResults = await baseMixin.methods.dispatchStoreAction(
        storeActions.PRICE_ORDER_ITEMS_AND_SAVE_SERVER_DATA,
        {
            availableLineItems: [mobileFeePart.data],
            serviceZipCode: serviceZipCode,
            serviceZipCodeCtu: zipCodeData.zipCodeCtu,
        },
        false
    );

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

export async function getServiceabilityDetails(serviceZipCode, lineItems) {
    // Get the Mobile Fee Part
    const serviceabilityDetails = await baseMixin.methods.dispatchStoreAction(
        storeActions.GET_SERVICEABILITY_DETAILS,
        {
            serviceZipCode: serviceZipCode,
            lineItems: 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 baseMixin.methods.dispatchStoreAction(
        storeActions.GET_SHOP_TIME_SLOTS,
        {
            providerNumber: providerNumber,
            startDate: startDate,
            endDate: endDate,
            shopAppointmentType: shopAppointmentType,
        },
        false
    );

    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);
}
