import { storeActions } from "@/constants/store-actions.js";
import store from "@/store";
import vehicleQuestionsMixin from "@/mixins/vehicle-questions-mixin";
import { saveSession } from "@/helpers/heritage-integration/order-helper.js";
import { experimentSettings } from "@/constants/experiments";
import { vinLookupMethodSelections } from "@/constants/vin-lookup-method-selections.js";

export default {
    computed: {
        IsEmailOptional() {
            const emailOptional = this.getSettingValue(experimentSettings.IS_EMAIL_OPTIONAL);
            return emailOptional === "true";
        },
        EmailValidationRules() {
            return this.IsEmailOptional
                ? "email-address-format"
                : "email-address-required|email-address-format";
        },
        EmailOrSmsValidationRules() {
            return this.IsEmailOptional
                ? "email-sms-format"
                : "email-sms-required|email-sms-format";
        },
    },
    methods: {
        async navigateForwardWithSingleCarMatch() {
            const pageName = this.$options?.name;

            // If we have not already saved a session, we need to save one now before the lengthy call to getPartsOrQuestions
            if (!store.getters.applicationUser.savedSessionId) {
                await saveSession({ pageNameToLog: pageName });
            }

            const result = await this.dispatchStoreAction(storeActions.GET_PARTS_OR_QUESTIONS, {
                pageNameToLog: pageName,
            });
            const partsOrQuestions = result.data.partsOrQuestions;

            vehicleQuestionsMixin.methods.navigateForward(partsOrQuestions, this);
        },
        getVinlookupMethod(vinSelection) {
            const vinLookupMethods = {
                vin: vinLookupMethodSelections.MANUALVIN,
                licensePlate: vinLookupMethodSelections.LICENSEPLATE,
                address: vinLookupMethodSelections.HOMEADDRESS,
                decline: vinLookupMethodSelections.DECLINE,
            };
            return vinLookupMethods[vinSelection] || null;
        },
        async getAndSaveBillToAccountNumber() {
            const pageName = this.$options?.name;
            const billToAccountNumber = await this.dispatchStoreActionWithLogging(
                storeActions.GET_BILL_TO_ACCOUNT_NUMBER,
                {},
                pageName,
                false
            );
            await this.dispatchStoreAction(
                this.storeActions.SAVE_BILL_TO_ACCOUNT_NUMBER,
                billToAccountNumber,
                false
            );
        },
        isPhoneNumber(input) {
            const phoneRegex = /^\(?(\d{3})\)?[-. ]?(\d{3})[-. ]?(\d{4})$/;
            return phoneRegex.test(input);
        },
    },
};
