<template>
    <reviewBlock
        :headerCmsWidgetName="packageNameWidget"
        :content="displayContent" />
</template>

<script>
import settleAllPromises from '@/helpers/layout-helper';
// import baseMixin from '@/mixins/base-mixin.js';
import { useMainStore } from '@/store';
import reviewBlock from '@/layouts/payment-method/review-dropdown/review-block/review-block.vue';
import {
    getHighestFullySatisfiedTier,
    containsLineItemWithPartType
} from '@/helpers/service-package-helper.js';

export default {
    name: 'service-package-review',
    components: {
        reviewBlock
    },
    props: {
        servicePackageOptionsCmsName: String,
        defaultPackageItemsCmsName: String,
        vapsItemsCmsName: String,
        lineItems: Object,
        damage: Object
    },
    emits: ['edit-clicked'],
    data() {
        return {
            availableVaps: []
        };
    },
    computed: {
        displayContent() {
            return [...this.defaultPackageText, ...this.vapsItemText];
        },
        packageNameWidget() {
            const servicePackageNames = this.getCmsContent(
                this.servicePackageOptionsCmsName,
                'Answers'
            );

            if (!servicePackageNames) {
                return '';
            }

            const currentPackage = servicePackageNames.find((entry) => entry.Name === this.packageLevel);

            return currentPackage.SubWidgetName;
        },
        defaultPackageText() {
            const defaultItems = this.getCmsContent(this.defaultPackageItemsCmsName, 'Answers');

            if (!defaultItems) {
                return [];
            }

            return defaultItems.map((answer) => answer.Text);
        },
        vapsItemText() {
            const vapsDescriptions = this.getCmsContent(this.vapsItemsCmsName, 'Answers');

            if (!vapsDescriptions) {
                return [];
            }

            const vapsDescriptionsOnOrder = vapsDescriptions.filter((answer) =>
                containsLineItemWithPartType(answer.Name, this.vaps));

            return vapsDescriptionsOnOrder.map((answer) => answer.Text);
        },
        packageLevel() {
            return getHighestFullySatisfiedTier(
                this.glassToReplace,
                this.availableLineItems,
                this.isRepair,
                this.vaps
            );
        },
        glassToReplace() {
            return this.damage.glassToReplace;
        },
        isRepair() {
            return this.damage.isRepair;
        },
        vaps() {
            return this.lineItems?.vaps;
        },
        availableLineItems() {
            return [
                ...(this.lineItems?.glassParts ?? []),
                ...(this.lineItems?.supportingItems ?? []),
                ...(useMainStore().order.availableVaps ?? [])
            ];
        }
    }
};
</script>
