import bailoutCode from '@/constants/bailoutCode';

function getItemData(data) {
    if (data === undefined || data == null) {
        return 'null';
    }

    if (typeof data === 'string') {
        return data;
    }

    return JSON.stringify(data);
}

const bailoutMessage = Object.freeze({
    unknown: (error) => ({
        code: bailoutCode.Unknown,
        message: `An unknown bailout occurred: ${getItemData(error)}`
    }),
    saveSessionError: (error) => ({
        code: bailoutCode.SaveSessionError,
        message: `An error occurred during save session: ${getItemData(error)}`
    }),
    vehicleNotFound: (vin) => ({
        code: bailoutCode.VehicleNotFound,
        message: `Failed to find vehicle in system with vin: ${vin}`
    }),
    vehicleVinLookupError: (vin, error) => ({
        code: bailoutCode.VehicleVinLookupError,
        message: `An error occurred looking up Vin: ${vin}. Error: ${getItemData(error)}`
    }),
    coverageStatementInvalidState: () => ({
        code: bailoutCode.CoverageStatementInvalidState,
        message: 'Coverage Statement has entered an invalid state'
    }),
    doNotSeeMyShop: () => ({
        code: bailoutCode.DoNotSeeMyShop,
        message: 'User does not see their shop.'
    }),
    pricingResponseError: (lineItems, error) => ({
        code: bailoutCode.PricingResponseError,
        message: `An error occurred in getPriceOrderItems. Line Items: ${getItemData(lineItems)} Error: ${getItemData(error)}`
    }),
    TPANotEnabled: () => ({
        code: bailoutCode.TPANotEnabled,
        message: 'User selected TPA when TPA is not enabled for this client'
    }),
    RequestCallback: () => ({
        code: bailoutCode.RequestCallback,
        message: 'User selected option to receive callback from Safelite'
    }),
    HeavyTruckVehicle: (carId) => ({
        code: bailoutCode.HeavyTruckVehicle,
        message: `User selected a heavy truck vehicle. Car ID: ${carId}`
    }),
    NoPartsAvailable: () => ({
        code: bailoutCode.NoPartsAvailable,
        message: 'No parts were returned'
    }),
    PartsServiceError: (error) => ({
        code: bailoutCode.PartsServiceError,
        message: `An error occurred in getPartsOrQuestions. Error: ${getItemData(error)}`
    }),
    SafeliteNotTheProvider: () => ({
        code: bailoutCode.SafeliteNotTheProvider,
        message: 'User selected to continue a referral where a TPA shop was previously selected.'
    }),
    vehicleYMMSLookupError: (year, make, model, style, error) => {
        const baseMessage = 'An error occurred looking up vehicle';
        const errorMessage = `Error: ${getItemData(error)}`;

        const getMessage = (year, make, model, style) => {
            if (!year) return `${baseMessage} years. ${errorMessage}`;
            if (!make) return `${baseMessage} makes for Year: ${year}. ${errorMessage}`;
            if (!model) return `${baseMessage} models for Year: ${year}, Make: ${make}. ${errorMessage}`;
            if (!style) return `${baseMessage} styles for Year: ${year}, Make: ${make}, Model: ${model}. ${errorMessage}`;
            return `${baseMessage} with Year: ${year}, Make: ${make}, Model: ${model}, Style: ${style}. ${errorMessage}`;
        };

        return {
            code: bailoutCode.VehicleYMMSLookupError,
            message: getMessage(year, make, model, style)
        };
    }
});

export default bailoutMessage;
