<?php
/**
 * @package   DPCalendar
 * @copyright Copyright (C) 2018 Digital Peak GmbH. <https://www.digital-peak.com>
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
 */

namespace DigitalPeak\Component\DPCalendar\Administrator\Booking\Stages;

\defined('_JEXEC') or die();

use DigitalPeak\Component\DPCalendar\Administrator\Pipeline\StageInterface;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

class AdjustCustomFields implements StageInterface
{
	public function __invoke(\stdClass $payload): \stdClass
	{
		if (empty($payload->item) || empty($payload->eventsWithTickets)) {
			return $payload;
		}

		FieldsHelper::clearFieldsCache();

		$event                   = reset($payload->eventsWithTickets);
		$payload->item->catid    = $event->catid;
		$payload->item->jcfields = FieldsHelper::getFields('com_dpcalendar.booking', $payload->item, true);
		unset($payload->item->catid);

		return $payload;
	}
}
