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

use YOOtheme\Str;
use ZOOlanders\YOOessentials\Util;
use ZOOlanders\YOOessentials\Dynamic\Update230Helper;

return [
    'nodes' => [
        '2.3.0-rc.8' => function ($node) {
            Update230Helper::iterateNodeSources($node, function ($source) {
                // remove outdated meta props in filters and ordering
                foreach (['filters', 'ordering'] as $arg) {
                    foreach ((array) ($source->query->arguments->{$arg} ?? []) as &$condition) {
                        if (!isset($condition->props)) {
                            continue;
                        }

                        $metaProperties = array_filter(array_keys(get_object_vars($condition->props)), fn (string $property) => stripos($property, '_') === 0);

                        foreach ($metaProperties as $property) {
                            unset($condition->props->{$property});
                        }
                    }
                }
            });
        },

        // convert filters source to source_extended custom prop query
        '1.7.0-beta.4' => function ($node) {
            if (!Str::startsWith($node->source->query->name ?? '', ['databaseRecord', 'fileCSV'])) {
                return;
            }

            foreach (['filters', 'ordering'] as $arg) {
                foreach ($node->source->query->arguments->{$arg} ?? [] as $condition) {
                    if (!isset($condition->source->query)) {
                        continue;
                    }

                    $condition->source_extended = (object) ['props' => (object) []];

                    foreach ($condition->source->props ?? [] as $name => $prop) {
                        $prop->query = $condition->source->query;
                        $condition->source_extended->props->{$name} = $prop;
                    }

                    unset($condition->source);
                }
            }
        },
    ],

    'config' => [
        // Remove duplicated sources
        '2.2.5' => function (array $config) {
            $sources = Util\Arr::get($config, 'source.sources', []);
            $sources = Util\Arr::removeDuplicates($sources);

            Util\Arr::set($config, 'source.sources', $sources);

            return $config;
        },

        // migrate sources to new format
        '1.6.0' => function (array $config) {
            // filter invalid sources
            Util\Config::filter($config, 'source.sources', function ($source) {
                return array_key_exists('provider', $source);
            });

            // cleanup corrupted sources (some weird issue we had)
            Util\Config::iterate($config, 'source.sources', function (&$source) {
                $source = array_filter(
                    $source,
                    function ($key) {
                        return !is_int($key);
                    },
                    ARRAY_FILTER_USE_KEY
                );
            });

            return $config;
        }
    ],
];
