<?php
/**
 * WeCodeArt Framework
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package		WeCodeArt Framework
 * @subpackage  Gutenberg\Blocks
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		5.0.0
 * @version		6.6.2
 */

namespace WeCodeArt\Gutenberg\Blocks\Text;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Singleton;
use WeCodeArt\Gutenberg\Blocks\Dynamic;
use function WeCodeArt\Functions\{ get_prop, toJSON };

/**
 * Gutenberg Code block.
 */
class Code extends Dynamic {

	use Singleton;

	/**
	 * Block namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'core';

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'code';

	/**
	 * Send to Constructor
	 */
	public function init() {
		add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
	}

	/**
	 * Block args.
	 *
	 * @param	array $current	Existing register args
	 *
	 * @return 	array
	 */
	public function block_type_args( array $current ): array {
		$render_fn	= fn( array $attrs = [], string $content = '' ): string => preg_replace( "/<br\W*?\/?>/", "\n", $content );

		return [
			'render_callback' => $render_fn,
		];
	}

	/**
	 * Has syntax highlighter
	 *
	 * @since	6.3.7
	 * @version	6.3.7
	 */
	public function has_syntax_highlight() {
		$assets = wecodeart_config( 'paths' )['uri'] . '/assets/cache';

		return apply_filters( 'wecodeart/filter/gutenberg/block/code/hljs', [
			$assets . '/highlight.min.js',
			$assets . '/highlight-ln.min.js',
			$assets . '/vs2015.min.css',
		] );
	}
	
	/**
	 * Enqueue Front-End Assets
	 *
	 * @since	6.3.7
	 * @version	6.3.7
	 */
	public function register_assets(): void {
		$highlight_js = $this->has_syntax_highlight();

		if( ! $highlight_js || ! is_array( $highlight_js ) ) {
			return;
		}

		$highlight_js = toJSON( $highlight_js );

		wecodeart( 'assets' )->add_script( 'wecodeart-block-code', [
			'deps'	 => [ 'wecodeart-support-assets' ],
			'load'	 => fn( $blocks ) => in_array( $this->get_block_type(), $blocks, true ),
			'inline' => <<<JS
				(function( wecodeart ) {
					const { Selector, fn: { loadJs } } = wecodeart;
				
					loadJs( $highlight_js, 'highlightjs', { async: false } );

					loadJs.ready( 'highlightjs', () => {
						hljs.configure({ ignoreUnescapedHTML: true });
						
						Selector.find( 'pre.wp-block-code:not(.ignore) code' ).forEach(el => {
							hljs.highlightElement(el);
							hljs.lineNumbersBlock(el);
						});
					});
				}).apply( this, [ window.wecodeart ] );
			JS,
		] );
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
		return <<<CSS
			pre {
				overflow: auto;
				color: var(--wp--pink);
			}
			pre code {
				font-size: inherit;
				color: inherit;
				word-break: normal;
			}
			code {
				font-size: var(--wp--preset--font-size--small);
				color: var(--wp--pink);
				word-wrap: break-word;
			}
			a > code {
				color: inherit;
			}
			.wp-block-code code {
				display: block;
				overflow-wrap: break-word;
				white-space: pre-wrap;
				padding: 1rem;
			}
			.wp-block-code .hljs-ln-numbers {
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
				text-align: center;
				color: #ccc;
				border-right: 1px solid #ccc;
				vertical-align: top;
				padding-right: 5px;
			}
			.wp-block-code .hljs-ln-code {
				padding-left: 10px;
			}
		CSS;
	}
}
