<template>
    <transition
        name="fade"
        mode="out-in">
        <div
            v-if="shouldDisplayReplaceOptionsQuestion"
            class="replace-options-question-container"
            :class="answersToDisplay.length < 2 ? 'd-none' : ''"
            aria-live="polite">
            <buttonQuestion
                v-model="selectedValues"
                isWide
                :questionText="questionText"
                :isMultiSelect="isMultiSelect"
                :answers="answersToDisplay"
                :groupName="groupName"
                buttonTypeString="listCard"
                :questionTextClasses="['replace-options-question-item']"
                :validationRules="validationRules"
                :suppressError="suppressError"
                :isRequired="isRequired" />
        </div>
    </transition>
</template>

<script>
import buttonQuestion from '@/digital-components/button-question/button-question.vue';

export default {
    name: 'replace-options-question',
    components: {
        buttonQuestion
    },
    props: {
        isAvailable: Boolean,
        filterByVehicleCategory: Boolean,
        groupName: String,
        modelValue: [Array, String, Number],
        isMultiSelect: Boolean,
        validationRules: String,
        suppressError: Boolean,
        cmsWidgetName: String,
        isRequired: Boolean
    },
    emits: ['update:modelValue'],
    data() {
        return {
            replaceOptions: this.isMultiSelect ? [] : ''
        };
    },
    computed: {
        questionText() {
            return this.getCmsContent(this.cmsWidgetName, 'QuestionText');
        },
        answersFromCms() {
            return this.getCmsContent(this.cmsWidgetName, 'Answers');
        },
        selectedValues: {
            get() {
                return this.modelValue;
            },
            set(newValue) {
                this.$emit('update:modelValue', newValue);
            }
        },
        answersToDisplay() {
            const filteredAnswers = Array.isArray(this.answersFromCms)
                ? this.answersFromCms.filter((ans) => {
                    const name = ans.Name.split('-');
                    return this.filterByVehicleCategory
                        ? name[0].toUpperCase() === this.mainStore.vehicle.category
                                && this.replaceOptions.includes(name[1])
                        : this.replaceOptions.includes(ans.Name);
                })
                : [];

            return filteredAnswers.map((ans) => {
                const newName = ans.Name.includes('-') ? ans.Name.split('-')[1] : ans.Name;
                ans.Name = newName;
                return ans;
            });
        },
        shouldDisplayReplaceOptionsQuestion() {
            return this.isAvailable && this.answersToDisplay.length > 0;
        }
    },
    watch: {
        isAvailable(val) {
            // CHECK TO UPDATE SELECTED VALUES WHEN ISAVAILABLE IS TRUE
            // eslint-disable-next-line no-unused-expressions
            val && this.updateSelectedValues();
        },
        shouldDisplayReplaceOptionsQuestion(shouldDisplayReplaceOptionsQuestion) {
            if (!shouldDisplayReplaceOptionsQuestion) {
                this.selectedValues = this.isMultiSelect ? [] : '';
            }
        }
    },
    methods: {
        initializeComponent(replaceOptions) {
            this.replaceOptions = replaceOptions;
        },
        updateSelectedValues() {
            // UPDATE SELECTEDVALUES IF ONLY ONE ANSWER
            if (Array.isArray(this.answersToDisplay) && this.answersToDisplay.length === 1) {
                this.selectedValues = this.isMultiSelect
                    ? [this.answersToDisplay[0].Name]
                    : this.answersToDisplay[0].Name;
            }
        }
    }
};
</script>

<style lang="scss" scoped>
.replace-options-question-container {
    margin-top: 1rem;
}
</style>
