<?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 NokautAvailProductFieldModificator implements ProductFieldValueModificator, ProductFieldStringValue
{
    public const AVAIL_CHECK_IN_SHOP = 4;
    public const AVAIL_BACKORDER = 3;
    public const AVAIL_OVER_A_WEEK = 2;
    public const AVAIL_ON_WEEK = 1;
    public const AVAIL_ON_STOCK = 0;
    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 = self::AVAIL_CHECK_IN_SHOP;
        if ($this->product->is_on_backorder()) {
            return self::AVAIL_BACKORDER;
        }
        if ($this->product->is_in_stock()) {
            if (null !== $this->product->get_stock_quantity()) {
                return $this->product->get_stock_quantity() > 0 ? self::AVAIL_ON_STOCK : (int) $avail;
            }
            return self::AVAIL_ON_STOCK;
        }
        return (int) $avail;
    }
    public function set_product(WC_Product $product): void
    {
        $this->product = $product;
    }
}
