var Wick = Wick || {};

Wick.RepaymentToggle = (function () {
    const SELECTORS = {
        repaymentRadio: 'input[name="repaymentTerms"]',
        term: '.billie-info .term',
        paymentMethod: '[name="payment-method"]',
    };

    function updateTerm(value) {
        const $term = $(SELECTORS.term);
        if (!$term.length) return;

        $term.text(value);
    }

    function bindEvents() {
        $(document).on('change', SELECTORS.repaymentRadio, function () {
            const value = $(this).val();
            if (!value) return;

            updateTerm(value);

            const currentMethod = $(SELECTORS.paymentMethod + ':checked').val();
            if (currentMethod !== 'billie') return;

            if (typeof Wick.showPaymentLoader === 'function') {
                Wick.showPaymentLoader('billie');
            }
        });
    }

    bindEvents();
})();
