<?php
/**
 * Footer module — registry, schema labels, body class.
 *
 * @package Kutby
 * @since   2.5.0
 */

declare(strict_types=1);

namespace Kutby\Modules;

use Kutby\Contracts\ModuleInterface;
use Kutby\Footer\Layouts\ClassicFooter;
use Kutby\Footer\Layouts\MinimalFooter;
use Kutby\Footer\Layouts\ModernFooter;
use Kutby\Footer\Services\FooterRegistry;
use Kutby\Footer\Support\FooterContext;

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

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

	/**
	 * {@inheritdoc}
	 */
	public function register(): void {
		$registry = FooterRegistry::instance();
		$registry->add( 'classic', ClassicFooter::class );
		$registry->add( 'modern', ModernFooter::class );
		$registry->add( 'minimal', MinimalFooter::class );

		/**
		 * Register additional footer layouts.
		 *
		 * @since 2.7.0
		 *
		 * @param FooterRegistry $registry Registry.
		 */
		do_action( 'kutby_footer_register_layouts', $registry );

		add_filter( 'kutby_options_schema', array( $this, 'filter_layout_options' ) );
		add_filter( 'body_class', array( $this, 'body_classes' ) );

		/**
		 * Footer module ready (layouts registered).
		 *
		 * @since 2.5.0
		 */
		do_action( 'kutby_footer_module_ready' );
	}

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

	/**
	 * Footer layout body class.
	 *
	 * @param array<int, string> $classes Classes.
	 * @return array<int, string>
	 */
	public function body_classes( array $classes ): array {
		$layout = FooterContext::from_request()->layout_id();
		if ( FooterRegistry::instance()->has( $layout ) ) {
			$classes[] = 'kutby-footer-layout-' . sanitize_html_class( $layout );
		}

		return $classes;
	}

	/**
	 * Sync layout select options from Registry labels.
	 *
	 * @param array<string, array<string, array<string, mixed>>> $schema Schema.
	 * @return array<string, array<string, array<string, mixed>>>
	 */
	public function filter_layout_options( array $schema ): array {
		$labels = FooterRegistry::instance()->labels();
		if ( empty( $labels ) || empty( $schema['footer']['layout'] ) ) {
			return $schema;
		}

		$schema['footer']['layout']['options'] = $labels;
		return $schema;
	}
}
