(function () {
	var selects = document.querySelectorAll('.woocommerce-ordering select');

	function resizeSelect(sel) {
		var temp = document.createElement('span');
		temp.style.visibility = 'hidden';
		temp.style.position = 'fixed';
		temp.style.whiteSpace = 'pre';
		temp.style.font = window.getComputedStyle(sel).font;

		temp.textContent = sel.options[sel.selectedIndex].text;

		document.body.appendChild(temp);

		sel.style.width = (temp.clientWidth + 20) + 'px'; // +30 for dropdown arrow
		document.body.removeChild(temp);
	}

	document.addEventListener('DOMContentLoaded', () => {
		var selects = document.querySelectorAll('.woocommerce-ordering select');

		// Initial resize
		for (var i = 0; i < selects.length; i++) {
			resizeSelect(selects[i]);
		}
	});

	// Update on change
	document.addEventListener('change', function (e) {
		if (e.target.tagName === 'SELECT' && e.target.closest('.woocommerce-ordering')) {
			resizeSelect(e.target);
		}
	});
})();