<?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\Str;
use YOOtheme\Builder\Source;
use YOOtheme\Builder\Wordpress\Source\Type\CustomPostQueryType;
use YOOtheme\Builder\Wordpress\Source\Type\CustomTaxonomyQueryType;
use YOOtheme\Builder\Wordpress\Source\Helper as SourceHelper;
use ZOOlanders\YOOessentials\Dynamic\DynamicResolver;

class ExpandWordpressSources
{
    /**
     * @param Source $source
     */
    public function handle($source)
    {
        if (!class_exists(SourceHelper::class) || !function_exists('wp')) {
            return;
        }

        $types = DynamicResolver::getSourceTypeDefinitions($source) ?? [];
        $queryFields = $types['Query']->config['fields'] ?? [];

        if (class_exists(CustomPostQueryType::class)) {
            foreach (SourceHelper::getPostTypes() as $type) {
                $name = Str::camelCase($type->name, true);
                $base = Str::camelCase(SourceHelper::getBase($type), true);
                $typeName = Str::camelCase([$base, 'Query'], true);

                $taxonomies = SourceHelper::getObjectTaxonomies($type->name);
                $terms = $taxonomies ? ['source' => true] : [];

                if (isset($types[$typeName])) {
                    $source->objectType($typeName, [
                        'fields' => [
                            "custom{$name}" => [
                                'metadata' => [
                                    'fields' => array_merge(
                                        [
                                            'id' => [
                                                'source' => true
                                            ],
                                        ],
                                        $terms ? ['terms' => $terms] : [],
                                        [
                                            'users' => [
                                                'source' => true,
                                            ],
                                            'offset' => [
                                                'source' => true,
                                            ],
                                        ],
                                    ),
                                ],
                            ],
                            "custom{$base}" => [
                                'metadata' => [
                                    'fields' => array_merge(
                                        $terms ? ['terms' => $terms] : [],
                                        [
                                            'users' => [
                                                'source' => true,
                                            ],
                                            '_offset' => [
                                                'fields' => [
                                                    'offset' => [
                                                        'source' => true,
                                                    ],
                                                    'limit' => [
                                                        'source' => true,
                                                    ],
                                                ],
                                            ],
                                        ]
                                    )
                                ],
                            ],
                        ]
                    ]);
                }
            }
        }

        if (class_exists(CustomTaxonomyQueryType::class)) {
            foreach (SourceHelper::getTaxonomies() as $taxonomy) {
                $base = Str::camelCase(SourceHelper::getBase($taxonomy), true);
                $name = Str::camelCase($taxonomy->name, true);
                $typeName = Str::camelCase([$base, 'Query'], true);

                if (isset($types[$typeName])) {
                    $source->objectType($typeName, [
                        'fields' => [
                            "custom{$name}" => [
                                'metadata' => [
                                    'fields' => [
                                        'id' => [
                                            'source' => true
                                        ]
                                    ]
                                ]
                            ],
                            "custom{$base}" => [
                                'metadata' => [
                                    'fields' => ($taxonomy->hierarchical
                                        ? [
                                            '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' => [
                                'roles' => [
                                    'source' => 'true',
                                ],
                                '_offset' => [
                                    'fields' => [
                                        'offset' => [
                                            'source' => 'true',
                                        ],
                                        'limit' => [
                                            'source' => 'true',
                                        ],
                                    ]
                                ],
                            ]
                        ]
                    ],
                ]
            ]);
        }
    }
}
