<?php

function local_supermarket_enqueue_fonts() {
    $local_supermarket_default_font_content = 'noto-sans';
    $local_supermarket_default_font_heading = 'noto-sans';

    $local_supermarket_font_map = array(
        'bad-script' => 'Bad Script',
        'inter' => 'Inter',
        'bitter' => 'Bitter',
        'charis-sil' => 'Charis SIL',
        'cuprum' => 'Cuprum',
        'exo-2' => 'Exo 2',
        'jost' => 'Jost',
        'montserrat' => 'Montserrat',
        'open-sans' => 'Open Sans',
        'oswald' => 'Oswald',
        'play' => 'Play',
        'roboto' => 'Roboto',
        'outfit' => 'Outfit',
        'ubuntu' => 'Ubuntu',
        'saira' => 'Saira',
        'cinzel' => 'Cinzel',
        'figtree' => 'Figtree',
        'noto-sans' => 'Noto Sans',
    );

    $local_supermarket_font_content = sanitize_key(get_theme_mod('local_supermarket_content_typography_font', $local_supermarket_default_font_content));
    $local_supermarket_font_heading = sanitize_key(get_theme_mod('local_supermarket_heading_typography_font', $local_supermarket_default_font_heading));

    if ( ! isset( $local_supermarket_font_map[ $local_supermarket_font_content ] ) ) {
        $local_supermarket_font_content = $local_supermarket_default_font_content;
    }
    if ( ! isset( $local_supermarket_font_map[ $local_supermarket_font_heading ] ) ) {
        $local_supermarket_font_heading = $local_supermarket_default_font_heading;
    }

    $local_supermarket_font_content_family = $local_supermarket_font_map[ $local_supermarket_font_content ];
    $local_supermarket_font_heading_family = $local_supermarket_font_map[ $local_supermarket_font_heading ];

    $local_supermarket_css = '';

    // Always enqueue main font
    $local_supermarket_css .= '
    :root {
        --font-main: "' . esc_attr( $local_supermarket_font_content_family ) . '", ' . (in_array($local_supermarket_font_content, ['bitter', 'charis-sil'], true) ? 'serif' : 'sans-serif') . ' !important;
    }';
    wp_enqueue_style('local-supermarket-style-font-general', get_template_directory_uri() . '/fonts/' . $local_supermarket_font_content . '/font.css');

    // Always enqueue header font
    $local_supermarket_css .= '
    :root {
        --font-head: "' . esc_attr( $local_supermarket_font_heading_family ) . '", ' . (in_array($local_supermarket_font_heading, ['bitter', 'charis-sil'], true) ? 'serif' : 'sans-serif') . ' !important;
    }';
    wp_enqueue_style('local-supermarket-style-font-h', get_template_directory_uri() . '/fonts/' . $local_supermarket_font_heading . '/font.css');

    // Add inline style
    wp_add_inline_style('local-supermarket-style-font-general', $local_supermarket_css);
}
add_action('wp_enqueue_scripts', 'local_supermarket_enqueue_fonts', 50);