/**
 * JOC Blockora Morph brand mark (admin + editor chrome).
 */

import { createElement } from '@wordpress/element';

const LOGO_PATH = 'assets/images/joc-blockora-morph-logo.gif';

/**
 * Absolute URL for the plugin brand logo.
 *
 * @return {string}
 */
export function brandLogoUrl() {
	if ( typeof window === 'undefined' ) {
		return '';
	}
	const cfg =
		window.jocbmAdmin ||
		window.jocbmEditor ||
		window.jocbmFront ||
		{};
	if ( cfg.logoUrl ) {
		return String( cfg.logoUrl );
	}
	const base = String( cfg.pluginUrl || '' ).replace( /\/$/, '' );
	return base ? `${ base }/${ LOGO_PATH }` : '';
}

/**
 * Gutenberg block / collection icon element (img of brand logo).
 *
 * @param {number} [size=24]
 * @return {import('@wordpress/element').WPElement|string}
 */
export function brandBlockIcon( size = 24 ) {
	const src = brandLogoUrl();
	if ( ! src ) {
		return 'layout';
	}
	return createElement( 'img', {
		src,
		alt: '',
		width: size,
		height: size,
		decoding: 'async',
		style: {
			width: size,
			height: size,
			objectFit: 'contain',
			borderRadius: 4,
			display: 'block',
		},
	} );
}
