<?php
/**
 * Enqueue fonts, CSS, and scripts.
 *
 * @package Foliora
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Front-end assets.
 */
function foliora_enqueue_assets() {
	wp_enqueue_style(
		'foliora-fonts',
		'https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,500;0,600;1,400&family=DM+Sans:wght@400;500;600&display=swap',
		array(),
		null
	);

	$css = array(
		'tokens'      => 'tokens.css',
		'base'        => 'base.css',
		'header'      => 'header.css',
		'home'        => 'home.css',
		'shop'        => 'shop.css',
		'product'     => 'product.css',
		'cart-drawer' => 'cart-drawer.css',
	);

	$prev = 'foliora-fonts';
	foreach ( $css as $handle => $file ) {
		$key = 'foliora-' . $handle;
		wp_enqueue_style(
			$key,
			get_theme_file_uri( 'assets/css/' . $file ),
			array( $prev ),
			FOLIORA_VERSION
		);
		$prev = $key;
	}

	wp_enqueue_script(
		'foliora-main',
		get_theme_file_uri( 'assets/js/main.js' ),
		array(),
		FOLIORA_VERSION,
		true
	);

	wp_enqueue_script(
		'foliora-cart',
		get_theme_file_uri( 'assets/js/cart-drawer.js' ),
		array(),
		FOLIORA_VERSION,
		true
	);

	if ( function_exists( 'is_product' ) && is_product() ) {
		wp_enqueue_script(
			'foliora-product',
			get_theme_file_uri( 'assets/js/product.js' ),
			array(),
			FOLIORA_VERSION,
			true
		);
	}

	$ajax_url    = admin_url( 'admin-ajax.php' );
	$wc_ajax_url = $ajax_url;
	if ( class_exists( 'WC_AJAX' ) ) {
		$wc_ajax_url = WC_AJAX::get_endpoint( '%%endpoint%%' );
	}

	wp_localize_script(
		'foliora-cart',
		'folioraCart',
		array(
			'ajaxUrl'         => $ajax_url,
			'nonce'           => wp_create_nonce( 'foliora_cart' ),
			'wcAjaxUrl'       => $wc_ajax_url,
			'cartUrl'         => function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : home_url( '/cart/' ),
			'checkoutUrl'     => function_exists( 'wc_get_checkout_url' ) ? wc_get_checkout_url() : home_url( '/checkout/' ),
			'freeShippingMin' => 50,
			'currencySymbol'  => '$',
			'i18n'            => array(
				'added' => 'Added to bag',
			),
		)
	);
}
add_action( 'wp_enqueue_scripts', 'foliora_enqueue_assets' );

/**
 * Keep the header logo from being stretched by WooCommerce image rules.
 */
function foliora_logo_size_override() {
	$css = 'body .site-header a.site-brand img.site-logo{width:7.2rem!important;height:7.2rem!important;max-width:7.2rem!important;max-height:7.2rem!important;object-fit:contain}body .site-footer img.footer-logo{width:6.5rem!important;height:6.5rem!important;max-width:6.5rem!important;max-height:6.5rem!important;object-fit:contain}@media(max-width:860px){body .site-header a.site-brand img.site-logo{width:5.4rem!important;height:5.4rem!important;max-width:5.4rem!important;max-height:5.4rem!important}}';
	if ( wp_style_is( 'woocommerce-general', 'enqueued' ) ) {
		wp_add_inline_style( 'woocommerce-general', $css );
	}
	wp_add_inline_style( 'foliora-header', $css );
}
add_action( 'wp_enqueue_scripts', 'foliora_logo_size_override', 30 );

/**
 * Brand favicon.
 */
function foliora_favicon() {
	$href = get_theme_file_uri( 'assets/brand/foliora-logo.png' );
	echo '<link rel="icon" href="' . esc_url( $href ) . '" type="image/png">' . "\n";
}
add_action( 'wp_head', 'foliora_favicon', 2 );
