<?php

declare(strict_types=1);

namespace mirzaev\huesos\controllers;

// Files of the project
use mirzaev\huesos\controllers\core,
	mirzaev\huesos\models\menu;

// Framework for PHP
use mirzaev\minimal\http\enumerations\content;

/**
 * Controller of pages
 *
 * @package mirzaev\huesos\controllers
 * @param array $errors Registry of errors
 *
 * @method null offer() Public offer
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class index extends core
{
	/**
	 * Errors
	 *
	 * @var array $errors Registry of errors
	 */
	protected array $errors = [
		'session' => [],
		'account' => [],
		'menu' => []
	];

	/**
	 * Public offer
	 *
	 * @return null
	 */
	public function offer(): null
	{
		if (isset($menu)) {
			// 

		} else {
			// Not received ... menu

			// Search for filters and write to the buffer of global variables of view templater
			$this->view->menu = menu::_read(
				return: 'MERGE(d, { name: d.name.@language })',
				sort: 'd.style.order ASC, d.created DESC, d._key DESC',
				amount: 4,
				parameters: ['language' => $this->language->name],
				errors: $this->errors['menu']
			);

			// Universalizing
			if ($this->view->menu instanceof _document) $this->view->menu = [$this->view->menu];
		}

		if (str_contains($this->request->headers['accept'], content::any->value)) {
			// Request for any response

			// Render page
			$page = $this->view->render('offer/page.html');

			// Sending response
			$this->response
				->start()
				->clean()
				->sse()
				->write($page)
				->validate($this->request)
				?->body()
				->end();

			// Deinitializing rendered page
			unset($page);
		}

		// Exit (success/fail)
		return null;
	}
}
