/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

export function activateDevNames() {
	const elements = Array.from( document.querySelectorAll( '.collapsing-list__dev-names > code' ) );
	const clsName = 'collapsing-list__dev-names--active';
	const lsDevName = 'umberto:devnames';

	document.addEventListener( 'keypress', evt => {
		if ( !evt.shiftKey || evt.key.toUpperCase() !== 'D' ) {
			return;
		}
		toggleDevNames( elements );
	} );

	if ( window.localStorage.getItem( lsDevName ) === 'true' ) {
		elements.forEach( element => {
			element.classList.add( clsName );
		} );
	}

	function toggleDevNames( elements ) {
		if ( window.localStorage.getItem( lsDevName ) === 'true' ) {
			elements.forEach( element => {
				element.classList.remove( clsName );
			} );
			window.localStorage.setItem( lsDevName, 'false' );
		} else {
			elements.forEach( element => {
				element.classList.add( clsName );
			} );
			window.localStorage.setItem( lsDevName, 'true' );
		}
	}
}
