<?php

namespace YOOtheme;

use Joomla\CMS\Helper\ModuleHelper;

return [

    'transforms' => [

        'render' => function ($node) {

            $load = new \ReflectionMethod('Joomla\CMS\Helper\ModuleHelper', 'load');
            $load->setAccessible(true);

            foreach ($load->invoke(null) as $module) {

                if ($node->props['module'] !== $module->id) {
                    continue;
                }

                $config = app(Config::class);
                $index = "~theme.modules.{$module->id}";
                $props = $config->get($index, []);

                $node->attrs['class'] = array_merge($node->attrs['class'], $props['class']);
                $node->props = Arr::merge($props, $node->props);

                // override module config with props
                $config->set($index, $node->props);

                // render module content
                $node->module = (object) [
                    'title' => $module->title,
                    'content' => ModuleHelper::renderModule($module),
                ];

                // reset module config
                $config->set($index, $props);

                break;
            }

            // return false, if no module content was found
            if (empty($node->props['module']) || empty($node->module->content)) {
                return false;
            }

        },

    ],

    'updates' => [

        '1.20.0-beta.1.1' => function ($node) {

            if (isset($node->props['maxwidth_align'])) {
                $node->props['block_align'] = $node->props['maxwidth_align'];
                unset($node->props['maxwidth_align']);
            }

        },

        '1.20.0-beta.0.1' => function ($node) {

            if (@$node->props['title_style'] === 'heading-primary') {
                $node->props['title_style'] = 'heading-medium';
            }

            /**
             * @var Config $config
             */
            $config = app(Config::class);

            list($style) = explode(':', $config('~theme.style'));

            if (in_array($style, ['craft', 'district', 'jack-backer', 'tomsen-brody', 'vision', 'florence', 'max', 'nioh-studio', 'sonic', 'summit', 'trek'])) {

                if (@$node->props['title_style'] === 'h1' || (empty($node->props['title_style']) && @$node->props['title_element'] === 'h1')) {
                    $node->props['title_style'] = 'heading-small';
                }

            }

            if (in_array($style, ['florence', 'max', 'nioh-studio', 'sonic', 'summit', 'trek'])) {

                if (@$node->props['title_style'] === 'h2') {
                    $node->props['title_style'] = @$node->props['title_element'] === 'h1' ? '' : 'h1';
                } elseif (empty($node->props['title_style']) && @$node->props['title_element'] === 'h2') {
                    $node->props['title_style'] = 'h1';
                }

            }

            if (in_array($style, ['fuse', 'horizon', 'joline', 'juno', 'lilian', 'vibe', 'yard'])) {

                if (@$node->props['title_style'] === 'heading-medium') {
                    $node->props['title_style'] = 'heading-small';
                }

            }

            if (in_array($style, ['copper-hill'])) {

                if (@$node->props['title_style'] === 'heading-medium') {
                    $node->props['title_style'] = @$node->props['title_element'] === 'h1' ? '' : 'h1';
                } elseif (@$node->props['title_style'] === 'h1') {
                    $node->props['title_style'] = @$node->props['title_element'] === 'h2' ? '' : 'h2';
                } elseif (empty($node->props['title_style']) && @$node->props['title_element'] === 'h1') {
                    $node->props['title_style'] = 'h2';
                }

            }

            if (in_array($style, ['trek', 'fjord'])) {

                if (@$node->props['title_style'] === 'heading-medium') {
                    $node->props['title_style'] = 'heading-large';
                }

            }

        },

    ],

];
