<?php
/**
 * Template part for displaying image in widget
 *
 * @package errigal
 * @since 1.0.3
 *
 * @param array $args {
 *     Associative array of arguments for image in widget.
 *
 *     @type string $image_class Image class. Default empty string.
 *     @type string $image_url   Image URL. Default empty string.
 *     @type string $image_alt   Optional. Image alternate text. Default empty string.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};
use Errigal\Widget\Errigal_Widget_Base as Widget;

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Widget::IMAGE_CLASS => '',
		Widget::IMAGE_URL   => '',
		Widget::IMAGE_ALT   => '',
	)
);

$image_class = $args[ Widget::IMAGE_CLASS ];
$image_url   = $args[ Widget::IMAGE_URL ];
$image_alt   = $args[ Widget::IMAGE_ALT ];

if (
	! Validator::is_non_empty_string( $image_class ) ||
	! Validator::is_non_empty_string( $image_alt )
) {
	return;
}

$html_builder = Html_Builder::create();

if ( Content::is_ie() ) {
	$html_builder->create_div(
		array(
			Html_Option::ATTRS => array(
				Html_Attr::CLASS_NAME => $image_class,
				Html_Attr::STYLE      => 'background-image: url(' . esc_url( $image_url ) . ')',
				Html_Attr::ROLE       => Html_Attr::VALUE_IMG,
				Html_Attr::ARIA_LABEL => $image_alt,
			),
		)
	)->render_element();

	return;
}

$html_builder->create_img(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::SRC        => $image_url,
			Html_Attr::ALT        => $image_alt,
			Html_Attr::CLASS_NAME => $image_class,
		),
	)
)->render_element();
