<?php

namespace Planaday\Html\NodeTags\Admin\Page\Booking;

use Planaday\Config\Booking\BookingSettings;
use Planaday\Html\NodeTags\Tag;
use Planaday\Html\NodeTags\TemplateOnlyTag;
use Planaday\Platform\PlatformAbstraction\DataHandler;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Utility\DependencyInjection\Attributes\Transient;
use Planaday\Utility\FormBuilder\Form;
use Planaday\Utility\FormBuilder\LayoutItems\MessageDisplay;
use Planaday\Utility\FormBuilder\LayoutItems\SubmitButton;
use Planaday\Utility\FormBuilder\SubmitStrategies\RestSubmit;

#[Transient]
class Booking implements Tag {
	use TemplateOnlyTag;

	public function __construct(
		protected BookingSettings $bookingSettings,
		protected UrlHandler $urlHandler,
		protected DataHandler $dataHandler,
		protected LocalizationController $translator,
	) {}

	protected function prepareData( $nodeData ): void {
		$nodeData->data["formJson"] = function () {
			return json_encode((new Form())
				->setSubmitBehaviour(
					(new RestSubmit())
						->setHeaders($this->dataHandler->getRestNonceHeader())
						->setUrl($this->urlHandler->getRestApiPrefix() . 'settings/')
				)
				->setName('settings')
				->addContents(
					new MessageDisplay(),
					new SubmitButton($this->translator->translate('Instellingen opslaan')),
					$this->bookingSettings->getFormGroup(),
				));
		};
	}

	public function getTagDir(): string {
		return __DIR__;
	}
}