<?php
/**
 * Layout factory.
 *
 * @package PRECO
 */

namespace PRECO\Includes\Frontend\Core\Layouts;

defined( 'ABSPATH' ) || exit;

/**
 * Maps template_layout slugs to layout objects.
 */
class LayoutFactory {

	/**
	 * Default layout slug.
	 */
	const DEFAULT_LAYOUT = 'lay_1';

	/**
	 * Slug => class map of all registered layouts.
	 *
	 * @return array<string,string>
	 */
	public static function get_layouts() {
		return array(
			'lay_1' => LayoutOne::class,
			'lay_2' => LayoutTwo::class,
			'lay_3' => LayoutThree::class,
			'lay_4' => LayoutFour::class,
			'lay_5' => LayoutFive::class,
		);
	}

	/**
	 * Whitelist a slug, falling back to the default layout.
	 *
	 * @param string $slug Raw slug from saved settings.
	 * @return string
	 */
	public static function normalize_slug( $slug ) {
		$layouts = self::get_layouts();
		return is_string( $slug ) && isset( $layouts[ $slug ] ) ? $slug : self::DEFAULT_LAYOUT;
	}

	/**
	 * Create the layout object for a slug.
	 *
	 * @param string $slug Layout slug (lay_1 … lay_5).
	 * @return AbstractLayout
	 */
	public static function create( $slug ) {
		$layouts = self::get_layouts();
		$class   = $layouts[ self::normalize_slug( $slug ) ];
		return new $class();
	}
}
