/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Osc
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

define(
    [
        'jquery',
        'Magento_Checkout/js/model/quote',
        'Mageplaza_Osc/js/model/resource-url-manager',
        'mage/storage',
        'Magento_Checkout/js/model/error-processor',
        'Magento_Customer/js/model/customer',
        'Magento_Checkout/js/model/payment/method-converter',
        'Magento_Checkout/js/model/payment-service',
        'Magento_Checkout/js/model/shipping-service',
        'Mageplaza_Osc/js/model/osc-loader',
        'uiRegistry'
    ],
    function ($,
              quote,
              resourceUrlManager,
              storage,
              errorProcessor,
              customer,
              methodConverter,
              paymentService,
              shippingService,
              oscLoader,
              registry) {
        'use strict';
        var itemUpdateLoader = ['total',],
            loadingSpeedConfig = window.loadingSpeedConfig;
        return function () {
            if (loadingSpeedConfig && loadingSpeedConfig.refresh_page !== '1' &&
                loadingSpeedConfig.product_change.includes("1")) {
                itemUpdateLoader.push('shipping');
            }
            oscLoader.startLoader(itemUpdateLoader);

            return storage.post(
                resourceUrlManager.getUrlForUpdatePaymentTotalInformation(quote)
            ).done(
                function (response) {
                    var options, paths ,
                        thumbnailComponent = registry.get('checkout.sidebar.summary.cart_items.details.thumbnail');

                    if (response.redirect_url) {
                        window.location.href = response.redirect_url;
                        return;
                    }

                    // remove downloadable options on cart item reload
                    $('#downloadable-links-list').remove();
                    $('#links-advice-container').remove();

                    if (response.image_data && thumbnailComponent) {
                        thumbnailComponent.imageData = JSON.parse(response.image_data);
                    }

                    if (response.options) {
                        options = JSON.parse(response.options);

                        response.totals.items.forEach(function (item) {
                            item.mposc = options[item.item_id];
                        });
                    }

                    if (response.request_path) {
                        paths = JSON.parse(response.request_path);

                        response.totals.items.forEach(function (item) {
                            item.request_path = paths[item.item_id];
                        });
                    }

                    quote.setTotals(response.totals);
                    paymentService.setPaymentMethods(methodConverter(response.payment_methods));
                }
            ).fail(
                function (response) {
                    errorProcessor.process(response);
                }
            ).always(
                function () {
                    oscLoader.stopLoader(itemUpdateLoader);
                }
            );
        };
    }
);
