<?php
/**
 * Audace theme bootstrap.
 */

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

define( 'AUDACE_VERSION', '0.5.9' );
define( 'AUDACE_DIR', get_template_directory() );
define( 'AUDACE_URI', get_template_directory_uri() );

require_once AUDACE_DIR . '/inc/template-parts.php';
require_once AUDACE_DIR . '/inc/block-styles.php';
require_once AUDACE_DIR . '/inc/patterns.php';
require_once AUDACE_DIR . '/inc/customizer.php';
require_once AUDACE_DIR . '/inc/nav-icon.php';
require_once AUDACE_DIR . '/inc/hide-title.php';
require_once AUDACE_DIR . '/inc/columns-width.php';
require_once AUDACE_DIR . '/inc/expertise-cpt.php';
require_once AUDACE_DIR . '/inc/production-cpt.php';
require_once AUDACE_DIR . '/inc/site-logo-width.php';
require_once AUDACE_DIR . '/inc/blocks.php';

add_action( 'after_setup_theme', function () {
    add_theme_support( 'wp-block-styles' );
    add_theme_support( 'editor-styles' );
    add_editor_style( 'assets/css/editor.css' );
    load_theme_textdomain( 'audace', AUDACE_DIR . '/languages' );
} );

add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_style(
        'audace-front',
        AUDACE_URI . '/assets/css/front.css',
        array(),
        AUDACE_VERSION
    );
    wp_enqueue_script(
        'audace-menu-overlay',
        AUDACE_URI . '/assets/js/menu-overlay.js',
        array(),
        AUDACE_VERSION,
        true
    );
    wp_enqueue_script(
        'audace-scroll-snap',
        AUDACE_URI . '/assets/js/scroll-snap.js',
        array(),
        AUDACE_VERSION,
        true
    );
} );

// Load audace-scroll-snap as an ES module so it can `import` from esm.sh.
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
    if ( 'audace-scroll-snap' === $handle ) {
        return str_replace( '<script ', '<script type="module" ', $tag );
    }
    return $tag;
}, 10, 2 );

add_action( 'enqueue_block_editor_assets', function () {
    $editor_deps = array( 'wp-blocks', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-compose', 'wp-block-editor', 'wp-components' );
    wp_enqueue_script(
        'audace-nav-icon-control',
        AUDACE_URI . '/assets/js/nav-icon-control.js',
        array_merge( $editor_deps, array( 'wp-data' ) ),
        AUDACE_VERSION,
        true
    );
    wp_enqueue_script(
        'audace-nav-hover-color',
        AUDACE_URI . '/assets/js/nav-hover-color.js',
        $editor_deps,
        AUDACE_VERSION,
        true
    );
    wp_enqueue_script(
        'audace-full-height-block',
        AUDACE_URI . '/assets/js/full-height-block.js',
        $editor_deps,
        AUDACE_VERSION,
        true
    );
    wp_enqueue_script(
        'audace-hide-title-panel',
        AUDACE_URI . '/assets/js/hide-title-panel.js',
        array( 'wp-plugins', 'wp-editor', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data', 'wp-i18n' ),
        AUDACE_VERSION,
        true
    );
    wp_enqueue_script(
        'audace-production-file-panel',
        AUDACE_URI . '/assets/js/production-file-panel.js',
        array( 'wp-plugins', 'wp-editor', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-data', 'wp-i18n' ),
        AUDACE_VERSION,
        true
    );
    wp_enqueue_script(
        'audace-nav-overlay-redirect',
        AUDACE_URI . '/assets/js/nav-overlay-redirect.js',
        array(),
        AUDACE_VERSION,
        true
    );
    wp_localize_script( 'audace-nav-overlay-redirect', 'AudaceOverlay', array(
        'editUrl' => admin_url(
            'site-editor.php?postType=wp_template_part&postId=' .
            rawurlencode( get_stylesheet() . '//menu-overlay' ) . '&canvas=edit'
        ),
    ) );
} );

add_action( 'enqueue_block_editor_assets', function () {
    wp_add_inline_script(
        'audace-expertise-showcase-editor-script',
        'window.AudaceBlocks = Object.assign(window.AudaceBlocks || {}, { themeUri: ' . wp_json_encode( AUDACE_URI ) . ' });',
        'before'
    );
}, 20 );

/**
 * Inject the menu-overlay template part at the very start of <body>.
 * Hidden by default; the menu-overlay JS toggles it from the Navigation block burger.
 */
add_action( 'wp_body_open', function () {
    echo '<div id="audace-menu-overlay" class="audace-menu-overlay" hidden aria-hidden="true" role="dialog" aria-modal="true" aria-label="' . esc_attr__( 'Menu', 'audace' ) . '">';
    echo '<button type="button" class="audace-menu-overlay__close" aria-label="' . esc_attr__( 'Fermer le menu', 'audace' ) . '">&times;</button>';
    echo '<div class="audace-menu-overlay__inner">';
    block_template_part( 'menu-overlay' );
    echo '</div>';
    echo '</div>';
} );
