<?php
/**
 * Echo the widget area and widget loop structural markup. It also calls the widget area and widget loop
 * action hooks.
 *
 * @package SplendorStudio\Framework\Templates\Structure
 *
 * @since   1.0.0
 */

// This includes everything added to wp hooks before the widgets.
echo fancify_lite_get_widget_area( 'before_widgets' ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped -- Widget area has to be echoed.

	// phpcs:disable Generic.WhiteSpace.ScopeIndent -- Code structure mirrors HTML markup.
	if ( 'grid' === fancify_lite_get_widget_area( 'fancify_lite_type' ) ) {
		fancify_lite_open_markup_e(
			'fancify_lite_widget_area_grid' . fancify_lite_widget_area_subfilters(),
			'div',
			array(
				'class'               		=> 'fancify-grid',
				'data-fancify-grid-margin' 	=> '',
			)
		);
	}

	if ( 'offcanvas' === fancify_lite_get_widget_area( 'fancify_lite_type' ) ) {

		fancify_lite_open_markup_e(
			'fancify_lite_widget_area_offcanvas_wrap' . fancify_lite_widget_area_subfilters(),
			'div',
			array(
				'id'    => esc_attr( fancify_lite_get_widget_area( 'id' ) ),
				'class' => 'fancify-offcanvas',
			)
		);

			fancify_lite_open_markup_e( 'fancify_lite_widget_area_offcanvas_bar' . fancify_lite_widget_area_subfilters(), 'div', array( 'class' => 'fancify-offcanvas-bar' ) );
	}

		// Widgets.
		if ( fancify_lite_have_widgets() ) :

			/**
			 * Fires before widgets loop.
			 *
			 * This hook only fires if widgets exist.
			 *
			 * @since 1.0.0
			 */
			do_action( 'fancify_lite_before_widgets_loop' );

				while ( fancify_lite_have_widgets() ) :
					fancify_lite_setup_widget();

					if ( 'grid' === fancify_lite_get_widget_area( 'fancify_lite_type' ) ) {
						fancify_lite_open_markup_e( 'fancify_lite_widget_grid' . fancify_lite_widget_subfilters(), 'div', fancify_lite_widget_shortcodes( 'class=fancify-width-medium-1-{count}' ) );
					}

						fancify_lite_open_markup_e( 'fancify_lite_widget_panel' . fancify_lite_widget_subfilters(), 'div', fancify_lite_widget_shortcodes( 'class=fancify-widget-container widget_{type} {id}' ) );

							/**
							 * Fires in each widget panel structural HTML.
							 *
							 * @since 1.0.0
							 */
							do_action( 'fancify_lite_widget' );

						fancify_lite_close_markup_e( 'fancify_lite_widget_panel' . fancify_lite_widget_subfilters(), 'div' );

					if ( 'grid' === fancify_lite_get_widget_area( 'fancify_lite_type' ) ) {
						fancify_lite_close_markup_e( 'fancify_lite_widget_grid' . fancify_lite_widget_subfilters(), 'div' );
					}
				endwhile;

			/**
			 * Fires after the widgets loop.
			 *
			 * This hook only fires if widgets exist.
			 *
			 * @since 1.0.0
			 */
			do_action( 'fancify_lite_after_widgets_loop' );
		else :

			/**
			 * Fires if no widgets exist.
			 *
			 * @since 1.0.0
			 */
			do_action( 'fancify_lite_no_widget' );
		endif;

	if ( 'offcanvas' === fancify_lite_get_widget_area( 'fancify_lite_type' ) ) {

			fancify_lite_close_markup_e( 'fancify_lite_widget_area_offcanvas_bar' . fancify_lite_widget_area_subfilters(), 'div' );

		fancify_lite_close_markup_e( 'fancify_lite_widget_area_offcanvas_wrap' . fancify_lite_widget_area_subfilters(), 'div' );
	}

	if ( 'grid' === fancify_lite_get_widget_area( 'fancify_lite_type' ) ) {
		fancify_lite_close_markup_e( 'fancify_lite_widget_area_grid' . fancify_lite_widget_area_subfilters(), 'div' );
	}

// This includes everything added to wp hooks after the widgets.
echo fancify_lite_get_widget_area( 'after_widgets' ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped -- Widget area has to be echoed.

// phpcs:enable Generic.WhiteSpace.ScopeIndent -- Code structure mirrors HTML markup.
