<?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
 */

use ZOOlanders\YOOessentials\Dynamic\DynamicResolver;

return [
    'nodes' => [
        // migrate global and closest settings to new format
        '2.0.0' => function ($node) {
            if (!($node->source_extended ?? false)) {
                return;
            }

            $node->source = $node->source ?? (object) [];

            // query global
            if ($node->source_extended->query->global ?? false) {
                $node->source->query = $node->source->query ?? (object) [];
                $node->source->query->name = DynamicResolver::GLOBAL . ':' . $node->source_extended->query->global;

                unset($node->source_extended->query);
            }

            // props global
            foreach ((array) ($node->source_extended->props ?? []) as $key => $prop) {
                if (!($prop->query->global ?? false)) {
                    continue;
                }

                $node->source->props = (object) ($node->source->props ?? []);
                $node->source->props->$key = clone $prop;
                $node->source->props->$key->query = (object) [
                    'name' => DynamicResolver::GLOBAL . ':' . $prop->query->global,
                ];

                unset($node->source_extended->props->$key);
            }

            // props closest
            foreach ((array) ($node->source_extended->props ?? []) as $key => $prop) {
                if (!($prop->query->inherit ?? false)) {
                    continue;
                }

                $node->source->props = (object) ($node->source->props ?? []);
                $node->source->props->$key = clone $prop;
                $node->source->props->$key->query = (object) [
                    'name' => DynamicResolver::CLOSEST,
                ];

                unset($node->source_extended->props->$key);
            }

            // cleanup
            $node->source_extended->props = array_filter((array) ($node->source_extended->props ?? []));

            if (empty($node->source_extended->props)) {
                unset($node->source_extended->props);
            }

            if (empty((array) $node->source_extended)) {
                unset($node->source_extended);
            }
        },
    ],
];
