<?php
    use BBE\Helpers as Helpers;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Customizer\Section\Hover_Effects as Hover_Effects;
    use BBE\Font_Manager;
    use BBE\Cache_Manager as Cache_Manager;

    /**
     * Controls the length of the excerpt
     * 
     * @since 1.0.0
     */
    add_filter( 'excerpt_length', function(){
        return 25;
    } );

    /**
     * Remove everything after 25 words in excerpt and in place add it replacement
     * 
     * @since 1.0.0 
     */
    add_filter( 'excerpt_more', function(){
        return '...';
    } );

    /**
     * Breadcrumb trails filter
     * 
     * @param string $breadcrumb Full HTML of breadcrumb
     * @since 1.0.0
     */
    add_filter( 'breadcrumb_trail', function( $breadcrumb ){
        $icon_attrs = Theme_Defaults::get_theme_option( 'breadcrumb_icon_picker' );
        $icon_attrs[ 'tag' ] = 'span';
        $icon_attrs[ 'class' ] = 'seperator';

        // Count how many </li> exist
        $count = substr_count( $breadcrumb, '</li>' );

        // Replace all except the last one
        $breadcrumb = preg_replace(
            '/<\/li>/',
            '</li>' . Helpers::get_icon_html( $icon_attrs ),
            $breadcrumb,
            $count - 1  // limit — skips the last </li>
        );

        return $breadcrumb;
    } );

    /**
     * Defer non critical css to prevent css files from blocking render
     * 
     * @param string $html The html tag
     * @param string $handle Tag handle
     * @since 1.0.0
     */
    add_filter( 'style_loader_tag', function( $html, $handle ) {
        if ( strpos( $html, 'onload=' ) !== false ) {
            return $html;
        }
        if( in_array( $handle, [ 'blog-bee-fontawesome', 'blog-bee-slick', 'blog-bee-animation', 'blog-bee-aos', 'blog-bee-customizer-fontawesome', 'blog-bee-typo-fonts', 'blog-bee-customizer-controls' ] ) ) {
            $async = str_replace( "media='print'", "media='print' onload=\"this.media='all'\"", $html );
            $async .= "<noscript>$html</noscript>";
            return $async;
        }
        return $html;
    }, 10, 2 );

    /**
     * Filters the archive title prefix.
     * 
     * @see https://developer.wordpress.org/reference/hooks/get_the_archive_title_prefix/
     * @param string $prefix Archive title prefix
     * @since 1.0.0
     */
    add_filter( 'get_the_archive_title_prefix', function ( $prefix ) {
        return apply_filters( 'blog_bee_archive_page_title_prefix', false );
    } );

    if( ! function_exists( 'blog_bee_preconnect_to_google_fonts' ) ) {
        /**
         * Preconnect to google links to fetch font families
         * 
         * @since 1.0.0
         */
        function blog_bee_preconnect_to_google_fonts() {
            echo '<link rel="preconnect" href="https://fonts.googleapis.com">';
            echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>';

            // Pick the font/weight that's critical for above-the-fold content
            $preload_urls = Font_Manager::get_preload_urls();

            foreach ( $preload_urls as $url ) {
                printf(
                    '<link rel="preload" href="%s" as="font" type="font/woff2" crossorigin>' . "\n",
                    esc_url( $url )
                );
            }
        }
        add_action( 'wp_head', 'blog_bee_preconnect_to_google_fonts' , 1 );
    }