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

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractQueryType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Source\Provider\Xml\XmlResolver;
use ZOOlanders\YOOessentials\Source\Provider\Xml\XmlSourceFile;
use ZOOlanders\YOOessentials\Source\Provider\Xml\XmlSourceUrl;
use ZOOlanders\YOOessentials\Source\Resolver\CachesResolvedSourceData;
use ZOOlanders\YOOessentials\Source\Resolver\HasDynamicArgs;
use ZOOlanders\YOOessentials\Source\Resolver\LoadsSourceFromArgs;
use ZOOlanders\YOOessentials\Source\Type\SourceInterface;
use ZOOlanders\YOOessentials\Util\Arr;

class XmlQueryType extends AbstractQueryType implements HasSourceInterface
{
    use CachesResolvedSourceData, LoadsSourceFromArgs, HasDynamicArgs;

    private XmlType $xmlType;

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

        $this->xmlType = $xmlType;
    }

    public function name(): string
    {
        return $this->xmlType->name() . '_query';
    }

    public function label(): string
    {
        return $this->source()->name();
    }

    public function config(): array
    {
        $args = [
            'source_id' => $this->source->id(),
        ];

        if (!$this->source->id()) {
            $args = array_merge($args, $this->source->config());
        }

        return [
            'fields' => [
                $this->name() => [
                    'type' => $this->xmlType->name(),

                    'args' => [
                        'cache' => [
                            'type' => 'Int',
                        ],
                    ],

                    'metadata' => [
                        'group' => 'XML',
                        'label' => $this->label(),
                        'fields' => [
                            'cache' => $this->cacheField(),
                        ],
                    ],

                    'extensions' => [
                        'call' => [
                            'func' => __CLASS__ . '::resolve',
                            'args' => $args,
                        ],
                    ],
                ],
            ],
        ];
    }

    public static function resolve($root, array $args): array
    {
        /** @var XmlSourceUrl|XmlSourceFile */
        $source = self::loadSource($args, XmlSourceFile::class);

        if (!$source) {
            return [];
        }

        return self::resolveFromCache($source, $args, function () use ($source) {
            return (new XmlResolver($source))->resolve();
        });
    }

    public static function getCacheKey(): string
    {
        return 'xml';
    }

    protected static function resolveArgs(array $args, $root): array
    {
        $dynamicArgs = ['filters', 'ordering', 'orderings'];

        foreach ($dynamicArgs as $arg) {
            $args[$arg] = Arr::map($args[$arg] ?? [], function ($dynamic) use ($root) {
                if (!isset($dynamic['source'])) {
                    return $dynamic;
                }

                return self::resolveDynamicArguments($dynamic, $root);
            });
        }

        return $args;
    }
}
