import {makeArray} from '@springernature/elements/themes/springernature/js/helpers/index';

function addInputListeners(inputs, submitButton) {
	inputs.forEach(input => {
		input.addEventListener('change', () => {
			submitButton.click();
		});
	});
}

function natureSortBy() {
	const forms = makeArray(document.querySelectorAll('[data-sort-by]'));
	forms.forEach(form => {
		const radioInputs = makeArray(form.querySelectorAll('[data-sort-by-input]'));
		const submitButton = form.querySelector('[data-sort-by-button]');
		addInputListeners(radioInputs, submitButton);
	});
}

export {natureSortBy};
