<?php
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function rt_setup() {
    /*
     * Make theme available for translation.
     * Translations can be filed in the /languages/ directory.
     * If you're building a theme based on retheme, use a find and replace
     * to change 'retheme' to the name of your theme in all the template files.
     */
    load_theme_textdomain('jelajah-text', get_template_directory() . '/languages');
    
    add_theme_support('responsive-embeds');
    add_theme_support('post-thumbnails');
    add_theme_support('title-tag');

    // Add theme support selective refresh for customizer
    add_theme_support('customize-selective-refresh-widgets');
    
    // Register theme menu
    register_nav_menus([
        'primary' => 'Primary Menu',
        'topbar' => 'Top Bar Menu',
        'footer' => 'Footer Menu',
        'mobile' => 'Mobile Menu',
    ]);

    // Disable block widget
    if (rt_get_option('disable_block_widget', true)) {
        add_filter('use_widgets_block_editor', '__return_false');
    }

    // Hook premium fiture
    if (rt_is_premium()) {
        do_action('rt_after_setup_theme');
    }
}
add_action('after_setup_theme', 'rt_setup');

/*====================================================
/* ADD BODY CLASS
/*====================================================*/
function rt_body_class($classes) {
    if(is_front_page()) {
        $classes[] = 'is-style-spacing-lg';
    }
    return $classes;
}
add_filter('body_class', 'rt_body_class');

/*=================================================;
/* DEFAUT VARIABLE
/*================================================= */
function rt_get_theme($value = '') {

    $theme_json = json_decode(file_get_contents(get_stylesheet_directory() . '/theme.json'));

    if ($theme_json) {

        // Colors
        $json_colors = $theme_json->settings->color->palette;

        foreach ($json_colors as $key => $json_color) {
            $theme_colors['color'][$json_color->slug] =  $json_color->color;
        }
    }

    $default = apply_filters('rt_setup_theme', array_merge([
        'product-name' => RT_THEME_NAME,
        'product-slug' => RT_THEME_SLUG,
        'product-version' => RT_THEME_VERSION,
        'product-docs' => RT_THEME_DOC,
        'product-url' => RT_THEME_URL,
        'extends' => [],
        'typography' => [
            'heading' => [
                'font-family' => 'Inter',
                'variant' => '700',
            ],
            'body' => [
                'font-family' => 'Inter',
                'variant' => 'regular',
            ],
            'menu' => [
                'font-family' => 'Inter',
                'variant' => '600',
            ],
        ],
        'button' => [
            'primary' => [
                'color' => $theme_json->settings->custom->button->color,
                'background' => $theme_json->settings->custom->button->background,
                'color-active' => $theme_json->settings->custom->button->color,
                'background-active' => $theme_json->settings->custom->button->background
            ],
        ],
        'color-secondary' => [
            'background' => '#161616',
            'heading' => '#e0e0e0',
            'body' => '#b3b3b3',
            'meta' => '#696969',
            'outline' => '#333333',
            'text-link' => '#b3b3b3',
            'text-link-active' => '#ffffff',
        ],
    ], $theme_colors));

    return !empty($default[$value]) ? $default[$value] : '';
}

/*=================================================;
/* THEME - BRAND META
/*=================================================
 * Add theme color brand on search board mobile browser
 *
 * @version 1.0.0
 */
function rt_theme_color() {

    $color = rt_get_option('brand_color_browser');

    if (!empty($color)) {
        echo '<meta name="theme-color" content="' . $color . '">';
    }
}
add_action('wp_head', 'rt_theme_color');