<?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 function YOOtheme\app;
use ZOOlanders\YOOessentials\Form\Action\Action;
use ZOOlanders\YOOessentials\Form\Action\ActionManager;

return [
    'nodes' => [
        // migrate fieldset as sublayout
        // '2.3?' => function ($node) {
        //     if ($node->type !== 'yooessentials_form_fieldset') {
        //         return;
        //     }

        //     $node->children = [
        //         (object) [
        //             'type' => 'row',
        //             'children' => [
        //                 (object) [
        //                     'type' => 'column',
        //                     'children' => $node->children
        //                 ]
        //             ]
        //         ]
        //     ];
        // },

        // fix fieldset regression
        '2.2.11' => function ($node) {
            if ($node->type !== 'yooessentials_form_fieldset') {
                return;
            }

            $firstChild = $node->children[0] ?? null;

            if ($firstChild && $firstChild->type === 'row') {
                $column = $firstChild->children[0] ?? null;

                $node->children = $column ? $column->children : $node->children;
            }
        },

        // rename mailchimp action name
        '2.2.5' => function ($node) {
            if (!isset($node->props['yooessentials_form'], $node->formid)) {
                return;
            }

            $config = $node->props['yooessentials_form'] ?? [];

            foreach ($config->after_submit_actions ?? [] as $action) {
                if ($action->type === 'mailchimp-member-add') {
                    $action->type = 'mailchimp-member-upsert';
                }
            }

            $node->props['yooessentials_form'] = $config;
        },

        // Generate Ids for Form actions
        '1.5.3' => function ($node) {
            if (!isset($node->props['yooessentials_form'], $node->formid)) {
                return;
            }

            $config = $node->props['yooessentials_form'] ?? [];

            foreach ($config->after_submit_actions ?? [] as $action) {
                $action->id = $action->id ?? uniqid();
            }

            $node->props['yooessentials_form'] = $config;
        },

        // Move action keys from class name to action name
        '1.5.2' => function ($node) {
            if (!isset($node->props['yooessentials_form'], $node->formid)) {
                return;
            }

            /** @var ActionManager $manager */
            $manager = app(ActionManager::class);

            $config = $node->props['yooessentials_form'] ?? [];

            foreach ($config->after_submit_actions ?? [] as $action) {
                // Old Action Key, and we still know the class
                if (class_exists($action->type ?? '')) {
                    $actionClass = app($action->type);
                    if ($actionClass instanceof Action) {
                        $actionClass = $manager->actionFromClassName(get_class($actionClass));
                        $action->type = $actionClass->name() ?? '';
                    }
                }
            }

            $node->props['yooessentials_form'] = $config;
        },
    ],
];
