<?php

namespace YOOtheme\Builder\Wordpress\Source;

use YOOtheme\Builder;
use YOOtheme\Builder\BuilderConfig;
use YOOtheme\Builder\Source\Filesystem\FileHelper;
use YOOtheme\Builder\Source\SourceTransform;
use YOOtheme\Builder\UpdateTransform;
use YOOtheme\Config;

return [
    'config' => [
        'source' => [
            'id' => get_current_blog_id(),
        ],

        BuilderConfig::class => __DIR__ . '/config/customizer.php',
    ],

    'routes' => [
        ['get', '/wordpress/posts', [SourceController::class, 'posts']],
        ['get', '/wordpress/users', [SourceController::class, 'users']],
    ],

    'events' => [
        'source.init' => [Listener\LoadSourceTypes::class => 'handle'],
        'builder.template' => [Listener\MatchTemplate::class => '@handle'],
        'builder.template.load' => [Listener\LoadTemplateUrl::class => 'handle'],
        BuilderConfig::class => [Listener\LoadBuilderConfig::class => ['handle', 10]],
    ],

    'filters' =>
        [
            'template_include' => [Listener\LoadTemplate::class => ['@include', 20]],
            'wp_link_query_args' => [Listener\AddPostType::class => '@handle'],
        ] +
        (!is_admin() && !wp_doing_cron()
            ? ['pre_get_posts' => [Listener\LoadTemplate::class => ['@match', 30]]]
            : []),

    'extend' => [
        Builder::class => function (Builder $builder) {
            $elements = ['comments', 'pagination'];

            foreach ($elements as $element) {
                $builder->addType($element, __DIR__ . "/elements/{$element}/element.php");
            }
        },

        UpdateTransform::class => function (UpdateTransform $update) {
            $update->addGlobals(require __DIR__ . '/updates.php');
        },

        SourceTransform::class => function (SourceTransform $transform) {
            $transform->addFilter('date', function ($value, $format) {
                if (!$value) {
                    return $value;
                }

                if (is_string($value) && !is_numeric($value)) {
                    $value = strtotime($value);
                }

                return wp_date(
                    $format ?: get_option('date_format', 'd/m/Y'),
                    $value,
                    wp_timezone(),
                );
            });

            $transform->addFilter(
                'limit',
                fn($value, $limit, array $filters) => $transform->applyLimit(
                    $limit ? strip_shortcodes($value) : $value,
                    $limit,
                    $filters,
                ),
            );
        },
    ],

    'services' => [
        Listener\LoadTemplate::class => '',

        Listener\MatchTemplate::class => '',

        FileHelper::class => fn(Config $config) => new FileHelper($config('app.uploadDir')),
    ],
];
