<?php
/**
 * Proto Previewer theme functions and definitions (Underscores _s based)
 *
 * @package Proto_Previewer
 */

if (!defined('ABSPATH')) exit;

if (!defined('PROTO_PREVIEWER_VERSION')) {
    define('PROTO_PREVIEWER_VERSION', '1.0.0');
}

/**
 * Sets up theme defaults and registers support for various WordPress features.
 */
function proto_previewer_setup() {
    // Make theme available for translation.
    load_theme_textdomain('proto-previewer', get_template_directory() . '/languages');

    // Add default posts and comments RSS feed links to head.
    add_theme_support('automatic-feed-links');

    // Let WordPress manage the document title.
    add_theme_support('title-tag');

    // Enable support for Post Thumbnails on posts and pages.
    add_theme_support('post-thumbnails');

    // Register primary navigation menu.
    register_nav_menus([
        'primary' => esc_html__('Primary Menu', 'proto-previewer'),
        'footer'  => esc_html__('Footer Menu', 'proto-previewer'),
    ]);

    // Switch default core markup for search form, comment form, and comments to output valid HTML5.
    add_theme_support('html5', [
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
        'style',
        'script',
    ]);

    // Set up the WordPress core custom background feature.
    add_theme_support('custom-background', apply_filters('proto_previewer_custom_background_args', [
        'default-color' => 'edf2f6',
        'default-image' => '',
    ]));

    // Add theme support for selective refresh for widgets.
    add_theme_support('customize-selective-refresh-widgets');

    // Add support for core custom logo.
    add_theme_support('custom-logo', [
        'height'      => 80,
        'width'       => 80,
        'flex-width'  => true,
        'flex-height' => true,
    ]);
}
add_action('after_setup_theme', 'proto_previewer_setup');

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 */
function proto_previewer_content_width() {
    $GLOBALS['content_width'] = apply_filters('proto_previewer_content_width', 1200);
}
add_action('after_setup_theme', 'proto_previewer_content_width', 0);

/**
 * Register widget area.
 */
function proto_previewer_widgets_init() {
    register_sidebar([
        'name'          => esc_html__('Sidebar', 'proto-previewer'),
        'id'            => 'sidebar-1',
        'description'   => esc_html__('Add widgets here.', 'proto-previewer'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ]);

    register_sidebar([
        'name'          => esc_html__('Footer Area', 'proto-previewer'),
        'id'            => 'footer-1',
        'description'   => esc_html__('Add footer widgets here.', 'proto-previewer'),
        'before_widget' => '<div id="%1$s" class="footer-widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="footer-widget-title">',
        'after_title'   => '</h4>',
    ]);
}
add_action('widgets_init', 'proto_previewer_widgets_init');

/**
 * Enqueue scripts and styles.
 */
function proto_previewer_scripts() {
    // Google Fonts (EB Garamond & Radio Canada)
    wp_enqueue_style('proto-previewer-fonts', 'https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,500;0,600;0,700;0,800;1,500;1,600;1,700&family=Radio+Canada:ital,wght@0,400;0,500;0,600;0,700;1,400;1,600&family=JetBrains+Mono:wght@400;500;700&display=swap', [], null);

    // Theme main stylesheet
    wp_enqueue_style('proto-previewer-style', get_stylesheet_uri(), [], PROTO_PREVIEWER_VERSION);

    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}
add_action('wp_enqueue_scripts', 'proto_previewer_scripts');

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';
