<?php

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

use WC_Product;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValueModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
class CeneoAvailProductFieldModificator implements ProductFieldValueModificator, ProductFieldStringValue
{
    public const AVAIL_PRESALE = 110;
    public const AVAIL_BACKORDER = 90;
    public const AVAIL_ON_14_DAYS = 14;
    public const AVAIL_ON_7_DAYS = 7;
    public const AVAIL_ON_3_DAYS = 3;
    public const AVAIL_ON_STOCK = 1;
    private ?WC_Product $product = null;
    public function has_string_value(): bool
    {
        return \is_string($this->get_string_value());
    }
    public function get_string_value(): ?string
    {
        if (!\is_object($this->product)) {
            return null;
        }
        return (string) $this->get_avail_number();
    }
    private function get_avail_number(): int
    {
        $avail_status = self::AVAIL_ON_7_DAYS;
        $product = $this->product;
        if ($product->is_on_backorder()) {
            return self::AVAIL_BACKORDER;
        }
        if (!empty($avail_status)) {
            if ($product->is_in_stock()) {
                if (null !== $product->get_stock_quantity()) {
                    return $product->get_stock_quantity() > 0 ? self::AVAIL_ON_STOCK : (int) $avail_status;
                }
                return self::AVAIL_ON_STOCK;
            }
            return (int) $avail_status;
        }
        return self::AVAIL_ON_STOCK;
    }
    public function set_product(WC_Product $product): void
    {
        $this->product = $product;
    }
}
