<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\Components;

use Ferienpass\CoreBundle\Entity\OfferDate as OfferDateEntity;
use Ferienpass\CoreBundle\Settings\PageConfigManager;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

#[AsTwigComponent(template: '@Contao/components/offer_date.html.twig')]
class OfferDate
{
    /** @var iterable<OfferDateEntity> */
    public iterable $dates;
    public string $delimiter = ' + ';
    public ?string $locale = null;
    public bool $short = false;

    public function __construct(private readonly PageConfigManager $pageConfigManager)
    {
    }

    /**
     * All occurrences across the given dates, anchored to the configured view
     * timezone so recurring times keep their wall-clock across DST transitions.
     *
     * @return list<array{begin: \DateTimeInterface, end: \DateTimeInterface}>
     */
    #[ExposeInTemplate]
    public function occurrences(): array
    {
        $tz = new \DateTimeZone($this->pageConfigManager->getPageConfig()->getTimezone());

        $out = [];
        foreach ($this->dates as $date) {
            foreach ($date->getOccurrences($tz) as $occ) {
                $out[] = $occ;
            }
        }

        return $out;
    }
}
