/**
 * Ported from app/snippets/form-counter.js
 */
(function($, window) {
    const Classes = {
        counter	: 'form-counter'
    };

    const Data = {
        up		: 'data-count-up',
        down	: 'data-count-down'
    };


    const proxy = function(fn, ...args) {
        return function() {
            fn.call(this, ...args);
        };
    };

    const updateCount = function(increase = false) {
        let $input = $(this).parents(`.${Classes.counter}`).find('.form-control');
        let input = $input.get(0);
        let control = $(this).closest('.custom-control').find('input.custom-control-input').get(0);
        let stepDirection = increase ? 'stepUp' : 'stepDown';

        try {
            input[stepDirection]();
            if (control != null && control.disabled == false) {
                control.checked = true;
            }
            $input.trigger('change');
        } catch(e) {
            let step = input.step > 0 ? parseFloat(input.step) : 1;

            let newValue = increase ? parseFloat(input.value) + step : parseFloat(input.value) - step;

            if(newValue > parseFloat(input.max)) return;
            if(newValue < parseFloat(input.min)) return;

            input.value = (newValue).toFixed($input.data('decimals'));
        }
    };

    window.initNumberInput = function () {
        $(`[${Data.up}]`).off('click.numberInputUp').on('click.numberInputUp', proxy(updateCount, true));
        $(`[${Data.down}]`).off('click.numberInputDown').on('click.numberInputDown', proxy(updateCount));
        $(`.${Classes.counter} .form-control`).off('blur.numberInput').on('blur.numberInput', function() {
            let min = parseInt(this.min);
            let max = parseInt(this.max);
            let value = parseInt(this.value);
            let control = $(this).closest('.custom-control').find('input.custom-control-input').get(0);

            if (control != null && control.disabled == false) {
                control.checked = true;
            }
            if(value < min) this.value = min;
            if(value > max) this.value = max;
        });
    };
})(jQuery, window);