<?php
/**
 * Giuliomax Menu Builder — Assets
 * Enqueues frontend and admin scripts/styles.
 *
 * @package GiuliomaxMenuBuilder
 */

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

/**
 * Returns the combined generated CSS (main + accessibility), rebuilding from
 * scratch only when the transient is missing (i.e. after a settings save).
 */
// Version-keyed so a plugin update always serves freshly generated CSS without
// waiting for a settings save or the 24h expiry (old-version transients linger
// harmlessly until they expire).
function menux_css_cache_key() {
    return 'menux_css_cache_' . MENUX_VERSION;
}

function menux_get_cached_css() {
    $cached = get_transient( menux_css_cache_key() );
    if ( false !== $cached ) {
        return $cached;
    }
    $css = menux_generate_css( get_option( 'menux_style', array() ) );
    if ( class_exists( 'Menux_Accessibility' ) ) {
        $css .= Menux_Accessibility::generate_css();
    }
    $menu_items_for_css = get_option( 'menux_menu_items', array() );
    $css .= Menux_MegaMenu::generate_items_css( $menu_items_for_css, get_option( 'menux_style', array() ) );
    // Footer Builder CSS — only when the footer is enabled and configured.
    if ( function_exists( 'menux_generate_footer_css' ) ) {
        $footer_opt = get_option( 'menux_footer', array() );
        if ( ( $footer_opt['enabled'] ?? '0' ) === '1' && ! empty( $footer_opt['columns'] ) ) {
            $css .= menux_generate_footer_css( get_option( 'menux_footer_style', array() ) );
        }
    }
    set_transient( menux_css_cache_key(), $css, DAY_IN_SECONDS );
    return $css;
}

function menux_invalidate_css_cache() {
    delete_transient( menux_css_cache_key() );
    delete_transient( 'menux_css_cache' ); // legacy key from < 3.7.0
}
// update_option_{name} fires only when the option already exists and changes;
// add_option_{name} covers the very first save on a fresh install.
foreach ( array( 'menux_style', 'menux_logo', 'menux_accessibility', 'menux_menu_items', 'menux_footer', 'menux_footer_style' ) as $menux_css_opt ) {
    add_action( 'update_option_' . $menux_css_opt, 'menux_invalidate_css_cache' );
    add_action( 'add_option_' . $menux_css_opt,    'menux_invalidate_css_cache' );
}
unset( $menux_css_opt );

add_action( 'wp_enqueue_scripts', 'menux_enqueue_frontend' );
function menux_enqueue_frontend() {
    $style = get_option( 'menux_style', array() );
    if ( ! empty( $style['google_font'] ) ) {
        $gf_slug = urlencode( $style['google_font'] );
        wp_enqueue_style( 'menux-gfont', "https://fonts.googleapis.com/css2?family={$gf_slug}:wght@300;400;500;600;700&display=swap", array(), MENUX_VERSION );
    }
    wp_enqueue_style( 'menux-fa6', MENUX_URL . 'assets/fa6/css/all.min.css', array(), '6.5.2' );

    wp_add_inline_style( 'menux-fa6', wp_strip_all_tags( menux_get_cached_css() ) );

    wp_register_script( 'menux-frontend', MENUX_URL . 'assets/js/frontend.js', array(), MENUX_VERSION, true );
    wp_add_inline_script( 'menux-frontend', 'window.menuxI18n=' . wp_json_encode( array(
        'back'           => __( 'Back', 'giuliomax-menu-builder' ),
        'startTyping'    => __( 'Start typing to search', 'giuliomax-menu-builder' ),
        'searchHint'     => sprintf(
            /* translators: 1: "Menu" tab name, 2: "In page" tab name */
            __( 'Tab %1$s: search menu links · Tab %2$s: find text on the current page', 'giuliomax-menu-builder' ),
            __( 'Menu', 'giuliomax-menu-builder' ),
            __( 'In page', 'giuliomax-menu-builder' )
        ),
        'noResultsFor'   => __( 'No results for', 'giuliomax-menu-builder' ),
        'zeroResults'    => __( '0 results', 'giuliomax-menu-builder' ),
        /* translators: 1: current match number, 2: total matches */
        'matchOf'        => __( 'Match %1$s of %2$s', 'giuliomax-menu-builder' ),
        /* translators: %s: number of occurrences */
        'occurrences'    => __( '%s occurrence', 'giuliomax-menu-builder' ),
        /* translators: %s: number of occurrences */
        'occurrencesPlural' => __( '%s occurrences', 'giuliomax-menu-builder' ),
        /* translators: %s: number of matches found */
        'found'          => __( '%s found', 'giuliomax-menu-builder' ),
    ) ) . ';', 'before' );
}

add_action( 'admin_head', 'menux_remove_admin_footer' );
function menux_remove_admin_footer() {
    $screen = get_current_screen();
    if ( ! $screen ) return;
    if ( in_array( $screen->id, array( 'toplevel_page_menux' ), true ) ) {
        add_filter( 'admin_footer_text', '__return_empty_string', 99 );
        add_filter( 'update_footer',     '__return_empty_string', 99 );
    }
}

add_action( 'admin_enqueue_scripts', 'menux_footer_admin_assets' );
function menux_footer_admin_assets( $hook ) {
    // Submenu hook name depends on the localized parent title, so match on the slug.
    if ( strpos( (string) $hook, 'menux-footer' ) === false ) return;
    wp_enqueue_style( 'menux-fa6-admin', MENUX_URL . 'assets/fa6/css/all.min.css', array(), '6.5.2' );
    wp_enqueue_script( 'menux-footer-admin', MENUX_URL . 'admin/js/footer-admin.js', array(), MENUX_VERSION, true );
}

add_action( 'admin_enqueue_scripts', 'menux_admin_assets' );
function menux_admin_assets( $hook ) {
    if ( $hook !== 'toplevel_page_menux' ) return;
    wp_enqueue_script( 'jquery-ui-sortable' );
    wp_enqueue_media();
    wp_enqueue_style( 'menux-fa6-admin', MENUX_URL . 'assets/fa6/css/all.min.css', array(), '6.5.2' );
    wp_enqueue_style( 'menux-admin-css', MENUX_URL . 'admin/css/admin.css', array(), MENUX_VERSION );
    if ( class_exists( 'Menux_Logo' ) ) {
        $logo_css_admin = Menux_Logo::generate_css();
        if ( $logo_css_admin ) {
            wp_add_inline_style( 'menux-admin-css', wp_strip_all_tags( $logo_css_admin ) );
        }
    }
    wp_enqueue_script( 'menux-admin-js', MENUX_URL . 'admin/js/admin.js', array( 'jquery', 'jquery-ui-sortable' ), MENUX_VERSION, true );
}
