<?php
/**
 * Immutable footer read model for one request.
 *
 * @package Kutby
 * @since   2.7.0
 */

declare(strict_types=1);

namespace Kutby\Footer\Support;

use Kutby\ThemeOptions\Manager;

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

/**
 * Class FooterContext
 */
final class FooterContext {

	/**
	 * @var self|null
	 */
	private static ?self $request = null;

	/**
	 * @param array<string, mixed> $footer Footer option group.
	 * @param array<string, mixed> $social Social option group.
	 */
	private function __construct(
		private readonly array $footer,
		private readonly array $social
	) {}

	/**
	 * @return self
	 */
	public static function from_request(): self {
		if ( null !== self::$request ) {
			return self::$request;
		}

		self::$request = new self(
			Manager::get_group( 'footer', array() ),
			Manager::get_group( 'social', array() )
		);

		return self::$request;
	}

	/**
	 * @param string $key     Key.
	 * @param mixed  $default Default.
	 * @return mixed
	 */
	public function get( string $key, mixed $default = null ): mixed {
		return $this->footer[ $key ] ?? $default;
	}

	/**
	 * @param string $key Key.
	 * @return bool
	 */
	public function bool( string $key ): bool {
		return ! empty( $this->footer[ $key ] );
	}

	/**
	 * @param string $key     Key.
	 * @param string $default Default.
	 * @return string
	 */
	public function string( string $key, string $default = '' ): string {
		return (string) ( $this->footer[ $key ] ?? $default );
	}

	/**
	 * @return string
	 */
	public function layout_id(): string {
		$id = $this->string( 'layout', 'classic' );
		return '' !== $id ? $id : 'classic';
	}

	/**
	 * Widget column count (1–4).
	 *
	 * @return int
	 */
	public function columns(): int {
		return max( 1, min( 4, (int) $this->get( 'columns', 4 ) ) );
	}

	/**
	 * @return array<string, mixed>
	 */
	public function social(): array {
		return $this->social;
	}
}
