<?php
/**
 * @package   Essentials YOOtheme Pro 2.3.4 build 0217.0955
 * @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;

use ZOOlanders\YOOessentials\Util;

return [
    'config' => [
        // remove duplicated global queries
        '2.2.20' => function (array $config) {
            $queries = Util\Arr::get($config, 'dynamic.queries', []);
            $queries = Util\Arr::removeDuplicates($queries);

            Util\Arr::set($config, 'dynamic.queries', $queries);

            return $config;
        },
    ],

    'nodes' => [
        '2.3.0-rc.8' => function ($node) {
            Update230Helper::iterateNodeSources($node, function ($source) {
                if (isset($source->query->name) && $source->query->name === '~closest') {
                    $source->query->name = '#parent';
                }
            });

            // remove source_extended props already present in source props
            // (seems to be a bug in some previous version)
            $sourceProps = (array) ($node->source->props ?? []);

            foreach ((array) ($node->source_extended->props ?? []) as $name => $prop) {
                if (isset($sourceProps[$name])) {
                    if (is_array($node->source_extended->props)) {
                        unset($node->source_extended->props[$name]);
                    } else {
                        unset($node->source_extended->props->{$name});
                    }
                }
            }
        },

        '2.0.0' => function ($node) {
            if (!($node->source_extended ?? false)) {
                return;
            }

            $globalKey = '~global';
            $closestKey = '~closest';

            // query global
            if ($node->source_extended->query->global ?? false) {
                $node->source = $node->source ?? (object) [];
                $node->source->query = $node->source->query ?? (object) [];
                $node->source->query->name = $globalKey . ':' . $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 = $node->source ?? (object) [];
                $node->source->props = (object) ($node->source->props ?? []);
                $node->source->props->$key = clone $prop;
                $node->source->props->$key->query = (object) [
                    'name' => $globalKey . ':' . $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 = $node->source ?? (object) [];
                $node->source->props = (object) ($node->source->props ?? []);
                $node->source->props->$key = clone $prop;
                $node->source->props->$key->query = (object) [
                    'name' => $closestKey,
                ];

                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);
            }
        }
    ],
];
