<?php

if (!function_exists('product_badge_presets')) {
    function product_badge_presets(): array
    {
        return [
            'products.badge_preset_new' => [
                'text' => __('products.badge_preset_new', 'New Arrival'),
                'class' => 'badge-new',
                'emoji' => '✨',
                'fallback' => 'New Arrival',
            ],
            'products.badge_preset_hot' => [
                'text' => __('products.badge_preset_hot', 'Best Seller'),
                'class' => 'badge-hot',
                'emoji' => '🔥',
                'fallback' => 'Best Seller',
            ],
            'products.badge_preset_sale' => [
                'text' => __('products.badge_preset_sale', 'Special Offer'),
                'class' => 'badge-sale',
                'emoji' => '🎁',
                'fallback' => 'Special Offer',
            ],
            'products.badge_preset_limited' => [
                'text' => __('products.badge_preset_limited', 'Limited Stock'),
                'class' => 'badge-limited',
                'emoji' => '⏳',
                'fallback' => 'Limited Stock',
            ],
            'products.badge_preset_exclusive' => [
                'text' => __('products.badge_preset_exclusive', 'Exclusive'),
                'class' => 'badge-exclusive',
                'emoji' => '💎',
                'fallback' => 'Exclusive',
            ],
            'products.badge_preset_shipping' => [
                'text' => __('products.badge_preset_shipping', 'Free Shipping'),
                'class' => 'badge-shipping',
                'emoji' => '🚚',
                'fallback' => 'Free Shipping',
            ],
            'products.badge_preset_discount' => [
                'text' => __('products.badge_preset_discount', 'Discount'),
                'class' => 'badge-discount',
                'emoji' => '🏷️',
                'fallback' => 'Discount',
            ],
        ];
    }
}

if (!function_exists('product_badge_legacy_map')) {
    function product_badge_legacy_map(): array
    {
        return [
            'وصل حديثًا' => 'products.badge_preset_new',
            'الأكثر مبيعًا' => 'products.badge_preset_hot',
            'عرض خاص' => 'products.badge_preset_sale',
            'كمية محدودة' => 'products.badge_preset_limited',
            'حصري' => 'products.badge_preset_exclusive',
            'توصيل مجاني' => 'products.badge_preset_shipping',
            'تخفيض' => 'products.badge_preset_discount',
            'New Arrival' => 'products.badge_preset_new',
            'Best Seller' => 'products.badge_preset_hot',
            'Special Offer' => 'products.badge_preset_sale',
            'Limited Stock' => 'products.badge_preset_limited',
            'Exclusive' => 'products.badge_preset_exclusive',
            'Free Shipping' => 'products.badge_preset_shipping',
            'Discount' => 'products.badge_preset_discount',
            'new' => 'products.badge_preset_new',
            'hot' => 'products.badge_preset_hot',
            'sale' => 'products.badge_preset_sale',
            'limited' => 'products.badge_preset_limited',
            'exclusive' => 'products.badge_preset_exclusive',
            'free_shipping' => 'products.badge_preset_shipping',
            'discount' => 'products.badge_preset_discount',
        ];
    }
}

if (!function_exists('product_badge_resolve')) {
    function product_badge_resolve(?string $badgeText): array
    {
        $badgeText = trim((string)$badgeText);
        $presets = product_badge_presets();
        $legacyMap = product_badge_legacy_map();
        $badgeKey = isset($presets[$badgeText]) ? $badgeText : ($legacyMap[$badgeText] ?? '');
        $badgeMeta = $badgeKey !== '' ? ($presets[$badgeKey] ?? null) : null;
        $badgeLabel = $badgeMeta ? __($badgeKey, $badgeMeta['fallback']) : $badgeText;

        return [
            'text' => $badgeText,
            'key' => $badgeKey,
            'meta' => $badgeMeta,
            'label' => $badgeLabel,
            'presets' => $presets,
            'legacy_map' => $legacyMap,
        ];
    }
}
