import { renderGoogleMap } from '../includes/utils/gmds-map';

function initializeMaps() {
	document.querySelectorAll('.gmds-google-map').forEach((mapElement) => {
		const canvas = mapElement.querySelector('.gmds-google-map__canvas');
		const address = mapElement.dataset.address || '';
		const apiKey = mapElement.dataset.apiKey || '';
		const zoomLevel = parseInt(mapElement.dataset.zoomLevel || '15', 10);
		const allowMouseWheelScroll = mapElement.dataset.allowMouseWheelScroll === 'on';
		const allowTouchOperations = mapElement.dataset.allowTouchOperations !== 'off';

		if (!canvas || !address || !apiKey || canvas.dataset.gmdsInitialized === 'true') {
			return;
		}

		canvas.dataset.gmdsInitialized = 'true';

		renderGoogleMap(canvas, {
			address,
			apiKey,
			zoomLevel,
			allowMouseWheelScroll,
			allowTouchOperations,
		}).catch(() => {
			canvas.dataset.gmdsInitialized = 'false';
		});
	});
}

if (document.readyState === 'loading') {
	document.addEventListener('DOMContentLoaded', () => {
		initializeMaps();
	});
} else {
	initializeMaps();
}

window.addEventListener('load', initializeMaps);
