<?php

/**
 * Global color
 */
FLPageData::add_group( 'bb', array(
	'label'  => __( 'Global Colors', 'fl-builder' ),
	'render' => false,
) );

$settings      = self::get_settings( false );
$prefix        = ! empty( $settings->prefix ) ? self::label_to_key( $settings->prefix ) : 'fl-global';
$global_colors = isset( $settings->colors ) ? $settings->colors : false;

if ( ! empty( $global_colors ) ) {
	foreach ( $global_colors as $color ) {
		if ( empty( $color ) || empty( $color['color'] ) || empty( $color['label'] ) ) {
			continue;
		}

		FLPageData::add_site_property( 'global_color_' . $color['uid'], array(
			'label'  => sprintf( '<span class="prefix" title="%2$s">%1$s<span> - <span title="%2$s">%2$s</span><span class="swatch" style="background-color: %3$s"></span>', esc_attr( __( 'Global', 'fl-builder' ) ), $color['label'], FLBuilderColor::hex_or_rgb( $color['color'] ) ),
			'group'  => 'bb',
			'type'   => 'color',
			'getter' => function () use ( $prefix, $color ) {
				return 'var(--' . $prefix . '-' . self::label_to_key( $color['label'] ) . ')';
			},
		) );
	}
}

/**
 * Gutenberg color
 */
if ( class_exists( 'WP_Theme_JSON_Resolver' ) ) {
	$settings = WP_Theme_JSON_Resolver::get_merged_data()->get_settings();
	/**
	 * @since 2.8
	 * @see fl_wp_core_global_colors
	 */
	$colors = isset( $settings['color']['palette']['default'] ) ? apply_filters( 'fl_wp_core_global_colors', $settings['color']['palette']['default'] ) : array();
	if ( ! empty( $colors ) ) {
		FLPageData::add_group( 'core', array(
			'label'  => __( 'WordPress Colors', 'fl-builder' ),
			'render' => false,
		) );

		foreach ( $colors as $color ) {
			FLPageData::add_site_property( 'theme_color_' . $color['slug'], array(
				'label'  => sprintf( '<span class="prefix" title="%2$s">%1$s<span> - <span title="%2$s">%2$s</span><span class="swatch" style="background-color: %3$s"></span>', esc_attr( __( 'WordPress', 'fl-builder' ) ), $color['name'], FLBuilderColor::hex_or_rgb( $color['color'] ) ),
				'group'  => 'core',
				'type'   => 'color',
				'getter' => function () use ( $color ) {
					return 'var(--wp--preset--color--' . $color['slug'] . ')';
				},
			) );
		}
	}

	if ( ! empty( $settings['color']['palette']['theme'] ) ) {
		FLPageData::add_group( 'theme', array(
			'label'  => __( 'Theme', 'fl-builder' ),
			'render' => false,
		) );

		foreach ( $settings['color']['palette']['theme'] as $color ) {
			$name = isset( $color['name'] ) ? $color['name'] : ucfirst( $color['slug'] );
			FLPageData::add_site_property( 'theme_color_' . $color['slug'], array(
				'label'  => sprintf( '<span class="prefix" title="%2$s">%1$s<span> - <span title="%2$s">%2$s</span><span class="swatch" style="background-color: %3$s"></span>', esc_attr( __( 'Theme', 'fl-builder' ) ), $name, FLBuilderColor::hex_or_rgb( $color['color'] ) ),
				'group'  => 'theme',
				'type'   => 'color',
				'getter' => function () use ( $color ) {
					return 'var(--wp--preset--color--' . $color['slug'] . ')';
				},
			) );
		}
	}
}
