<?php
/**
 * Template part for displaying notice content
 *
 * @package errigal
 * @since 1.1.0
 *
 * @param array $args Associative array of arguments for admin notice content sections.
 */

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_Shared_Class as Shared_Class,
};
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

$sections = $args ?? array();

if ( ! Validator::is_associative_array( $sections ) ) {
	return;
}

$html_builder       = Html_Builder::create();
$is_primary_section = true;

foreach ( $sections as $section => $items ) {
	if (
		! Validator::is_non_empty_string( $section ) ||
		! Validator::is_non_empty_array( $items )
	) {
		continue;
	}

	$section_heading = Notice::get_section_heading( $section );

	if ( Validator::is_non_empty_string( $section_heading ) ) {
		$html_builder->create_h3(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'errigal-notice__heading',
				),
			)
		)->render_element(
			esc_html( $section_heading )
		);
	}

	$html_builder->create_div(
		array(
			Html_Option::ATTRS => array(
				Html_Attr::CLASS_NAME => array(
					'errigal-notice__items',
					Validator::is_true( $is_primary_section )
						? null
						: Shared_Class::SECONDARY,
				),
			),
		)
	)->render_element(
		function () use ( $items ) {
			foreach ( $items as $item_args ) {
				if ( ! Validator::is_associative_array( $item_args ) ) {
					continue;
				}

				get_template_part(
					'template-parts/component/admin/notice/item',
					null,
					$item_args,
				);
			}
		}
	);

	if ( Validator::is_true( $is_primary_section ) ) {
		$is_primary_section = false;
	}
}
