<?php

const MYHOME_VERSION = '1.0.5';

add_filter('body_class', static function ($classes) {
    $classes[] = 'myhome-' . MYHOME_VERSION;
    return $classes;
});

require get_template_directory() . '/basic.php';
require get_template_directory() . '/src/tgm/class-tgm-plugin-activation.php';

function myhome_customer_agent_rule_allows(string $rule): bool
{
    if (function_exists('hwp_customer_agent_rule')) {
        return hwp_customer_agent_rule($rule, true) !== false;
    }

    return true;
}

add_action('after_setup_theme', static function () {
    add_theme_support('post-thumbnails');
    add_theme_support('nav-menus');
    add_theme_support('title-tag');
    add_theme_support('woocommerce');
    add_theme_support('custom-logo', [
        'width' => 160,
        'height' => 36,
    ]);
    add_theme_support(
        'html5', [
            'search-form',
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
        ]
    );

    load_theme_textdomain('myhome', get_template_directory() . '/languages');

    register_nav_menus(['myhome-primary' => esc_html__('MyHome Theme Default Menu', 'myhome')]);
});

add_action('widgets_init', static function () {
    register_sidebar(
        [
            'name' => esc_html__('MyHome Sidebar', 'myhome'),
            'id' => 'myhome-sidebar',
            'description' => esc_html__('Add widgets here.', 'myhome'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="myhome-widget-title">',
            'after_title' => '</h3>',
        ]
    );
});

add_action('tgmpa_register', static function () {
    $plugins = apply_filters('myhome/plugins', [
        [
            'name' => esc_html__('Contact Form 7', 'myhome'),
            'slug' => 'contact-form-7',
            'required' => true,
        ],
        [
            'name' => esc_html__('Elementor', 'myhome'),
            'slug' => 'elementor',
            'required' => true,
            'version' => '3.1.4',
            'force_activation' => false,
            'force_deactivation' => false,
        ],
        [
            'name' => esc_html__('MyHome Core', 'myhome'),
            'slug' => 'myhome-core',
            'source' => get_template_directory() . '/src/tgm/plugins/myhome-core.zip',
            'required' => true,
            'version' => '1.0.5',
            'force_activation' => false,
            'force_deactivation' => false,
        ],
        [
            'name' => esc_html__('MyHome Updater', 'myhome'),
            'slug' => 'myhome-updater',
            'source' => get_template_directory() . '/src/tgm/plugins/myhome-updater.zip',
            'required' => false,
            'version' => '1.4.5',
            'force_activation' => false,
            'force_deactivation' => false,
        ],
    ]);

    if (!myhome_customer_agent_rule_allows('allow_theme_update')) {
        $plugins = array_values(array_filter($plugins, static function (array $plugin): bool {
            return ($plugin['slug'] ?? '') !== 'myhome-updater';
        }));
    }

    tgmpa(
        $plugins
    );
});

add_action('wp_enqueue_scripts', static function () {
    $deps = [];

    if (class_exists(\Elementor\Plugin::class)) {
        $deps[] = 'elementor-frontend';
    }

    if (is_rtl()) {
        wp_enqueue_style('myhome-rtl', get_template_directory_uri() . '/style-rtl.css', $deps, MYHOME_VERSION);
    } else {
        wp_enqueue_style('myhome', get_stylesheet_uri(), $deps, MYHOME_VERSION);
    }

    if (!class_exists(\Tangibledesign\Framework\Core\App::class)) {
        wp_enqueue_style('font-awesome', get_template_directory_uri() . '/assets/css/all.css');
        wp_enqueue_script('myhome-js', get_template_directory_uri() . '/assets/js/blog.min.js', ['jquery'],
            MYHOME_VERSION, true);
    }
});

add_action('widgets_init', static function () {
    register_sidebar(
        [
            'name' => esc_html__('MyHome Sidebar', 'myhome'),
            'id' => 'myhome-sidebar',
            'description' => esc_html__('Add widgets here.', 'myhome'),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget' => '</section>',
            'before_title' => '<h3 class="myhome-widget-title">',
            'after_title' => '</h3>',
        ]
    );
});

if (!\Tangibledesign\Framework\Core\App::class) {
    add_theme_support('automatic-feed-links');
}

if (!isset($content_width)) {
    $content_width = 900;
}
