<?php

/*
 * This file is part of the eluceo/iCal package.
 *
 * (c) 2024 Markus Poerschke <markus@poerschke.nrw>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace CommonsBooking\Eluceo\iCal\Presentation\Factory;

use CommonsBooking\Eluceo\iCal\Domain\ValueObject\DateTime;
use CommonsBooking\Eluceo\iCal\Domain\ValueObject\PointInTime;
use CommonsBooking\Eluceo\iCal\Domain\ValueObject\Timestamp;
use CommonsBooking\Eluceo\iCal\Presentation\Component\Property;
use CommonsBooking\Eluceo\iCal\Presentation\Component\Property\Value\DateTimeValue;
use CommonsBooking\Eluceo\iCal\Presentation\Component\Property\Value\TextValue;

class DateTimeFactory
{
    /**
     * @param Timestamp|DateTime $pointInTime
     */
    public function createProperty(string $propertyName, PointInTime $pointInTime): Property
    {
        return new Property(
            $propertyName,
            new DateTimeValue($pointInTime),
            $this->getParameters($pointInTime)
        );
    }

    /**
     * @return array<Property\Parameter>
     */
    private function getParameters(PointInTime $pointInTime): array
    {
        if (
            $pointInTime instanceof DateTime
            && $pointInTime->hasDateTimeZone()
            && $pointInTime->getDateTimeZone()->getName() !== 'UTC'
        ) {
            return [
                new Property\Parameter('TZID', new TextValue($pointInTime->getDateTimeZone()->getName())),
            ];
        }

        return [];
    }
}
