<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use WC_Product;
class BrandProductFieldValue implements ProductFieldValue, ProductFieldStringValue
{
    public const KEY = 'brand';
    private ?string $value = null;
    private WC_Product $product;
    public function __construct(WC_Product $product)
    {
        $this->product = $product;
    }
    public static function get_key(): string
    {
        return self::KEY;
    }
    public static function get_name(): string
    {
        return esc_attr__('Brand', 'dropshipping-export-free');
    }
    public function has_string_value(): bool
    {
        return \is_string($this->get_string_value());
    }
    public function get_string_value(): ?string
    {
        if ($this->value !== null) {
            return $this->value;
        }
        $brand_names = wp_get_post_terms($this->product->get_id(), 'product_brand', ['fields' => 'names']);
        if (\is_array($brand_names) && !empty($brand_names)) {
            $this->value = (string) reset($brand_names);
        }
        return $this->value;
    }
}
