<?php
    /**
     * Archives Section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;
    use BBE\Query_Manager as Query_Manager;
    use BBE\Helpers as Helpers;

    use function esc_html__;
    use function esc_attr;
    use function has_block;
    use function get_media_embedded_in_content;
    use function apply_filters;
    use function get_the_content;

    if( ! class_exists( __NAMESPACE__ . '\\Archives' ) ) {
        /**
         * Archives class
         */
        class Archives extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-archives';

            /**
             * Layout
             * 
             * @since 1.0.0
             */
            private $layout = null;

            /**
             * Whether to position image in zigzag or not
             * 
             * @since 1.0.0
             */
            private $image_alter = null;

            /**
             * Overlay
             * 
             * @since 1.0.0
             */
            private $overlay = null;

            /**
             * Masonry
             * 
             * @since 1.0.0
             */
            private $masonry = null;

            /**
             * Image size
             * 
             * @since 1.0.0
             */
            private $image_size = null;

            /**
             * Column
             * 
             * @since 1.0.0
             */
            private $column = null;

            /**
             * Skip adding loading attribute
             * 
             * @since 1.0.0
             */
            private $skip_loading_attr = null;

            /**
             * Access layout
             * 
             * @since 1.0.0
             */
            public function get_layout(): string {
                if( $this->layout === null ) {
                    $this->layout = Theme_Defaults::get_theme_option( 'archives_layout' );
                }
                return $this->layout;
            }

            /**
             * Access overlay
             * 
             * @since 1.0.0
             */
            public function get_overlay(): string {
                if( $this->overlay === null ) {
                    $this->overlay = Theme_Defaults::get_theme_option( 'archives_enable_overlay' );
                }
                return $this->overlay;
            }

            /**
             * Access image alter
             * 
             * @since 1.0.0
             */
            public function get_image_alter(): string {
                if( $this->image_alter === null ) {
                    $this->image_alter = Theme_Defaults::get_theme_option( 'archives_image_alter' );
                }
                return $this->image_alter;
            }

            /**
             * Access masonry
             * 
             * @since 1.0.0
             */
            public function get_masonry(): string {
                if( $this->masonry === null ) {
                    $this->masonry = Theme_Defaults::get_theme_option( 'archives_enable_masonry' );
                }
                return $this->masonry;
            }

            /**
             * Access masonry
             * 
             * @since 1.0.0
             */
            public function get_image_size(): string {
                if( $this->image_size === null ) {
                    $this->image_size = Theme_Defaults::get_theme_option( 'archives_image_sizes' );
                }
                return $this->image_size;
            }

            /**
             * Access masonry
             * 
             * @since 1.0.0
             */
            public function get_column() {
                if( $this->column === null ) {
                    if( $this->get_layout() === 'grid' ) {
                        $this->column = Theme_Defaults::get_theme_option( 'archives_grid_number_of_column' );
                    } else {
                        $this->column = Theme_Defaults::get_theme_option( 'archives_list_number_of_column' );
                    }
                }
                return $this->column;
            }

            /**
             * Skip loading attr
             * 
             * @since 1.0.0
             */
            public function skip_loading_attr() {
                if( is_archive() || is_search() || is_date() ) {
                    return false;
                }

                $section_reorder = Theme_Defaults::get_theme_option( 'section_reorder_fields' );
                if( is_array( $section_reorder ) && ! empty( $section_reorder ) ) {
                    $first_section = $section_reorder[ 0 ][ 'id' ];
                    if( $first_section === 'latest' ) return true;
                }
            }

            /**
             * Is masonry and overlay both active
             * 
             * @since 1.0.0
             */
            private function is_masonry_and_overlay() {
                return $this->get_overlay() && $this->get_masonry();
            }

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'archives_section' );
                $this->add_control( 'archives_section_tab' );
                $this->add_control( 'archives_layout' );
                $this->add_control( 'archives_sidebar_redirect' );
                $this->add_control( 'archives_section_label' );
                $this->add_control( 'archives_grid_number_of_column' );
                $this->add_control( 'archives_list_number_of_column' );
                $this->add_control( 'archives_image_alter' );
                $this->add_control( 'archives_container' );
                $this->add_control( 'archives_post_elements_settings' );
                $this->add_control( 'archives_content_reorder_with_image' );
                $this->add_control( 'archives_meta_reorder' );
                $this->add_control( 'archives_excerpt_length' );
                $this->add_control( 'archives_alignment' );
                $this->add_control( 'archives_image_settings' );
                $this->add_control( 'archives_image_sizes' );
                $this->tab = 'design';
                $this->add_control( 'archives_title_typography' );
                $this->add_control( 'archives_section_background' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'archives_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_layout' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_image' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_sidebar_redirect' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_section_label' =>  [
                        'sanitize_callback' =>  'sanitize_text_field',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_grid_number_of_column' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_range' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_list_number_of_column' =>  [
                        'sanitize_callback' =>  'absint',
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_image_alter' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_container' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_post_elements_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_content_reorder_with_image' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_meta_reorder' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_reorder' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_excerpt_length' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_alignment' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_radio_tab' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_image_settings' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_image_sizes' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_select' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_title_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'archives_section_background' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_color' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'archives_section' =>  [
                        'title' =>  esc_html__( 'Archives', 'blog-bee' ),
                        'priority'  =>  10,
                        'panel' =>  'blog_panel'
                    ],
                    'archives_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'archives_layout' =>  [
                        'label'    =>  esc_html__( 'Layouts', 'blog-bee' ),
                        'type'  =>  'radio-image',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Grid', 'blog-bee' ),
                                'value' =>  'grid',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/carousel-one.webp'
                            ],
                            [
                                'label' =>  esc_html__( 'List', 'blog-bee' ),
                                'value' =>  'list',
                                'url'   =>  get_template_directory_uri() . '/inc/customizer/assets/images/carousel-three.webp'
                            ],
                        ]
                    ],
                    'archives_sidebar_redirect' =>  [
                        'label'    =>  esc_html__( 'Edit Sidebars', 'blog-bee' ),
                        'type'  =>  'redirect',
                        'section_to_expand' =>  'sidebar_options_section'
                    ],
                    'archives_section_label' =>  [
                        'label'    =>  esc_html__( 'Section Label', 'blog-bee' ),
                        'type'  =>  'bbe-text'
                    ],
                    'archives_grid_number_of_column' =>  [
                        'label'    =>  esc_html__( 'Grid No. of Columns', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'responsive'    =>  true,
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  1,
                            'max'   =>  5,
                            'step'  =>  1
                        ],
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'archives_layout',
                                    'operator'  =>  '==',
                                    'value' =>  'grid'
                                ]
                            ]
                        ]
                    ],
                    'archives_list_number_of_column' =>  [
                        'label'    =>  esc_html__( 'List No. of Columns', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  1,
                            'max'   =>  2,
                            'step'  =>  1
                        ],
                        'conditions'    =>  [
                            'relation'  =>  'AND',
                            'rules' =>  [
                                [
                                    'setting'   =>  'archives_layout',
                                    'operator'  =>  '==',
                                    'value' =>  'list'
                                ]
                            ]
                        ]
                    ],
                    'archives_image_alter' =>  [
                        'label'    =>  esc_html__( 'Image Alter', 'blog-bee' ),
                        'description'    =>  esc_html__( 'Featured images will be displayed in zig-zag format.', 'blog-bee' ),
                        'type'  =>  'toggle-button',
                    ],
                    'archives_container' =>  [
                        'label' =>  esc_html__( 'Container', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_card_options()
                    ],
                    'archives_post_elements_settings' =>  [
                        'label' =>  esc_html__( 'Post Elements Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle',
                    ],
                    'archives_content_reorder_with_image' =>  [
                        'label' =>  esc_html__( 'Content Reorder', 'blog-bee' ),
                        'description' =>  esc_html__( 'Featured Image will not be visible when overlay or image alter options are enabled.', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Category', 'blog-bee' ),
                                'value' =>  'category',
                                'allow_visibility'  =>  true,
                                'redirect'  =>  'category_colors_section'
                            ],
                            [
                                'label' =>  esc_html__( 'Title', 'blog-bee' ),
                                'value' =>  'title',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Featured Image', 'blog-bee' ),
                                'value' =>  'image',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Meta', 'blog-bee' ),
                                'value' =>  'meta',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Excerpt', 'blog-bee' ),
                                'value' =>  'excerpt',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Read More', 'blog-bee' ),
                                'value' =>  'read-more',
                                'allow_visibility'  =>  true,
                                'redirect'  =>  'post_button_section'
                            ],
                        ],
                    ],
                    'archives_meta_reorder' =>  [
                        'label' =>  esc_html__( 'Meta Reorder', 'blog-bee' ),
                        'type'  =>  'reorder',
                        'fields'    =>  [
                            [
                                'label' =>  esc_html__( 'Date', 'blog-bee' ),
                                'value' =>  'date',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Author', 'blog-bee' ),
                                'value' =>  'author',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Comments', 'blog-bee' ),
                                'value' =>  'comment',
                                'allow_visibility'  =>  true
                            ],
                            [
                                'label' =>  esc_html__( 'Read Time', 'blog-bee' ),
                                'value' =>  'read-time',
                                'allow_visibility'  =>  true
                            ],
                        ]
                    ],
                    'archives_excerpt_length'   =>  [
                        'label' =>  esc_html__( 'Excerpt Length', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  0,
                            'max'   =>  50,
                            'step'  =>  1
                        ]
                    ],
                    'archives_alignment'    =>  [
                        'label' =>  esc_html__( 'Alignment', 'blog-bee' ),
                        'type'  =>  'radio-tab',
                        'fields'    =>  Helpers::get_customizer_alignments()
                    ],
                    'archives_image_settings'    =>  [
                        'label' =>  esc_html__( 'Image Settings', 'blog-bee' ),
                        'type'  =>  'heading-toggle'
                    ],
                    'archives_image_sizes'    =>  [
                        'label' =>  esc_html__( 'Image Sizes', 'blog-bee' ),
                        'type'  =>  'bbe-select',
                        'choices'    =>  Helpers::get_customizer_image_sizes()
                    ],
                    'archives_title_typography'    =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                    'archives_section_background'    =>  [
                        'label' =>  esc_html__( 'Section Background', 'blog-bee' ),
                        'type'  =>  'bbe-color',
                        'color_types'   =>  [ 'solid', 'gradient', 'image' ]
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'archives_section_tab' =>  'general',
                    'archives_layout' =>  'grid',
                    'archives_sidebar_redirect' =>  '',
                    'archives_section_label' =>  'Editor\'s Choice',
                    'archives_grid_number_of_column' =>  $this->get_responsive( 4, 2, 1 ),
                    'archives_list_number_of_column' =>  1,
                    'archives_enable_masonry' =>  false,
                    'archives_enable_overlay' =>  false,
                    'archives_image_alter' =>  false,
                    'archives_image_position' =>  'left',
                    'archives_container' =>  'default',
                    'archives_show_media_embed' =>  false,
                    'archives_post_elements_settings'   =>  false,
                    'archives_content_reorder_with_image'  =>  [
                        [
                            'id'  =>  'image',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'category',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'title',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'meta',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'excerpt',
                            'value' =>  false
                        ],
                        [
                            'id'  =>  'read-more',
                            'value' =>  false
                        ]
                    ],
                    'archives_meta_reorder' =>  [
                        [
                            'id'  =>  'author',
                            'value' =>  false
                        ],
                        [
                            'id'  =>  'date',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'comment',
                            'value' =>  true
                        ],
                        [
                            'id'  =>  'read-time',
                            'value' =>  true
                        ]
                    ],
                    'archives_excerpt_length'   =>  15,
                    'archives_alignment'    =>  'left',
                    'archives_image_settings'    =>  false,
                    'archives_image_sizes'    =>  'bbe_post_card',
                    'archives_image_ratio'    =>  'boxy',
                    'archives_title_typography'    =>  $this->get_typography( [
                        'font_weight'   =>  '600',
                        'font_size'   =>  $this->get_responsive( '17px', '17px', '17px' ),
                        'line_height' =>  $this->get_responsive( 1.48, 1.48, 1.48 )
                    ] ),
                    'archives_section_background'    =>  $this->get_color( [ 'value' => '' ] ),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'archives_section_label'    =>  [
                        'selector'  =>  '.blog-bee-main-wrap .section-head .label',
                        'parent'  =>  '.blog-bee-main-wrap .section-head'
                    ],
                    'archives_image_alter'  =>  [
                        'selector'  =>  '.blog-bee-main-wrap',
                        'prefix'    =>  'alter--',
                        'on'    =>  'enabled'
                    ],
                    'archives_container'    =>  [
                        'selector'  =>  '.blog-bee-main-wrap',
                        'prefix'    =>  'card--'
                    ],
                    'archives_alignment'    =>  [
                        'selector'  =>  '.blog-bee-main-wrap',
                        'prefix'    =>  'align--'
                    ],
                    'archives_title_typography'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'archives_title_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'archives_title_typography' ),
                        'selector'  =>  'body.home .blog-bee-main-wrap .site-main .bbe-article .entry-title, body.home .blog-bee-main-wrap .site-main .bbe-article .entry-title a , body.archive .blog-bee-main-wrap .site-main .bbe-article .entry-title, body.archive .blog-bee-main-wrap .site-main .bbe-article .entry-title a'
                    ],
                    'archives_section_background'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'archives_section_background' ),
                        'default' =>  Theme_Defaults::get_defaults( 'archives_section_background' ),
                        'selector'  =>  'body.bbe-light.home .blog-bee-main-wrap, body.bbe-light.archive .blog-bee-main-wrap',
                        'property'  =>  '--bbe-site-bg-color'
                    ],
                ];
                return $dynamic_args;
            }

            /**
             * Get layout
             * 
             * @since 1.0.0
             */
            public function check_layout_post_meta() {
                if( is_category() || is_tag() || is_tax() ) {
                    $queried_object = get_queried_object();
                    $term_id = $queried_object->term_id;
                    if( metadata_exists( 'term', $term_id, 'bbe_taxonomy_layout_meta' ) ) {
                        $layout_meta = get_term_meta( $term_id, 'bbe_taxonomy_layout_meta', true );
                        $grid_noc_desktop = get_term_meta( $term_id, 'bbe_taxonomy_grid_noc_desktop', true );
                        $grid_noc_tablet = get_term_meta( $term_id, 'bbe_taxonomy_grid_noc_tablet', true );
                        $grid_noc_mobile = get_term_meta( $term_id, 'bbe_taxonomy_grid_noc_mobile', true );
                        $list_column = get_term_meta( $term_id, 'bbe_taxonomy_list_noc', true );
                        $grid_column = [
                            'desktop'   =>  $grid_noc_desktop,
                            'tablet'   =>  $grid_noc_tablet,
                            'mobile'   =>  $grid_noc_mobile,
                        ];
                        $is_customizer = ( $layout_meta === 'customizer-layout' );
                        switch( $layout_meta ) {
                            case 'grid' :
                                    $this->layout = $layout_meta;
                                    if( ! $is_customizer ) $this->column = $grid_column;
                                break;
                            case 'list' :
                                    $this->layout = $layout_meta;
                                    if( ! $is_customizer ) $this->column = $list_column;
                                break;
                            case 'masonry':
                                    $this->masonry = true;
                                    if( ! $is_customizer ) $this->column = $grid_column;
                                break;
                            case 'list-overlay':
                                    $this->layout = 'list';
                                    $this->overlay = true;
                                    if( ! $is_customizer ) $this->column = $list_column;
                                break;
                            case 'grid-overlay':
                                    $this->layout = 'grid';
                                    $this->overlay = true;
                                    if( ! $is_customizer ) $this->column = $grid_column;
                                break;
                            case 'list-alter':
                                    $this->layout = 'list';
                                    $this->image_alter = true;
                                    if( ! $is_customizer ) $this->column = $list_column;
                                break;
                            case 'grid-alter':
                                    $this->layout = 'grid';
                                    $this->image_alter = true;
                                    if( ! $is_customizer ) $this->column = $grid_column;
                                break;
                        }
                    }
                }
            }

            /**
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $meta_reorder = Theme_Defaults::get_theme_option( 'archives_meta_reorder' );
                $excerpt_length = Theme_Defaults::get_theme_option( 'archives_excerpt_length' );
                $image_size = Theme_Defaults::get_theme_option( 'archives_image_sizes' );
                $is_list = ( $this->get_layout() === 'list' );
                $is_grid = ( $this->get_layout() === 'grid' );
                $content_reorder = $this->get_visible_fields( Theme_Defaults::get_theme_option( 'archives_content_reorder_with_image' ) );
                if( $this->get_overlay() || $this->get_image_alter() || $is_list ) {
                    $is_image_outside = true;
                } else {
                    $is_image_outside = false;
                }
                if( $is_grid && $this->get_image_alter() ) $is_image_outside = false;
                ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class( 'bbe-article' ); ?> <?php Helpers::the_aos_data_attr(); ?>>

                        <?php

                            echo '<div class="entry-content">';

                                if( $is_image_outside ) :

                                    Query_Manager::get_opening_figure_tag();

                                        $this->get_featured_image();

                                    if( ! $this->get_overlay() ) Query_Manager::get_closing_figure_tag();

                                endif;

                                if( ! empty( $content_reorder ) && is_array( $content_reorder ) ) :

                                    $image_index = $this->get_image_index( $content_reorder );

                                    $array_count = count( $content_reorder );

                                    if( $is_image_outside ) echo '<div class="entry-inner">';

                                    foreach( $content_reorder as $index => $content ) :
                                        $is_image_first = ( $image_index === 0 );
                                        $is_image_last = ( $image_index + 1 === $array_count );

                                        if( ! $is_image_outside && ( ( $image_index !== 0 && $index === 0 ) || ( $is_image_first && $index === 1 ) || ( $image_index + 1 === $index ) || ( $is_image_last && $index === 0 ) ) ) echo '<div class="entry-inner">';

                                        switch( $content[ 'id' ] ) :

                                            case 'title':
                                                    Query_Manager::get_title();
                                                break;

                                            case 'image':
                                                    if( ! $is_image_outside ) { 

                                                        Query_Manager::get_opening_figure_tag();
                                                        
                                                            $this->get_featured_image();
                                                        
                                                        Query_Manager::get_closing_figure_tag();

                                                    }
                                                break;

                                            case 'excerpt':
                                                    Query_Manager::get_excerpt( $excerpt_length );
                                                break;

                                            case 'meta':

                                                    echo '<div class="entry-meta">';
                                                
                                                        if( ! empty( $meta_reorder ) && is_array( $meta_reorder ) ) :

                                                            foreach( $meta_reorder as $meta ) :
                                                                
                                                                if( ! $meta[ 'value' ] ) continue;

                                                                switch( $meta[ 'id' ] ) :

                                                                    case 'author':
                                                                            Query_Manager::get_author();
                                                                        break;

                                                                    case 'comment':
                                                                            Query_Manager::get_comments();
                                                                        break;

                                                                    case 'read-time':
                                                                            Query_Manager::get_read_time();
                                                                        break;

                                                                    case 'date':
                                                                            Query_Manager::get_date();
                                                                        break;

                                                                endswitch;

                                                            endforeach;

                                                        endif;

                                                    echo '</div><!-- .entry-meta -->';

                                                break;

                                            case 'read-more':
                                                    Query_Manager::get_read_more_button();
                                                break;

                                            case 'category':
                                                    Query_Manager::get_categories();
                                                break;

                                        endswitch;

                                        if( ! $is_image_outside && ( ( ! $is_image_first && ! $is_image_last && ( $index + 1 ) === $image_index ) ||  ( ! $is_image_last && ( $array_count === ( $index + 1 ) ) ) || ( $is_image_last && ( $index === ( $array_count - 2 ) ) ) ) ) echo '</div><!-- .entry-inner -->';

                                    endforeach;

                                    if( $is_image_outside ) echo '</div>';

                                endif;

                                if( $is_image_outside && $this->get_overlay() ) Query_Manager::get_closing_figure_tag();
                            
                            echo '</div><!-- .entry-content -->';

                        ?>

                        <footer class="entry-footer">

                            <?php blog_bee_entry_footer(); ?>

                        </footer><!-- .entry-footer -->

                    </article><!-- #post-<?php the_ID(); ?> -->
                <?php
            }

            /**
             * Section title
             * 
             * @since 1.0.0
             */
            public function render_section_title() {
                $label = Theme_Defaults::get_theme_option( 'archives_section_label' );
                Helpers::get_section_head( $label );
            }

            /**
             * Get image index
             * 
             * @param array $reorder_control The reorder control value
             * @since 1.0.0
             */
            private function get_image_index( $reorder_control ): int {
                $array = array_column( $reorder_control, 'id' );
                return array_search( 'image', $array );
            }

            /**
             * Get featured image
             * 
             * @since 1.0.0
             */
            private function get_featured_image() {
                $show_embed = Theme_Defaults::get_theme_option( 'archives_show_media_embed' );
                if( $this->get_overlay() || ! $show_embed ) return Query_Manager::get_featured_image( $this->get_image_size(), [ 'post_format'  => ! $this->is_masonry_and_overlay(), 'add_loading' => $this->skip_loading_attr() ] );
                $post_format = get_post_format();
                switch( $post_format ) {
                    case 'audio':
                            $this->get_audio_as_featured_image();
                        break;
                    case 'video':
                            $this->get_video_as_featured_image();
                        break;
                    default:
                            Query_Manager::get_featured_image( $this->get_image_size(), [ 'post_format' => ! $this->is_masonry_and_overlay(), 'add_loading' => $this->skip_loading_attr() ] );
                        break;
                }
            }

            /**
             * Archive Audio
             * 
             * @since 1.0.0
             */
            private function get_audio_as_featured_image() {
                $is_soundcloud = false;
                $embeded_content = '';
                if( has_block( 'core/embed' ) ) {
                    $embeds = get_media_embedded_in_content( apply_filters( 'the_content', get_the_content() ) );
                    foreach( $embeds as $embed ) {
                        if( strpos( $embed, 'soundcloud' ) || strpos( $embed, 'spotify' ) ) {
                            $is_soundcloud = true;
                            $embeded_content = $embed;
                            break; 
                        }
                    }
                }

                if( $is_soundcloud ) {
                    echo $embeded_content;
                } else {
                    Query_Manager::get_featured_image( $this->get_image_size(), [ 'post_format' => ! $this->is_masonry_and_overlay(), 'add_loading' => $this->skip_loading_attr() ] );
                }
            }

            /**
             * Archive Video
             * 
             * @since 1.0.0
             */
            private function get_video_as_featured_image() {
                if( has_block( 'core/embed' ) || has_block( 'core/video' ) ) {
                    $embeded_content = '';
                    $embeds = get_media_embedded_in_content( apply_filters( 'the_content', get_the_content() ) );
                    foreach( $embeds as $embed ) {
                        if( strpos( $embed, 'youtube' ) || strpos( $embed, 'video' ) ) {
                            $embeded_content = $embed;
                            break; 
                        }
                    }
                    echo '<div class="video-overlay"></div>', $embeded_content;
                } else {
                    Query_Manager::get_featured_image( $this->get_image_size(), [ 'post_format' => ! $this->is_masonry_and_overlay(), 'add_loading' => $this->skip_loading_attr() ] );
                }
            }

            /**
             * Get only visible fields
             * 
             * @param array $content_reorder The array fields
             * @since 1.0.0
             */
            private function get_visible_fields( $content_reorder ) {
                return array_filter( $content_reorder, function( $item ) {
                    if( $item[ 'value' ] ) {
                        return $item[ 'id' ];
                    }
                } );
            }
        }
        Archives::get_instance();
    }