<?php
/**
 * Functions and definitions for the Reunionary theme.
 *
 * @package Reunionary
 */

if ( ! function_exists( 'reunionary_setup' ) ) {
	/**
	 * Set up theme defaults and declare support for WordPress features.
	 *
	 * @return void
	 */
	function reunionary_setup() {

		// Make theme available for translation.
		load_theme_textdomain( 'reunionary', get_template_directory() . '/languages' );

		// Let WordPress manage the document title.
		add_theme_support( 'title-tag' );

		// Enable post thumbnails.
		add_theme_support( 'post-thumbnails' );

		// Support for responsive embeds and HTML5 markup.
		add_theme_support( 'responsive-embeds' );
		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
			)
		);

		// Block styles + wide alignment.
		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'align-wide' );

		// Optional but recommended for block themes.
		add_theme_support( 'appearance-tools' );

		// Make editor reflect front-end styles.
		add_theme_support( 'editor-styles' );
		if ( file_exists( get_theme_file_path( 'assets/css/custom.css' ) ) ) {
			add_editor_style( 'assets/css/custom.css' );
		}
	}
}
add_action( 'after_setup_theme', 'reunionary_setup' );

/**
 * Register Reunionary block pattern categories.
 */
if ( ! function_exists( 'reunionary_register_pattern_categories' ) ) {
	function reunionary_register_pattern_categories() {
		if ( ! function_exists( 'register_block_pattern_category' ) ) {
			return;
		}

		$categories = array(
			'reunionary-core'     => array( 'label' => esc_html__( 'Reunionary — Core', 'reunionary' ) ),
			'reunionary-sections' => array( 'label' => esc_html__( 'Reunionary — Sections', 'reunionary' ) ),
			'reunionary-features' => array( 'label' => esc_html__( 'Reunionary — Features', 'reunionary' ) ),
			'reunionary-gallery'  => array( 'label' => esc_html__( 'Reunionary — Gallery', 'reunionary' ) ),
			'reunionary-cta'      => array( 'label' => esc_html__( 'Reunionary — Calls to Action', 'reunionary' ) ),
			'reunionary-pages'    => array( 'label' => esc_html__( 'Reunionary — Pages', 'reunionary' ) ),
		);

		foreach ( $categories as $slug => $args ) {
			register_block_pattern_category( $slug, $args );
		}
	}
}
add_action( 'init', 'reunionary_register_pattern_categories' );

/**
 * Register custom block styles (recommended for block themes).
 */
function reunionary_register_block_styles() {
	if ( ! function_exists( 'register_block_style' ) ) {
		return;
	}

	register_block_style(
		'core/button',
		array(
			'name'  => 'reunionary-pill-outline',
			'label' => esc_html__( 'Reunionary Pill Outline', 'reunionary' ),
		)
	);

	register_block_style(
		'core/group',
		array(
			'name'  => 'reunionary-soft-card',
			'label' => esc_html__( 'Reunionary Soft Card', 'reunionary' ),
		)
	);

	register_block_style(
		'core/navigation',
		array(
			'name'  => 'reunionary-nav-pill',
			'label' => esc_html__( 'Reunionary Nav Pill', 'reunionary' ),
		)
	);
}
add_action( 'init', 'reunionary_register_block_styles' );

function reunionary_enqueue_styles() {
	$css_rel  = 'assets/css/custom.css';
	$css_path = get_theme_file_path( $css_rel );

	if ( ! file_exists( $css_path ) ) {
		return;
	}

	// Bust cache when the file changes.
	$ver = filemtime( $css_path );

	wp_enqueue_style(
		'reunionary-custom',
		get_theme_file_uri( $css_rel ),
		array(),
		$ver
	);
}
add_action( 'wp_enqueue_scripts', 'reunionary_enqueue_styles' );