<?php
/** Reusable store theme helpers. @package Fragrance_Trade */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function fragrance_trade_get_default( $key, $fallback = '' ) {
	$defaults = fragrance_trade_defaults();
	return array_key_exists( $key, $defaults ) ? $defaults[ $key ] : $fallback;
}

function fragrance_trade_get_mod( $key, $fallback = null ) {
	$default = null === $fallback ? fragrance_trade_get_default( $key ) : $fallback;
	return get_theme_mod( $key, $default );
}

function fragrance_trade_resolve_url( $url ) {
	$url = trim( (string) $url );
	if ( str_starts_with( $url, '/' ) && ! str_starts_with( $url, '//' ) ) {
		return home_url( $url );
	}
	return $url;
}

function fragrance_trade_asset_uri( $relative_path ) {
	return trailingslashit( get_template_directory_uri() ) . 'assets/' . ltrim( $relative_path, '/' );
}

function fragrance_trade_image_uri( $setting, $fallback_filename ) {
	$image = fragrance_trade_get_mod( $setting, '' );
	return $image ? $image : fragrance_trade_asset_uri( 'images/' . $fallback_filename );
}

function fragrance_trade_show_section( $setting ) {
	return (bool) fragrance_trade_get_mod( $setting, true );
}

function fragrance_trade_whatsapp_number() {
	return preg_replace( '/[^0-9]/', '', (string) fragrance_trade_get_mod( 'whatsapp', '' ) );
}

function fragrance_trade_inquiry_email() {
	$email = sanitize_email( fragrance_trade_get_mod( 'inquiry_email', '' ) );
	return is_email( $email ) ? $email : sanitize_email( get_option( 'admin_email' ) );
}

function fragrance_trade_get_inquiry_url( $project = '' ) {
	$message = $project
		? sprintf( __( '您好，我想咨询 %s 的贴膜服务。', 'fragrance-trade' ), $project )
		: __( '您好，我想咨询汽车贴膜服务。', 'fragrance-trade' );
	$number = fragrance_trade_whatsapp_number();
	if ( $number ) {
		return add_query_arg( 'text', $message, 'https://wa.me/' . $number );
	}
	return 'mailto:' . antispambot( fragrance_trade_inquiry_email() ) . '?subject=' . rawurlencode( __( '汽车贴膜服务咨询', 'fragrance-trade' ) ) . '&body=' . rawurlencode( $message );
}

function fragrance_trade_primary_menu_fallback() {
	$links = array(
		array( 'label' => __( '首页', 'fragrance-trade' ), 'url' => home_url( '/' ) ),
		array( 'label' => __( '服务项目', 'fragrance-trade' ), 'url' => get_post_type_archive_link( 'service' ) ?: home_url( '/services/' ) ),
		array( 'label' => __( '案例作品', 'fragrance-trade' ), 'url' => get_post_type_archive_link( 'case' ) ?: home_url( '/cases/' ) ),
		array( 'label' => __( '关于门店', 'fragrance-trade' ), 'url' => home_url( '/about/' ) ),
		array( 'label' => __( '常见问题', 'fragrance-trade' ), 'url' => home_url( '/faq/' ) ),
		array( 'label' => __( '联系我们', 'fragrance-trade' ), 'url' => home_url( '/contact/' ) ),
	);

	echo '<ul class="navbar-nav justify-content-end flex-grow-1 gap-xl-4 pe-xl-3">';
	foreach ( $links as $link ) {
		printf(
			'<li class="nav-item"><a class="nav-link" href="%s">%s</a></li>',
			esc_url( $link['url'] ),
			esc_html( $link['label'] )
		);
	}
	echo '</ul>';
}

add_action(
	'admin_notices',
	function () {
		if ( ! current_user_can( 'edit_theme_options' ) ) {
			return;
		}

		$locations = get_nav_menu_locations();
		$menu_id   = isset( $locations['primary'] ) ? $locations['primary'] : 0;
		if ( ! $menu_id ) {
			printf(
				'<div class="notice notice-warning is-dismissible"><p><strong>%s</strong> %s <a href="%s">%s</a></p></div>',
				esc_html__( '尚未分配主导航菜单', 'fragrance-trade' ),
				esc_html__( '前台暂时显示主题默认导航。请创建正式菜单并分配到“主导航”。', 'fragrance-trade' ),
				esc_url( admin_url( 'nav-menus.php' ) ),
				esc_html__( '配置菜单', 'fragrance-trade' )
			);
			return;
		}
		$items = wp_get_nav_menu_items( $menu_id );
		$count = $items ? count( $items ) : 0;
		if ( $count < 2 ) {
			printf(
				'<div class="notice notice-warning is-dismissible"><p><strong>%s</strong> %s <a href="%s">%s</a></p></div>',
				esc_html__( '主导航菜单项过少', 'fragrance-trade' ),
				esc_html__( '手机端和桌面端导航目前只显示一个菜单项。请前往后台添加：首页、服务项目、案例作品、关于门店、常见问题、联系我们。', 'fragrance-trade' ),
				esc_url( admin_url( 'nav-menus.php' ) ),
				esc_html__( '编辑菜单', 'fragrance-trade' )
			);
		}
	}
);
