import {updateDiscount} from "./discounts";

const osEl = {
    $summarySection: $('.summary'),
    checkoutWidgetDetailsDiscount: '.checkout-widget__details-discount',
    checkoutWidgetDetails: '.checkout-widget__details',
    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))
            }
        }

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

    updateDiscount(data, isVoucher);
    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.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>
                    `
                })
            }
        }
    })
}

