<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\OptionalProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldArrayValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldDefaultValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldFloatValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldIntValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\NoProductFieldValue;
abstract class CustomProductField implements ProductFieldDefaultValue, OptionalProductField
{
    public static function is_optional(): bool
    {
        return \false;
    }
    public static function get_default_class_value(): string
    {
        return NoProductFieldValue::class;
    }
    public static function is_allowed_to_change(): bool
    {
        return \false;
    }
    protected function get_int_value(ProductFieldValue $product_value): ?int
    {
        if ($product_value instanceof ProductFieldIntValue && $product_value->has_int_value()) {
            return $product_value->get_int_value();
        }
        return null;
    }
    protected function get_string_value(ProductFieldValue $product_value): ?string
    {
        if ($product_value instanceof ProductFieldStringValue && $product_value->has_string_value()) {
            return $product_value->get_string_value();
        }
        return null;
    }
    protected function get_float_value(ProductFieldValue $product_value): ?float
    {
        if ($product_value instanceof ProductFieldFloatValue && $product_value->has_float_value()) {
            return $product_value->get_float_value();
        }
        return null;
    }
    protected function get_array_value(ProductFieldValue $product_value): ?array
    {
        if ($product_value instanceof ProductFieldArrayValue && $product_value->has_array_value()) {
            return $product_value->get_array_value();
        }
        return null;
    }
    protected function get_values_as_array(ProductFieldValue $product_value): ?array
    {
        $val = $this->get_array_value($product_value);
        if (is_array($val)) {
            return $val;
        }
        $val = $this->get_string_value($product_value);
        if (\is_string($val)) {
            return [$val];
        }
        return null;
    }
    protected function get_values_as_string(ProductFieldValue $product_value): ?string
    {
        $val = $this->get_string_value($product_value);
        if (is_string($val)) {
            return $val;
        }
        $val = $this->get_int_value($product_value);
        if (\is_int($val)) {
            return (string) $val;
        }
        $val = $this->get_float_value($product_value);
        if (\is_float($val)) {
            return (string) $val;
        }
        return null;
    }
    protected function get_values_as_int(ProductFieldValue $product_value): ?int
    {
        $val = $this->get_int_value($product_value);
        if (\is_int($val)) {
            return $val;
        }
        $val = $this->get_float_value($product_value);
        if (\is_float($val)) {
            return (int) $val;
        }
        $val = $this->get_string_value($product_value);
        if (is_numeric($val)) {
            return (int) $val;
        }
        return null;
    }
    protected function get_values_as_float(ProductFieldValue $product_value): ?float
    {
        $val = $this->get_float_value($product_value);
        if (\is_float($val)) {
            return $val;
        }
        $val = $this->get_int_value($product_value);
        if (\is_int($val)) {
            return (float) $val;
        }
        $val = $this->get_string_value($product_value);
        if (is_numeric($val)) {
            return (float) $val;
        }
        return null;
    }
    protected function change_node_name(string $node_name): string
    {
        return $this->strip_invalid_string(str_replace(' ', '_', trim($node_name)));
    }
    protected function strip_invalid_string(string $value): string
    {
        $ret = '';
        $current = null;
        if (empty($value)) {
            return $ret;
        }
        $length = strlen($value);
        for ($i = 0; $i < $length; $i++) {
            $current = ord($value[$i]);
            if ($current == 0x9 || $current == 0xa || $current == 0xd || $current >= 0x20 && $current <= 0xd7ff || $current >= 0xe000 && $current <= 0xfffd || $current >= 0x10000 && $current <= 0x10ffff) {
                $ret .= chr($current);
            } else {
                $ret .= ' ';
            }
        }
        return $ret;
    }
}
