<?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;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValueModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\PriceModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldCustomFloatValueModificator;
class SalePriceProductFieldValue implements ProductFieldValue, ProductFieldFloatValue, PriceModificator
{
    public const KEY = 'sale_price';
    private ?float $value = null;
    private WC_Product $product;
    private ?ProductFieldValueModificator $modificator = null;
    public function __construct(WC_Product $product)
    {
        $this->product = $product;
    }
    public function set_price_modificator(ProductFieldValueModificator $modificator)
    {
        $this->modificator = $modificator;
        $this->modificator->set_product($this->product);
    }
    public static function get_key(): string
    {
        return self::KEY;
    }
    public static function get_name(): string
    {
        return esc_attr__('Sale price', '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;
        }
        $price = $this->product->get_sale_price();
        $price = \is_numeric($price) ? (float) $price : null;
        if (\is_float($price) && $this->modificator instanceof ProductFieldFloatValue && $this->modificator instanceof ProductFieldCustomFloatValueModificator) {
            $this->modificator->set_float_value($price);
            $price = $this->modificator->get_float_value();
        }
        if (\is_float($price)) {
            $this->value = $price;
        }
        return $this->value;
    }
}
