<?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\Dynamic\Listener;

use YOOtheme\Builder\Source;
use ZOOlanders\YOOessentials\Dynamic\DynamicResolver;

class ExpandJoomlaSources
{
    /**
     * @param Source $source
     */
    public function handle($source)
    {
        $types = DynamicResolver::getSourceTypeDefinitions($source) ?? [];
        $queryFields = $types['Query']->config['fields'] ?? [];

        if (isset($types['Article']) && isset($queryFields['customArticle']) && isset($queryFields['customArticles'])) {
            $source->queryType([
                'fields' => [
                    'customArticle' => [
                        'metadata' => [
                            'fields' => [
                                'id' => [
                                    'source' => 'true',
                                ],
                                'catid' => [
                                    'source' => 'true',
                                ],
                                'tags' => [
                                    'source' => 'true',
                                ],
                                'users' => [
                                    'source' => 'true',
                                ],
                                'featured' => [
                                    'source' => 'true',
                                ],
                                'offset' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                    'customArticles' => [
                        'metadata' => [
                            'fields' => [
                                'catid' => [
                                    'source' => 'true',
                                ],
                                'tags' => [
                                    'source' => 'true',
                                ],
                                'users' => [
                                    'source' => 'true',
                                ],
                                'featured' => [
                                    'source' => 'true',
                                ],
                                '_offset' => [
                                    'fields' => [
                                        'offset' => [
                                            'source' => 'true',
                                        ],
                                        'limit' => [
                                            'source' => 'true',
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]);

            $source->objectType('Article', [
                'fields' => [
                    'tags' => [
                        'metadata' => [
                            'arguments' => [
                                'parent_id' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                ]
            ]);
        }

        if (isset($queryFields['customCategory']) && isset($queryFields['customCategories'])) {
            $source->queryType([
                'fields' => [
                    'customCategory' => [
                        'metadata' => [
                            'fields' => [
                                'id' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                    'customCategories' => [
                        'metadata' => [
                            'fields' => [
                                'catid' => [
                                    'source' => 'true',
                                ],
                                '_offset' => [
                                    'fields' => [
                                        'offset' => [
                                            'source' => 'true',
                                        ],
                                        'limit' => [
                                            'source' => 'true',
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ],
                ],
            ]);
        }

        if (isset($queryFields['customTag']) && isset($queryFields['customTags'])) {
            $source->queryType([
                'fields' => [
                    'customTag' => [
                        'metadata' => [
                            'fields' => [
                                'id' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                    'customTags' => [
                        'metadata' => [
                            'fields' => [
                                'parent_id' => [
                                    'source' => 'true',
                                ],
                                '_offset' => [
                                    'fields' => [
                                        'offset' => [
                                            'source' => 'true',
                                        ],
                                        'limit' => [
                                            'source' => 'true',
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ],
                ],
            ]);
        }

        if (isset($queryFields['customMenuItem']) && isset($queryFields['customMenuItems'])) {
            $source->queryType([
                'fields' => [
                    'customMenuItem' => [
                        'metadata' => [
                            'fields' => [
                                'menu' => [
                                    'source' => 'true',
                                ],
                                'id' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                    'customMenuItems' => [
                        'metadata' => [
                            'fields' => [
                                'id' => [
                                    'source' => 'true',
                                ],
                                'parent' => [
                                    'source' => 'true',
                                ],
                                'ids' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                ],
            ]);
        }

        if (isset($queryFields['customUser']) && isset($queryFields['customUsers'])) {
            $source->queryType([
                'fields' => [
                    'customUser' => [
                        'metadata' => [
                            'fields' => [
                                'id' => [
                                    'source' => 'true',
                                ],
                            ]
                        ]
                    ],
                    'customUsers' => [
                        'metadata' => [
                            'fields' => [
                                'groups' => [
                                    'source' => 'true',
                                ],
                                '_offset' => [
                                    'fields' => [
                                        'offset' => [
                                            'source' => 'true',
                                        ],
                                        'limit' => [
                                            'source' => 'true',
                                        ],
                                    ]
                                ],
                            ]
                        ]
                    ],
                ]
            ]);
        }
    }
}
