<?php
/**
 * Gutenberg Typography Functionality
 * 
 * Handles block editor font loading and styling based on customizer settings.
 * 
 * @package Chic Lifestyle
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Block editor assets - fonts and styles
 * 
 * Loads Google Fonts and applies custom typography styles in the block editor
 * based on theme customizer font family settings.
 */
function chic_lifestyle_block_editor_assets() {
    // Only run in admin context
    if ( ! is_admin() ) {
        return;
    }

    $font_family         = get_theme_mod( 'font_family', 'Montserrat' );
    $heading_font_family = get_theme_mod( 'heading_font_family', 'Montserrat' );
    $font_size           = esc_attr(get_theme_mod('font_size', '13px'));
    $font_weight         = absint(get_theme_mod('chic_lifestyle_font_weight', 400));
    $line_height         = absint(get_theme_mod('chic_lifestyle_line_height', 22));
    $heading_font_weight = esc_attr(get_theme_mod('heading_font_weight', 400));
    $text_color          = get_theme_mod('text_color', '#333');
    $heading_title_color = get_theme_mod('heading_title_color', '#f99595');
    $primary_color       = get_theme_mod('primary_colors', '#f99595');

    // Get heading sizes
    $default_size = array(
        '1' =>  35,
        '2' =>  32,
        '3' =>  18,
        '4' =>  17,
        '5' =>  16,
        '6' =>  12,
    );

    $heading = array();
    for ($i = 1; $i <= 6; $i++) {
        $heading[$i] = absint(get_theme_mod('chic_lifestyle_heading_' . $i . '_size', absint($default_size[$i])));
    }

    // Enqueue Google Fonts for block editor
    wp_enqueue_style(
        'chic-lifestyle-editor-googlefonts',
        'https://fonts.googleapis.com/css?family=' . 
          esc_attr( $font_family ) . ':100,200,300,400,500,600,700,800,900|' .
          esc_attr( $heading_font_family ) . ':100,200,300,400,500,600,700,800,900',
        array(),
        null
    );

    // Enqueue editor styles CSS file
    wp_enqueue_style(
        'chic-lifestyle-editor-styles',
        get_template_directory_uri() . '/inc/gutenberg/typography/editor-styles.css',
        array( 'wp-edit-blocks' ),
        filemtime( get_template_directory() . '/inc/gutenberg/typography/editor-styles.css' )
    );

    // Add dynamic font family values as CSS custom properties
    $custom_css = "
    :root {
        --font-family: '{$font_family}';
        --heading-font-family: '{$heading_font_family}';
        --font-size: {$font_size};
        --font-weight: {$font_weight};
        --line-height: {$line_height}px;
        --heading-font-weight: {$heading_font_weight};
        --text-color: {$text_color};
        --heading-title-color: {$heading_title_color};
        --h1-font-size: {$heading[1]}px;
        --h2-font-size: {$heading[2]}px;
        --h3-font-size: {$heading[3]}px;
        --h4-font-size: {$heading[4]}px;
        --h5-font-size: {$heading[5]}px;
        --h6-font-size: {$heading[6]}px;
        --primary-color: {$primary_color};
    }";
    wp_add_inline_style( 'chic-lifestyle-editor-styles', $custom_css );
}
add_action( 'enqueue_block_assets', 'chic_lifestyle_block_editor_assets' );
