<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValueModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\DropshippingDataProvider;
use WC_Product;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportMapperFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ModificatorFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldFloatValue;
class StockProductFieldModificator implements ProductFieldValueModificator, ProductFieldFloatValue
{
    private DropshippingDataProvider $mapper_data_provider;
    private ModificatorFactory $modificator_factory;
    private WC_Product $product;
    public function __construct(ModificatorFactory $modificator_factory, DropshippingDataProvider $data_provider)
    {
        $this->modificator_factory = $modificator_factory;
        $this->mapper_data_provider = $data_provider;
    }
    public function set_product(WC_Product $product): void
    {
        $this->product = $product;
    }
    public function has_float_value(): bool
    {
        return \is_float($this->get_float_value());
    }
    public function get_float_value(): ?float
    {
        if ($this->product->get_stock_quantity() === null) {
            return null;
        }
        $raw_conditions = $this->mapper_data_provider->get(ExportMapperFormFields::PRODUCT_ADVANCED_STOCK_MOD);
        $modificator_collection = $this->modificator_factory->create_modificator_collection($raw_conditions);
        $stock = $modificator_collection->get_value($this->product, $this->product->get_stock_quantity());
        return \is_numeric($stock) ? (float) $stock : null;
    }
}
