<?php

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

use WC_Product;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldFloatValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
class WeightProductFieldValue implements ProductFieldValue, ProductFieldFloatValue
{
    public const KEY = 'weight';
    private ?float $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__('Weight', 'dropshipping-export-free');
    }
    public function has_float_value(): bool
    {
        return \is_float($this->get_field_value());
    }
    public function get_float_value(): ?float
    {
        return $this->get_field_value();
    }
    private function get_field_value(): ?float
    {
        if ($this->value !== null) {
            return $this->value;
        }
        $weight = $this->product->get_weight();
        $this->value = is_numeric($weight) ? (float) $weight : null;
        return $this->value;
    }
}
