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

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

class CsvFileType extends AbstractObjectType implements HasSourceInterface
{
    use HasDynamicFields;

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

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

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

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