<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldArrayValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValueModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\CategoryModificator;
use WC_Product;
class CategoryProductFieldValue implements ProductFieldValue, ProductFieldStringValue, ProductFieldArrayValue, CategoryModificator
{
    public const KEY = 'category';
    private ?string $string_value = null;
    private ?array $value = null;
    private WC_Product $product;
    private ProductFieldValueModificator $modificator;
    public function __construct(WC_Product $product)
    {
        $this->product = $product;
    }
    public function has_string_value(): bool
    {
        return \is_string($this->get_string_value());
    }
    public function get_string_value(): ?string
    {
        if ($this->string_value !== null) {
            return $this->string_value;
        }
        if (isset($this->modificator) && $this->modificator instanceof ProductFieldStringValue && $this->modificator->has_string_value()) {
            $this->string_value = $this->modificator->get_string_value();
        } else {
            $categories = $this->get_default_category_list();
            if (is_array($categories) && !empty($categories)) {
                $this->string_value = reset($categories);
            }
        }
        return $this->string_value;
    }
    public function set_category_modificator(ProductFieldValueModificator $modificator): void
    {
        $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__('Category', 'dropshipping-export-free');
    }
    public function has_array_value(): bool
    {
        return \is_array($this->get_field_value());
    }
    public function get_array_value(): ?array
    {
        return $this->get_field_value();
    }
    private function get_field_value(): ?array
    {
        if ($this->value !== null) {
            return $this->value;
        }
        if (isset($this->modificator) && $this->modificator instanceof ProductFieldArrayValue) {
            $this->value = $this->modificator->get_array_value();
        } else {
            $this->value = $this->get_default_category_list();
        }
        return $this->value;
    }
    private function get_default_category_list(): array
    {
        $product = $this->product;
        if ($this->product->get_type() === 'variation') {
            $product = wc_get_product($this->product->get_parent_id());
        }
        $categories_ids = $product->get_category_ids();
        $tmp_categories = [];
        foreach ($categories_ids as $category_id) {
            $term = get_term_by('id', $category_id, 'product_cat');
            if (\is_object($term)) {
                $tmp_categories[$term->term_id] = $term->name;
            }
        }
        return $tmp_categories;
    }
}
