const osEl = {
    $discountList: $('.checkout-widget__details-discount ul'),
    $summarySection: $('.summary'),
    checkoutWidgetDetailsDiscount: '.checkout-widget__details-discount',
    checkoutWidgetDetails: '.checkout-widget__details',
    discountItem: '.checkout-widget__details-discount',
    delivery: '.checkout-widget__item-delivery',
    cc: '.checkout-widget__item-cc',
    itemFree: '.checkout-widget__item-free',
    itemDeficit: '.checkout-widget__item-deficit',
    summaryDelivery: '.checkout-widget__item-delivery',
}


export function updateOrderSummary(data, equalMap, isVoucher) {
    equalMap.forEach((obj) => {
        if ($(obj.item).length) {
            $(obj.item).show();
        } else {
            if ($(osEl.checkoutWidgetDetailsDiscount).length) {
                $(osEl.checkoutWidgetDetailsDiscount).before(createCheckoutWidgetItem(obj))
            } else {
                $(osEl.checkoutWidgetDetails).append(createCheckoutWidgetItem(obj))
            }
        }

        if (isVoucher) {
            obj.dataValue
                ? ($(obj.item).find(obj.itemValue).show(), $(obj.item).find(obj.itemValue).text(obj.dataValue))
                : $(obj.item).hide();
        } else {
            obj.value
                ? ($(obj.item).find(obj.itemValue).show(), $(obj.item).find(obj.itemValue).text(obj.dataValue))
                : $(obj.item).hide();
        }
    })

    if ((Array.isArray(data.appliedProductPromotions) && data.appliedProductPromotions.length)
        || (Array.isArray(data.appliedOrderPromotions) && data.appliedOrderPromotions.length)) {
        $(osEl.discountItem).show();
        osEl.$discountList.empty();

        !data.appliedOrderPromotions.length || data.appliedOrderPromotions.forEach(function (discountObj) {
            if (discountObj.usedCouponCode && discountObj.description) {
                osEl.$discountList.append(function () {
                    return `
                            <li data-coupon-code="${discountObj.usedCouponCode}">
                                ${discountObj.description}
                                <span class="icon checkout-widget__voucher-remove">
                                    <span class="fas fa-times"></span>
                                </span>
                            </li>
                        `
                });
            }
        });

        !data.appliedProductPromotions || data.appliedProductPromotions.forEach(function (discountObj) {
            if (discountObj.promotionData.description) {
                osEl.$discountList.append(function () {
                    return `
                            <li>${discountObj.promotionData.description}</li>
                        `
                });
            }
        });
    } else if (isVoucher && (Array.isArray(data.promotionsInfo) && data.promotionsInfo.length)) {
        $(osEl.discountItem).show();
        osEl.$discountList.empty();

        data.promotionsInfo.forEach(function (discountItem) {
            osEl.$discountList.append(function () {
                if (discountItem.usedCouponCode && discountItem.description) {
                    return `
                            <li data-coupon-code="${discountItem.usedCouponCode}">
                                ${discountItem.description}
                                <span class="icon checkout-widget__voucher-remove">
                                    <span class="fas fa-times"></span>
                                </span>
                            </li>
                        `
                } else if (discountItem.description) {
                    return `
                            <li>${discountItem.description}</li>
                        `
                } else {
                    return ''
                }

            });
        });
    } else {
        $(osEl.discountItem).hide();
    }


    hideUnusedDeliveryTypeCost(data, isVoucher);
    calculateFreeDelivery(data);
}

function createCheckoutWidgetItem(obj) {
    return `
            <li class="checkout-widget__item ${obj.item.substr(1)}">
                <span>${obj.title}</span>
                <span class="checkout-widget__item-value">${obj.dataValue}</span>
            </li>
        `
}

function hideUnusedDeliveryTypeCost(data, isVoucher) {
    $(osEl.delivery).show()
    $(osEl.cc).show()
    if (isVoucher) {
        if (!data.deliveryOrder) {
            $(osEl.delivery).hide()
        } else if (!data.clickAndCollectOrder) {
            $(osEl.cc).hide()
        }
    } else {
        if (data.clickAndCollectOnly) {
            $(osEl.delivery).hide()
        } else if (data.deliveryItemsQuantity > 0 && data.pickupItemsQuantity === 0) {
            $(osEl.cc).hide()
        }
    }
}

function calculateFreeDelivery(data) {
    osEl.$summarySection.each(function () {
        let $free = $(this).find(osEl.itemFree);
        let $deficit = $(this).find(osEl.itemDeficit);

        !$free.length || $free.remove();
        !$deficit.length || $deficit.remove()

        if (!data.clickAndCollectOnly) {
            if (!data.clickAndCollectOrder) {
                if (data.freeDelivery && data.messageForFreeDelivery) {
                    $(this).find(osEl.summaryDelivery).before(function () {
                        return `
                        <div class="checkout-widget__item checkout-widget__item-free">
                            <span>
                               ${data.messageForFreeDelivery}
                            </span>
                        </div>
                    `
                    })
                    $(this).find(osEl.summaryDelivery).hide();
                } else if (!data.freeDelivery && data.messageForFreeDelivery) {
                    $(this).find(osEl.summaryDelivery).before(function () {
                        return `
                        <div class="checkout-widget__item checkout-widget__item-deficit">
                            ${data.messageForFreeDelivery}
                        </div>
                    `
                    })
                }
            }
        }
    })
}
