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

export async function getPricedMobileFeePart(serviceZipCode, pageNameToLog) {
    if (!serviceZipCode) {
        return null;
    }

    const zipCodeData = await getZipCodeData(serviceZipCode);

    // Get the Mobile Fee Part
    const mobileFeePart = await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_MOBILE_FEE_PART,
        {
            serviceZipCode: serviceZipCode,
            serviceZipCodeCtu: zipCodeData.zipCodeCtu,
        },
        pageNameToLog,
        false
    );

    if (
        mobileFeePart.data === null ||
        mobileFeePart.data === undefined ||
        mobileFeePart.data === ""
    ) {
        return null;
    }

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

    return pricingResults[0];
}

export async function getPricedRecycleFeePart(serviceZipCode, pageNameToLog) {
    if (!serviceZipCode) {
        return null;
    }

    const zipCodeData = await getZipCodeData(serviceZipCode);

    // Get supporting items
    const supportingItems = await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_SUPPORTING_ITEMS,
        null,
        pageNameToLog,
        false
    );

    if (
        supportingItems.data === null ||
        supportingItems.data === undefined ||
        supportingItems.data === ""
    ) {
        return null;
    }

    const recycleFeePart = supportingItems.data.find(
        (item) => item.partNumber === partNumberStrings.RECYCLE_FEE
    );

    if (recycleFeePart) {
        // Get the Recycle Fee Part Price
        const pricingResults = await baseMixin.methods.dispatchStoreActionWithLogging(
            storeActions.PRICE_ORDER_ITEMS_AND_SAVE_SERVER_DATA,
            {
                availableLineItems: [recycleFeePart],
                serviceZipCode: serviceZipCode,
                serviceZipCodeCtu: zipCodeData.zipCodeCtu,
            },
            pageNameToLog,
            false
        );

        return pricingResults[0];
    }
}

export async function getServiceabilityDetails(serviceZipCode, lineItems, pageNameToLog) {
    // Get the Mobile Fee Part
    return await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_SERVICEABILITY_DETAILS,
        {
            serviceZipCode: serviceZipCode,
            lineItems: lineItems,
        },
        pageNameToLog,
        false
    );
}

export async function getBillToAccountNumber(providerNumber) {
    // Get the BillTo Account Number
    return await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_BILL_TO_ACCOUNT_NUMBER,
        { providerNumber: providerNumber },
        "service-location",
        false
    );
}

export async function getShopProviderData(serviceZipCode) {
    return await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_PROVIDERS,
        {
            serviceZipCode: serviceZipCode,
        },
        "service-location"
    );
}

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.dispatchStoreActionWithLogging(
        storeActions.GET_SHOP_TIME_SLOTS,
        {
            providerNumber: providerNumber,
            startDate: startDate,
            endDate: endDate,
            shopAppointmentType: shopAppointmentType,
        },
        "service-location",
        false
    );

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

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

    return shopStatus;
}

export async function getClosestApplicableShops(serviceZipCode, carId, pageNameToLog) {
    if (!serviceZipCode) {
        return null;
    }

    const closestShops = await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_CLOSEST_APPLICABLE_SHOPS,
        {
            zip: serviceZipCode,
            carId: carId,
        },
        pageNameToLog
    );

    return closestShops.data;
}

export async function getZipCodeData(serviceZipCode) {
    return await baseMixin.methods.getZipCodeData(serviceZipCode, "service-location");
}
