/**
 * Frontend viewer bootstrap.
 */
import PanoramaViewer from './viewer';
import './viewer.scss';

const init = () => {
	document
		.querySelectorAll( '.vist360-viewer[data-vist360]' )
		.forEach( ( node ) => {
			if ( node.dataset.vist360Ready ) {
				return;
			}
			node.dataset.vist360Ready = '1';

			let config;
			try {
				config = JSON.parse( node.dataset.vist360 );
			} catch {
				return;
			}

			try {
				new PanoramaViewer( node, config );
			} catch {
				node.textContent = config.i18n?.webglError || '';
			}
		} );
};

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