<?php
/**
 * This file has function for rendering dropdown field.
 *
 * @package woodmart.
 */

use XTS\Admin\Modules\Options\Google_Fonts\Google_Fonts;

if ( ! defined( 'WOODMART_THEME_DIR' ) ) {
	exit( 'No direct script access allowed' );
}

if ( ! function_exists( 'woodmart_get_fonts_param' ) ) {
	/**
	 * This function rendering dropdown field.
	 *
	 * @param array  $settings Settings.
	 * @param string $value    Value.
	 *
	 * @return false|string
	 */
	function woodmart_get_fonts_param( $settings, $value ) {
		$settings['style']         = 'select';
		$settings['devices']       = array(
			'desktop' => array(
				'value' => '',
			),
		);
		$settings['value']         = array(
			esc_html__( 'Default', 'woodmart' ) => '',
		);
		$settings['options_group'] = array();

		// From theme settings.
		$fonts_labels   = woodmart_get_current_theme_settings_fonts_labels(
			array(
				'primary-font',
				'secondary-font',
			)
		);
		$secondary_font = woodmart_get_opt( 'secondary-font' );
		$primary_font   = woodmart_get_opt( 'primary-font' );

		if ( isset( $secondary_font[0] ) && ! empty( $secondary_font[0]['font-family'] ) ) {
			$secondary_font_label = isset( $fonts_labels['secondary-font'] ) ? $fonts_labels['secondary-font'] : '';

			$settings['options_group'][ esc_html__( 'Theme fonts', 'woodmart' ) ][ $secondary_font_label ] = $secondary_font[0]['font-family'];
		}

		if ( isset( $primary_font[0] ) && ! empty( $primary_font[0]['font-family'] ) ) {
			$primary_font_label = isset( $fonts_labels['primary-font'] ) ? $fonts_labels['primary-font'] : '';

			$settings['options_group'][ esc_html__( 'Theme fonts', 'woodmart' ) ][ $primary_font_label ] = $primary_font[0]['font-family'];
		}

		// Custom fonts.
		$custom_fonts_data = woodmart_get_opt( 'multi_custom_fonts' );

		if ( isset( $custom_fonts_data['{{index}}'] ) ) {
			unset( $custom_fonts_data['{{index}}'] );
		}

		if ( is_array( $custom_fonts_data ) ) {
			foreach ( $custom_fonts_data as $font ) {
				if ( ! $font['font-name'] ) {
					continue;
				}

				$settings['options_group'][ esc_html__( 'Custom fonts', 'woodmart' ) ][ $font['font-name'] ] = $font['font-name'];
			}
		}

		// System fonts.
		$system_fonts = woodmart_get_config( 'standard-fonts' );

		foreach ( $system_fonts as $font => $title ) {
			$settings['options_group'][ esc_html__( 'System fonts', 'woodmart' ) ][ $title ] = $font;
		}

		// Typekit fonts.
		$typekit_fonts = woodmart_get_opt( 'typekit_fonts' );

		if ( $typekit_fonts ) {
			$typekit = explode( ',', $typekit_fonts );
			foreach ( $typekit as $font ) {
				$settings['options_group'][ esc_html__( 'Adobe fonts', 'woodmart' ) ][ ucfirst( trim( $font ) ) ] = trim( $font );
			}
		}

		// Google fonts.
		$google_fonts = Google_Fonts::$all_google_fonts;

		if ( $google_fonts ) {
			foreach ( $google_fonts as $font => $data ) {
				$settings['options_group'][ esc_html__( 'Google fonts', 'woodmart' ) ][ ucfirst( trim( $font ) ) ] = trim( $font );
			}
		}

		return woodmart_get_select_param( $settings, $value );
	}
}
