<?php

declare (strict_types=1);
namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Response;

use WC_Coupon;
use WC_Order_Item_Coupon;
/**
 * Formats coupon titles for JSON API responses.
 *
 * Returns plain text (never HTML) suitable for JSON output.
 */
final class CouponTitleFormatter
{
    public function from_coupon(WC_Coupon $coupon): string
    {
        $desc = $coupon->get_description();
        if ($desc) {
            return $desc;
        }
        return $this->format_amount((float) $coupon->get_amount(), $coupon->get_discount_type());
    }
    public function from_order_item(WC_Order_Item_Coupon $item): string
    {
        $code = $item->get_code();
        $desc = $item->get_name();
        if ($desc && $desc !== $code) {
            return $desc;
        }
        $coupon = new WC_Coupon($code);
        return $this->format_amount((float) $coupon->get_amount(), $coupon->get_discount_type());
    }
    private function format_amount(float $amount, string $type): string
    {
        if ('percent' === $type) {
            return $amount . '% Off';
        }
        return \wp_strip_all_tags(\html_entity_decode((string) \wc_price($amount))) . ' Off';
    }
}
