<template>
    <reviewBlock
        :headerCmsWidgetName="packageNameWidget"
        :content="displayContent"
        @edit-clicked="editClicked" />
</template>

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

export default {
    name: "service-package-review",
    props: {
        servicePackageOptionsCmsName: String,
        defaultPackageItemsCmsName: String,
        vapsItemsCmsName: String,
        lineItems: Object,
        damage: Object,
    },
    data() {
        return {
            availableVaps: [],
        };
    },
    methods: {
        loadInitialData() {
            const wipersPromise = baseMixin.methods.dispatchStoreActionWithLogging(
                storeActions.GET_WIPERS,
                {
                    serviceZipCode: store.getters.order.serviceLocation.zipCode,
                    carId: store.getters.vehicle.carId,
                },
                "review"
            );
            const rainDefensePromise = baseMixin.methods.dispatchStoreActionWithLogging(
                storeActions.GET_RAIN_DEFENSE,
                null,
                "review"
            );

            const promiseResultMap = [
                {
                    resultKey: "wipers",
                    promise: wipersPromise,
                },
                {
                    resultKey: "rainDefense",
                    promise: rainDefensePromise,
                },
            ];

            return settleAllPromises(promiseResultMap);
        },
        initializeComponent(apiResponses) {
            const vapsFromApi = [...apiResponses.wipers, apiResponses.rainDefense];

            this.availableVaps = vapsFromApi;
        },
        editClicked() {
            this.$emit("edit-clicked");
        },
    },
    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 ?? []),
                ...this.availableVaps,
            ];
        },
    },
    components: {
        reviewBlock,
    },
};
</script>
