<?php
/**
 * Widgets module — sidebar / footer widget areas.
 *
 * @package Kutby
 * @since   2.5.0
 */

declare(strict_types=1);

namespace Kutby\Modules;

use Kutby\Contracts\ModuleInterface;
use Kutby\ThemeOptions\Manager;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class WidgetsModule
 */
final class WidgetsModule implements ModuleInterface {

	/**
	 * {@inheritdoc}
	 */
	public function register(): void {
		add_action( 'widgets_init', array( $this, 'register_sidebars' ) );

		/**
		 * Fires when the widgets module is ready.
		 *
		 * @since 2.5.0
		 */
		do_action( 'kutby_widgets_module_ready' );
	}

	/**
	 * {@inheritdoc}
	 */
	public function boot(): void {}

	/**
	 * Register widget areas.
	 *
	 * @return void
	 */
	public function register_sidebars(): void {
		register_sidebar(
			array(
				'name'          => 'الشريط الجانبي',
				'id'            => 'sidebar-1',
				'description'   => 'منطقة ودجات الشريط الجانبي.',
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title">',
				'after_title'   => '</h2>',
			)
		);

		$columns = (int) Manager::get( 'footer.columns', 4 );
		$columns = max( 1, min( 4, $columns ) );

		for ( $i = 1; $i <= $columns; $i++ ) {
			register_sidebar(
				array(
					'name'          => sprintf( 'عمود الفوتر %d', $i ),
					'id'            => 'footer-' . $i,
					'description'   => 'منطقة ودجات الفوتر.',
					'before_widget' => '<section id="%1$s" class="widget %2$s">',
					'after_widget'  => '</section>',
					'before_title'  => '<h2 class="widget-title">',
					'after_title'   => '</h2>',
				)
			);
		}
	}
}
