<?php
/**
 * Topbar Options
 */

$wp_customize->add_section(
	'early_news_topbar_options',
	array(
		'title' => esc_html__( 'Topbar Options', 'early-news' ),
		'panel' => 'early_news_theme_options_panel',
	)
);

// Highlights News section enable settings.
$wp_customize->add_setting(
	'early_news_highlights_news_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'early_news_sanitize_checkbox',
	)
);
$wp_customize->add_control(
	new Early_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'early_news_highlights_news_section_enable',
		array(
			'label'    => esc_html__( 'Enable Highlights News Section', 'early-news' ),
			'type'     => 'checkbox',
			'settings' => 'early_news_highlights_news_section_enable',
			'section'  => 'early_news_topbar_options',
		)
	)
);

// Highlights news content type settings.
$wp_customize->add_setting(
	'early_news_highlights_news_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'early_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'early_news_highlights_news_content_type',
	array(
		'label'           => esc_html__( 'Content type:', 'early-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'early-news' ),
		'section'         => 'early_news_topbar_options',
		'type'            => 'select',
		'active_callback' => 'early_news_if_highlights_news_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'early-news' ),
			'category' => esc_html__( 'Category', 'early-news' ),
		),
	)
);

for ( $i = 1; $i <= 5; $i++ ) {
	// Highlights news post setting.
	$wp_customize->add_setting(
		'early_news_highlights_news_post_' . $i,
		array(
			'sanitize_callback' => 'early_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'early_news_highlights_news_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'early-news' ), $i ),
			'section'         => 'early_news_topbar_options',
			'type'            => 'select',
			'choices'         => early_news_get_post_choices(),
			'active_callback' => 'early_news_highlights_news_section_content_type_post_enabled',
		)
	);

}

// Highlights news category setting.
$wp_customize->add_setting(
	'early_news_highlights_news_category',
	array(
		'sanitize_callback' => 'early_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'early_news_highlights_news_category',
	array(
		'label'           => esc_html__( 'Category', 'early-news' ),
		'section'         => 'early_news_topbar_options',
		'type'            => 'select',
		'choices'         => early_news_get_post_cat_choices(),
		'active_callback' => 'early_news_highlights_news_section_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function early_news_if_highlights_news_enabled( $control ) {
	return $control->manager->get_setting( 'early_news_highlights_news_section_enable' )->value();
}
function early_news_highlights_news_section_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'early_news_highlights_news_content_type' )->value();
	return early_news_if_highlights_news_enabled( $control ) && ( 'post' === $content_type );
}
function early_news_highlights_news_section_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'early_news_highlights_news_content_type' )->value();
	return early_news_if_highlights_news_enabled( $control ) && ( 'category' === $content_type );
}

/*========================Partial Refresh==============================*/
if ( ! function_exists( 'early_news_highlights_news_title_text_partial' ) ) :
	// Title.
	function early_news_highlights_news_title_text_partial() {
		return esc_html( get_theme_mod( 'early_news_highlights_news_title' ) );
	}
endif;
