<?php
/** Reusable B2B 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_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_fallback_menu( $args = array() ) {
	$items = array(
		'#intro'        => __( '首页', 'fragrance-trade' ),
		'#services'     => __( '服务', 'fragrance-trade' ),
		'#capabilities' => __( '产品', 'fragrance-trade' ),
		'#factory'      => __( '工厂', 'fragrance-trade' ),
		'#quality'      => __( '品质', 'fragrance-trade' ),
		'#process'      => __( '流程', 'fragrance-trade' ),
		'#rfq'          => __( '获取报价', 'fragrance-trade' ),
	);
	$class = isset( $args['menu_class'] ) ? $args['menu_class'] : 'navbar-nav';
	echo '<ul class="' . esc_attr( $class ) . '">';
	foreach ( $items as $anchor => $label ) {
		$url = is_front_page() ? $anchor : home_url( '/' ) . $anchor;
		echo '<li class="nav-item"><a class="nav-link p-0" href="' . esc_url( $url ) . '">' . esc_html( $label ) . '</a></li>';
	}
	echo '</ul>';
}

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 的 OEM/ODM 项目。', '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_get_notice() {
	if ( empty( $_GET['fragrance_notice'] ) ) {
		return '';
	}
	return sanitize_key( wp_unslash( $_GET['fragrance_notice'] ) );
}

function fragrance_trade_notice_message( $code ) {
	$messages = array(
		'success'            => __( '感谢您的提交，项目需求已发送。', 'fragrance-trade' ),
		'invalid'            => __( '请检查必填项后重新提交。', 'fragrance-trade' ),
		'rate_limited'       => __( '请稍候片刻再重新提交。', 'fragrance-trade' ),
		'mail_failed'        => __( '发送失败，请通过邮件或 WhatsApp 联系我们。', 'fragrance-trade' ),
	);
	return isset( $messages[ $code ] ) ? $messages[ $code ] : '';
}
