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

/**
 * Appends a `<link>` element to the `<head>` to preload a resource.
 *
 * 	* It should detect if the resource is already preloaded.
 * 	* It should detect type of the resource and set the `as` attribute accordingly.
 *
 * @param url The URL of the resource to preload.
 * @param props Additional properties for the preload.
 * @param props.attributes Additional attributes to be set on the `<link>` element.
 */
export function injectResourcePreload( url, { attributes } = {} ) {
	if ( document.head.querySelector( `link[href="${ url }"][rel="preload"]` ) ) {
		return;
	}

	const link = document.createElement( 'link' );

	// Set additional attributes if provided.
	for ( const [ key, value ] of Object.entries( attributes || {} ) ) {
		link.setAttribute( key, value );
	}

	link.setAttribute( 'data-injected-by', 'ckeditor-docs' );

	link.rel = 'preload';
	link.as = detectTypeOfResource( url );
	link.href = url;

	document.head.insertBefore( link, document.head.firstChild );
}

/**
 * Detects the type of the resource based on its URL.
 */
function detectTypeOfResource( url ) {
	switch ( true ) {
		case /\.css$/.test( url ):
			return 'style';

		case /\.js$/.test( url ):
			return 'script';

		default:
			return 'fetch';
	}
}
