<ul class="main-nav">
	<?php
	$nav = [];

	$activeController = $this->context->simpleControllerName;
	$activeAction = $this->context->simpleActionName;

	$registerNavItem = function ($text, $link, $active = false) use (&$nav)
	{
		$nav []= [
			'text' => $text,
			'link' => $link,
			'active' => $active];
	};

	$registerNavItem(
		'Home',
		\Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']),
		$activeController == 'static-pages' and $activeAction == 'main-page');

	if (Access::check(Privilege::ListPosts))
	{
		$registerNavItem(
			'Browse',
			\Chibi\Router::linkTo(['PostController', 'listView']),
			$activeController == 'post' and $activeAction != 'upload');
	}

	if (Access::check(Privilege::UploadPost))
	{
		$registerNavItem(
			'Upload',
			\Chibi\Router::linkTo(['PostController', 'uploadView']),
			$activeController == 'post' and $activeAction == 'upload');
	}

	if (Access::check(Privilege::ListComments))
	{
		$registerNavItem(
			'Comments',
			\Chibi\Router::linkTo(['CommentController', 'listView']),
			$activeController == 'comment');
	}

	if (Access::check(Privilege::ListTags))
	{
		$registerNavItem(
			'Tags',
			\Chibi\Router::linkTo(['TagController', 'listView']),
			$activeController == 'tag');
	}

	if (Access::check(Privilege::ListUsers))
	{
		$registerNavItem(
			'Users',
			\Chibi\Router::linkTo(['UserController', 'listView']),
			$activeController == 'user' and $activeAction != 'registration' and
				(!isset($this->context->route->arguments['name']) or
					$this->context->route->arguments['name'] != Auth::getCurrentUser()->name));
	}

	if (!Auth::isLoggedIn())
	{
		$registerNavItem(
			'Log in',
			\Chibi\Router::linkTo(['AuthController', 'loginView']),
			$activeController == 'auth' and $activeAction == 'login');

		$registerNavItem(
			'Register',
			\Chibi\Router::linkTo(['UserController', 'registrationAction']),
			$activeController == 'user' and $activeAction == 'registration');
	}
	else
	{
		$registerNavItem(
			'My account',
			\Chibi\Router::linkTo(['UserController', 'viewAction'], ['name' => Auth::getCurrentUser()->name]),
			$activeController == 'user' and isset($this->context->route->arguments['name']) and
				$this->context->route->arguments['name'] == Auth::getCurrentUser()->name);

		$registerNavItem(
			'Log out',
			\Chibi\Router::linkTo(['AuthController', 'logoutAction']));
	}

	if (!empty(getConfig()->help->title))
	{
		$registerNavItem(
			getConfig()->help->title,
			\Chibi\Router::linkTo(['StaticPagesController', 'helpView']),
			$activeController == 'static-pages' and $activeAction == 'help');
	}

	foreach ($nav as $navItem)
	{
		extract($navItem);
		echo TextHelper::htmlTag('li', TextHelper::HTML_OPEN, ['class' => rtrim('main-nav-item ' . ($active ? 'active' : ''))]);
		echo TextHelper::htmlTag('a', TextHelper::HTML_OPEN, ['href' => $link]);
		echo $text;
		echo TextHelper::htmlTag('a', textHelper::HTML_CLOSE);
		echo TextHelper::htmlTag('li', textHelper::HTML_CLOSE);
	}
	?>

	<?php if (Access::check(Privilege::ChangeUserSettings, Access::getIdentity(Auth::getCurrentUser()))): ?>
		<li class="safety">
			<ul>
				<?php foreach (PostSafety::getAll() as $safety): ?>
					<?php if (Access::check(
						Privilege::ListPosts,
						PostSafety::toString($safety))): ?>

						<li class="safety-<?= TextCaseConverter::convert(
							PostSafety::toString($safety),
							TextCaseConverter::CAMEL_CASE,
							TextCaseConverter::SPINAL_CASE) ?>">

							<a class="<?= Auth::getCurrentUser()->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>"
								href="<?= \Chibi\Router::linkTo(
									['UserController', 'toggleSafetyAction'],
									['safety' => $safety]) ?>"
								title="<?= sprintf('Searching %s posts: %s',
									PostSafety::toDisplayString($safety),
									Auth::getCurrentUser()->hasEnabledSafety($safety)
										? 'enabled'
										: 'disabled') ?>">

								<span><?= ucfirst(PostSafety::toDisplayString($safety)) ?></span>

							</a>
						</li>

					<?php endif ?>
				<?php endforeach ?>
			</ul>
		</li>
	<?php endif ?>

	<li class="search">
		<form name="search" action="<?= \Chibi\Router::linkTo(['PostController', 'listView']) ?>" method="get">

			<input
				class="autocomplete"
				type="search"
				name="query"
				placeholder="Search&hellip;"
				value="<?= isset($this->context->transport->searchQuery)
					? htmlspecialchars($this->context->transport->searchQuery)
					: '' ?>"/>

		</form>
	</li>
</ul>