<?php
/**
 * Template part for displaying notice
 *
 * @package errigal
 * @since 1.1.0
 *
 * @param array $args {
 *     Associative array of arguments for admin notice.
 *
 *     @type string   $notice_id       Notice ID. Default empty string.
 *     @type string   $notice_title    Notice title. Default empty string.
 *     @type callable $render_callback Render callback. Default null.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Admin\Errigal_Notice as Notice;
use Errigal\Constant\{
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Template_Part as Template_Part,
};
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Notice::ID                     => '',
		Notice::TITLE                  => '',
		Template_Part::RENDER_CALLBACK => null,
	)
);

$notice_id = $args[ Notice::ID ];

if (
	! Validator::is_non_empty_string( $notice_id ) ||
	! Validator::is_non_empty_string( $args[ Notice::TITLE ] ) ||
	! Validator::is_closure( $args[ Template_Part::RENDER_CALLBACK ] )
) {
	return;
}

Html_Builder::create( $args )->create_div(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::ID         => $notice_id,
			Html_Attr::CLASS_NAME => array(
				'notice',
				'notice-info',
				'is-dismissible',
			),
		),
	)
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		$html_builder->create_div(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'errigal-notice',
				),
			)
		)->render_element(
			function ( $html_builder ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				$html_builder->create_h2(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => 'errigal-notice__title',
						),
					)
				)->render_element(
					esc_html(
						$html_builder->get_arg( Notice::TITLE )
					)
				);
				$html_builder->create_div(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => 'errigal-notice__content',
						),
					)
				)->render_element(
					$html_builder->get_arg( Template_Part::RENDER_CALLBACK )
				);
			}
		);
	}
);
