<?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\GoogleSheet\Type;

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractObjectType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Source\HasDynamicFields;
use ZOOlanders\YOOessentials\Source\Type\SourceInterface;

class GoogleSheetType extends AbstractObjectType implements HasSourceInterface
{
    use HasDynamicFields;

    public const TYPE_LABEL = 'Google Sheet';

    protected SourceInterface $source;

    public function name(): string
    {
        return 'googleSheet_' . sha1(json_encode(array_filter($this->source->headers())));
    }

    public function label(): string
    {
        return self::TYPE_LABEL;
    }

    public function config(): array
    {
        try {
            $fields = [];

            foreach ($this->source->headers() as $header) {
                $fields[self::encodeField($header)] = [
                    'type' => 'String',
                    'metadata' => [
                        'label' => self::labelField($header),
                        'fields' => [],
                    ],
                ];
            }

            return [
                'fields' => $fields,
                'metadata' => [
                    'type' => true,
                    'label' => $this->label(),
                ],
            ];
        } catch (\Exception $e) {
            return [];
        }
    }
}
