export function updateQuantityAction(params) {
    const {
        entryNumber,
        quantityToUpdate,
        quantityInput,
        context,
        addQuantityDisable,
        removeQuantityDisable,
        hideLoader,
        loaderSelector,
        updateAction,
        updateQuantityOperation,
        basketData,
        isLocalEnv,
        callActionFn,
        routes,
        basketSectionSelector,
    } = params;
    console.log('case(api) to update quantity', entryNumber, quantityToUpdate);
    if (!isLocalEnv) {
        addQuantityDisable && addQuantityDisable(context);
        const payload = {
            url: `${routes.BASKET_ADD_STOCK}/${entryNumber}`,
            queries: { quantity: quantityToUpdate },
            type: 'PUT',
            complete: () => {
                removeQuantityDisable && removeQuantityDisable();
                hideLoader(loaderSelector);
            },
        };

        callActionFn(
            payload,
            updateAction,
            quantityToUpdate,
            quantityInput,
            context,
            (response) => {
                if (response.statusMessage === 'accConfirmedMsgs') {
                    $(document).trigger('EEUpdateCart', {
                        cartEntryNumber: entryNumber,
                        initialCartQuantity: response.quantity - response.quantityAdded,
                        newCartQuantity: response.quantity,
                        fulfillment: context.hasClass('charity-new')
                            ? 'Delivery'
                            : context.closest(basketSectionSelector).attr('data-type'),
                    });
                }
                context.find('.order-item__action').removeClass('disabled');
            }
        );
    } else {
        updateQuantityOperation(
            quantityToUpdate,
            quantityInput,
            context,
            basketData.updateQuantity
        );
        addQuantityDisable && addQuantityDisable(context);
        setTimeout(() => {
            removeQuantityDisable && removeQuantityDisable();
            hideLoader && hideLoader(loaderSelector);
        }, 500);
    }
}
