<?php
use OneNewspaper\CustomizerDefault as ONP;
/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
add_action( 'customize_preview_init', function() {
    $enqueue_script_args = [ 'strategy' => 'defer', 'in_footer' => true ];
    wp_enqueue_script( 
        'one-newspaper-customizer-preview',
        get_template_directory_uri() . '/inc/customizer/assets/customizer-preview.js',
        [ 'customize-preview' ],
        ONE_NEWSPAPER_VERSION,
        $enqueue_script_args
    );
});

add_action( 'customize_controls_enqueue_scripts', function() {
    $enqueue_script_args = [ 'strategy' => 'defer', 'in_footer' => true ];
    $buildControlsDeps = apply_filters(  'one_newspaper_customizer_build_controls_dependencies', [
        'wp-element',
        'react',
        'wp-blocks',
        'wp-editor',
        'wp-i18n',
        'wp-polyfill',
        'jquery',
        'wp-components'
    ]);
	wp_enqueue_style( 
        'one-newspaper-builder-style',
        get_template_directory_uri() . '/inc/customizer/assets/builder.min.css', 
        [ 'wp-components' ],
        ONE_NEWSPAPER_VERSION,
        'all'
    );
	wp_enqueue_style( 
        'one-newspaper-customizer-control',
        get_template_directory_uri() . '/inc/customizer/assets/customizer-controls.min.css', 
        [ 'wp-components' ],
        ONE_NEWSPAPER_VERSION,
        'all'
    );
    wp_enqueue_script( 
        'one-newspaper-customizer-control',
        get_template_directory_uri() . '/inc/customizer/assets/customizer-extends.min.js',
        $buildControlsDeps,
        ONE_NEWSPAPER_VERSION,
        $enqueue_script_args
    );

    wp_enqueue_script( 
        'one-newspaper-customizer-extras',
        get_template_directory_uri() . '/inc/customizer/assets/extras.min.js',
        [ 'jquery', 'customize-controls' ],
        ONE_NEWSPAPER_VERSION,
        $enqueue_script_args
    );
    // localize scripts
    wp_localize_script( 
        'one-newspaper-customizer-control', 
        'customizerControlsObject', [
            'imageSizes'    => one_newspaper_get_image_sizes_option_array(),
            'templateDirectoryUri'  =>  get_template_directory_uri(),
            'homeUrl'   =>  home_url()
        ]
    );
    // localize scripts
    wp_localize_script( 
        'one-newspaper-customizer-extras', 
        'customizerExtrasObject', array(
            '_wpnonce'	=> wp_create_nonce( 'one-newspaper-customizer-controls-nonce' ),
            'ajaxUrl' => esc_url( admin_url( 'admin-ajax.php' ) ),
            'custom_callback'   =>  [
                'preloader_option'  =>  [
                    'true'  =>  [ 'preloader_type' ]
                ],
                'website_layout'    =>  [
                    'boxed--layout'  =>  [ 'website_box_shadow', 'website_layout_horizontal_gap', 'website_layout_vertical_gap', 'website_layout_container_setting_heading', 'website_layout_background_color' ]
                ],
                'bottom_footer_header_or_custom' =>  [
                    'custom' =>  [ 'bottom_footer_logo_option', 'bottom_footer_logo_width' ]
                ],
                'archive_pagination_type'   =>  [
                    'number'    =>  [ 'pagination_button_background_color' ]
                ],
                /* Header Builder custom callbacks */
                'header_first_row_column'   =>  [
                    '1' =>  [ 'header_first_row_column_one' ],
                    '2' =>  [ 'header_first_row_column_one', 'header_first_row_column_two' ],
                    '3' =>  [ 'header_first_row_column_one', 'header_first_row_column_two', 'header_first_row_column_three' ]
                ],
                'header_second_row_column'  =>  [
                    '1' =>  [ 'header_second_row_column_one' ],
                    '2' =>  [ 'header_second_row_column_one', 'header_second_row_column_two' ],
                    '3' =>  [ 'header_second_row_column_one', 'header_second_row_column_two', 'header_second_row_column_three' ]
                ],
                'header_third_row_column'   =>  [
                    '1' =>  [ 'header_third_row_column_one' ],
                    '2' =>  [ 'header_third_row_column_one', 'header_third_row_column_two' ],
                    '3' =>  [ 'header_third_row_column_one', 'header_third_row_column_two', 'header_third_row_column_three' ]
                ],
                /* Footer Builder custom callbacks */
                'footer_first_row_column'   =>  [
                    '1' =>  [ 'footer_first_row_column_one' ],
                    '2' =>  [ 'footer_first_row_column_one', 'footer_first_row_column_two' ],
                    '3' =>  [ 'footer_first_row_column_one', 'footer_first_row_column_two', 'footer_first_row_column_three' ],
                    '4' =>  [ 'footer_first_row_column_one', 'footer_first_row_column_two', 'footer_first_row_column_three', 'footer_first_row_column_four' ],
                ],
                'footer_second_row_column'  =>  [
                    '1' =>  [ 'footer_second_row_column_one' ],
                    '2' =>  [ 'footer_second_row_column_one', 'footer_second_row_column_two' ],
                    '3' =>  [ 'footer_second_row_column_one', 'footer_second_row_column_two', 'footer_second_row_column_three' ],
                    '4' =>  [ 'footer_second_row_column_one', 'footer_second_row_column_two', 'footer_second_row_column_three', 'footer_second_row_column_four' ],
                ],
                'footer_third_row_column'   =>  [
                    '1' =>  [ 'footer_third_row_column_one' ],
                    '2' =>  [ 'footer_third_row_column_one', 'footer_third_row_column_two' ],
                    '3' =>  [ 'footer_third_row_column_one', 'footer_third_row_column_two', 'footer_third_row_column_three' ],
                    '4' =>  [ 'footer_third_row_column_one', 'footer_third_row_column_two', 'footer_third_row_column_three', 'footer_third_row_column_four' ],
                ],
                'header_buiilder_header_sticky' =>  [
                    'true'  =>  [ 'header_first_row_header_sticky', 'header_second_row_header_sticky', 'header_third_row_header_sticky' ]
                ],
            ],
            'custom'    =>  [
                'single_section_panel'   =>  one_newspaper_wp_query( 'post' ),
                'page_settings_section'   =>  one_newspaper_wp_query( 'page' ),
                'archive_panel'   =>  home_url() . '/',
                '404_section'   =>  home_url() . '/~~~hfieojfw',
                'search_page_settings'  =>  home_url() . '?s=a',
            ],
        )
    );
});

