<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Source\Provider\Rss\Type;

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractObjectType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Source\Provider\Xml\Type\ExtractsFields;
use ZOOlanders\YOOessentials\Source\Type\SourceInterface;

class RssFeedType extends AbstractObjectType implements HasSourceInterface
{
    use ExtractsFields;

    private ?RssFeedItemType $itemType = null;

    public function __construct(SourceInterface $source)
    {
        parent::__construct($source);

        $this->getFields($this->source->rss()->toArray());
    }

    public function itemType(): RssFeedItemType
    {
        if ($this->itemType !== null) {
            return $this->itemType;
        }

        $this->itemType = new RssFeedItemType($this->source->rss()->items(), $this->name());

        return $this->itemType;
    }

    public function name(): string
    {
        return self::encodeField('RSSfeed_' . $this->source->id());
    }

    public function config(): array
    {
        $data = $this->source->rss()->toArray();

        $fields = $this->getFields($data);

        return [
            'fields' => $fields,
            'metadata' => [
                'type' => true,
                'label' => $this->label(),
            ],
        ];
    }

    public static function resolveDateTime($data, $args)
    {
        $date = $data[$args['header']] ?? null;

        return $date ? $date->format('U') : null;
    }
}
