<?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.5.0
 */

namespace WeCodeArt\Gutenberg\Blocks\Media;

defined( 'ABSPATH' ) || exit;

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

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

	use Singleton;

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

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

	/**
	 * Block args.
	 *
	 * @param	array $current	Existing register args
	 *
	 * @return 	array
	 */
	public function block_type_args( $current ): array {
		$supports 	= get_prop( $current, [ 'supports' ], [] );

		return [
			'render_callback' 	=> [ $this, 'render' ],
			'supports' 			=> wp_parse_args( [
				'color'		=> [
					'gradients'	 => true,
					'background' => true,
					'link' 		 => true, // For Caption links.
					'text'       => true, // For SVG currentColor.
				],
				'spacing'	=> [
					'margin'  => true,
					'padding' => true,
				]
			], $supports ),
			'styles'	=> wp_list_filter( get_prop( $current, [ 'styles' ], [] ), [ 'name' => 'rounded' ], 'NOT' )
		];
	}

	/**
	 * Dynamically renders the `core/image` block.
	 *
	 * @param 	array 	$attributes	The attributes.
	 * @param 	string 	$content 	The block markup.
	 * @param 	object 	$block 		The block object.
	 *
	 * @return 	string 	The block markup.
	 */
	public function render( array $attributes = [], string $content = '', $block = null ): string {
		$html 	= render_block_core_image( $attributes, $content, $block ); // For lightbox logic.

		$dom	= $this->dom( $content );
		$div  	= wecodeart( 'dom' )::get_element( 'figure', $dom );
		$link 	= wecodeart( 'dom' )::get_element( 'a', $div );
		$img  	= wecodeart( 'dom' )::get_element( 'img', $link ?? $div );

		// If no image, use placeholder.
		if ( $img && ! $img->getAttribute( 'src' ) ) { 
			wecodeart( 'dom' )::add_classes( $div, [ 'is-placeholder' ] );

			$img->setAttribute( 'class', 'wp-block-image__placeholder' );
			$img->setAttribute( 'src', get_placeholder_source() );
			$img->setAttribute( 'alt', esc_attr__( 'Placeholder', 'wecodeart' ) );

			$html = $dom->saveHTML();
		}

		// If image is SVG, import it.
		if ( str_contains( $html, '.svg' ) ) {
			dom_image_2_svg( $dom, $link ?? $div, $img, $attributes, $html );

			$html = $dom->saveHTML();
		}

		// If lightbox is enabled, load styles.
		$lightbox = get_prop( block_core_image_get_lightbox_settings( $block->parsed_block ), [ 'enabled' ] );
		if( $lightbox ) {
			wecodeart( 'styles' )->Components->load( [ 'lightbox' ] );
		}

		return $html;
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
		return <<<CSS
			/* Block */
			:where(.wp-block-image) {
				margin: 0;
			}
			.is-layout-grid .wp-block-image :where(div,img,svg) {
				height: 100%;
				object-fit: cover;
			}
			.wp-block-image.alignfull {
				max-width: initial;
			}
			.wp-block-image[class*='align'] {
				display: table;
			}
			.wp-block-image.aligncenter {
				text-align: center;
			}
			.wp-block-image.is-placeholder {
				background-color: var(--wp--preset--color--accent);
			}
			.wp-block-image > a,
			.wp-block-image :where(img,svg) {
				border-radius: inherit;
			}
			.wp-block-image:where(.alignfull,.alignwide) :where(img,svg) {
				width: 100%;
			}
			.wp-block-image__placeholder {
				aspect-ratio: var(--wp--aspect-ratio, 16/9);
				filter: opacity(0.035);
				object-fit: fill!important;
			}
			.wp-block-image figcaption {
				font-size: var(--wp--preset--font-size--small);
				font-style: italic;
			}
		CSS;
	}
}