if( ! function_exists( 'one_newspaper_wp_query' ) ) :
    /**
     * Returns permalink
     * 
     * @param post_type
     * @since 1.0.0
     * @package Blogzee Pro
     */
    function one_newspaper_wp_query( $type ) {
        $permalink = home_url();
        switch( $type ) :
            case ( in_array( $type, [ 'page', 'post' ] ) ):
                    $type_args = [
                        'post_type'	=>	$type,
                        'posts_per_page'	=>	1,
                        'orderby'	=>	'rand',
                        'fields'    =>  'ids'
                    ];
                    if( $type == 'search' ) $type_args['s'] = 'a';
                    $type_query = new \WP_Query( apply_filters( 'blogzee_query_args_filter', $type_args ) );
                    if( $type_query->have_posts() ) :
                        while( $type_query->have_posts() ):
                            $type_query->the_post();
                            $permalink = get_the_permalink();
                        endwhile;
                        wp_reset_postdata();
                    endif;
                    return $permalink;
                break;
            case ( in_array( $type, [ 'tag', 'category' ] ) ):
                    $nexus_collective = function( $args ){
                        return get_terms( $args );
                    };
                    $taxonomy = ( $type == 'category' ) ? 'category' : 'post_tag';
                    $total = count( $nexus_collective([ 'taxonomy'  =>  $taxonomy, 'number' => 0, 'fields'    =>  'ids' ]) );
                    $random_number = rand( 0, ( $total - 1 ) );
                    $taxonomy_args = [
                        'orderby'   =>  'rand',
                        'number'    =>  1,
                        'taxonomy'  =>  $taxonomy,
                        'offset'	=>	$random_number,
                        'fields'    =>  'ids'
                    ];
                    $get_taxonomies = $nexus_collective( $taxonomy_args );
                    if( ! empty( $get_taxonomies ) && is_array( $get_taxonomies ) ) :
                        foreach( $get_taxonomies as $taxonomy ) :
                            $permalink = get_term_link( $taxonomy );
                        endforeach;
                    endif;
                    return $permalink;
                break;
            case 'author':
                    $nexus_collective = function( $args ) {
                        return new \WP_User_Query( $args );
                    };
                    $total = $nexus_collective( [ 'number' => 0, 'fields'    =>  'ids' ] )->get_total();
                    $random_number = rand( 0, ( $total - 1 ) );
                    $author_args = [
                        'number'    =>  1,
                        'offset'    =>  $random_number
                    ];
                    $user_query = $nexus_collective( $author_args );
                    if ( ! empty( $user_query->get_results() ) ) :
                        foreach ( $user_query->get_results() as $user ) :
                            $permalink = get_author_posts_url( $user->data->ID );
                        endforeach;
                    endif;
                    wp_reset_postdata();
                    return $permalink;
                break;
        endswitch;
    }
endif;