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

import { afterDomReady } from './helpers/after-dom-ready';

import { Dropdown } from './components/dropdown';
import { MobileOverlay } from './components/mobile-overlay';
import { TableOfContents } from './modules/table-of-contents';
import { ApiTableOfContents } from './modules/table-of-contents-api';
import { ApiFilter } from './modules/api-filter';
import { MobileNav } from './modules/mobile-nav';
import { LinksPrefetch } from './modules/links-prefetch';
import { Header } from './modules/header';
import { HeaderBar } from './modules/header-bar';
import { HashLink } from './components/hash-link';
import { Sidebar } from './modules/sidebar';
import { NavTree } from './components/nav-tree';
import { ApiNavTree } from './components/api-nav-tree';
import { ApiToggle } from './components/api-toggle';
import { Tabs } from './components/tabs';
import { Breadcrumbs } from './components/breadcrumbs';
import { AlgoliaSearch } from './modules/algolia-search';
import { HeadingLink } from './components/heading-link';
import { TooltipPopover } from './components/tooltip-popover';
import { imageZoom } from './components/image-zoom';
import { Expandable } from './components/expandable';
import { CodeSwitcher } from './components/code-switcher';
import { CodeBlock } from './components/code-block';
import { FakeDevtools } from './components/fake-devtools';
import { IFrame } from './components/iframe';

import { isMobile } from './helpers/is-mobile';

import './web-components/svg-icon';
import './public-api';

const safeAttach = ( component, name ) => {
	try {
		return component.attach();
	} catch ( error ) {
		console.error( `Failed to initialize ${ name } component:`, error );
	}
};

// Ask the SOS team before touching this 😛
window.open.closed = 1;

afterDomReady( () => {
	// Initialize it faster to avoid CLS flashing when header is closed.
	safeAttach( HeaderBar, 'HeaderBar' );

	// It MUST be synchronous to avoid race conditions between snippets that use rendered components.
	safeAttach( Header, 'Header' );
	safeAttach( Dropdown, 'Dropdown' );
	safeAttach( TableOfContents, 'TableOfContents' );
	safeAttach( ApiTableOfContents, 'ApiTableOfContents' );
	safeAttach( Sidebar, 'Sidebar' );
	safeAttach( NavTree, 'NavTree' );
	safeAttach( ApiToggle, 'ApiToggle' );
	safeAttach( ApiFilter, 'ApiFilter' );
	safeAttach( MobileOverlay, 'MobileOverlay' );
	safeAttach( MobileNav, 'MobileNav' );
	safeAttach( HashLink, 'HashLink' );
	safeAttach( Tabs, 'Tabs' );
	safeAttach( AlgoliaSearch, 'AlgoliaSearch' );
	safeAttach( Breadcrumbs, 'Breadcrumbs' );
	safeAttach( HeadingLink, 'HeadingLink' );
	safeAttach( TooltipPopover, 'TooltipPopover' );
	safeAttach( Expandable, 'Expandable' );
	safeAttach( IFrame, 'IFrame' );

	// Make sure that code blocks are initialized before code switcher.
	safeAttach( CodeBlock, 'CodeBlock' );
	safeAttach( CodeSwitcher, 'CodeSwitcher' );
	safeAttach( FakeDevtools, 'FakeDevtools' );

	imageZoom();

	if ( !isMobile() ) {
		LinksPrefetch.the.init();
	}

	// Attach the ApiNavTree component when the document is fully loaded. (Safari case)
	safeAttach( ApiNavTree, 'ApiNavTree' );

	// Make sure all components related to header size are initialized.
	Header.the.dispatchHeaderResizeEvent();

	// Flush the callbacks queue that will be executed after mounting all umberto components.
	if ( window.umberto._afterReadyCallbacks ) {
		window.umberto._afterReadyCallbacks.forEach( callback => {
			callback();
		} );

		delete window.umberto._afterReadyCallbacks;
	}

	window.umberto.afterReady = callback => {
		callback();
	};
} );
