<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldArrayValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldImgLocationValue;
use WC_Product;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldImgNamePathValue;
class ImageProductFieldValue implements ProductFieldValue, ProductFieldArrayValue, ProductFieldStringValue, ProductFieldImgLocationValue, ProductFieldImgNamePathValue
{
    public const KEY = 'images';
    private ?array $value = null;
    private ?array $file_locations = null;
    private WC_Product $product;
    private bool $only_images_name = \false;
    public function __construct(WC_Product $product)
    {
        $this->product = $product;
    }
    public function has_string_value(): bool
    {
        return $this->get_string_value() !== null;
    }
    public function get_string_value(): ?string
    {
        $url = wp_get_attachment_url($this->product->get_image_id());
        if (is_string($url)) {
            return $url;
        }
        return null;
    }
    public function set_only_image_name(bool $only_image_name): void
    {
        $this->only_images_name = $only_image_name;
    }
    public static function get_key(): string
    {
        return self::KEY;
    }
    public static function get_name(): string
    {
        return esc_attr__('Images', '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
    {
        $images = [];
        if ($this->value !== null) {
            return $this->value;
        }
        $image_id = $this->product->get_image_id();
        if ($image_id) {
            $images[] = wp_get_attachment_url($image_id);
        }
        foreach ($this->product->get_gallery_image_ids() as $attachment_id) {
            $images[] = wp_get_attachment_url($attachment_id);
        }
        if (!empty($images)) {
            if ($this->only_images_name) {
                $images = \array_map(function ($value) {
                    return basename($value);
                }, $images);
            }
            $this->value = $images;
        }
        return $this->value;
    }
    public function get_file_locations(): ?array
    {
        $images = [];
        if ($this->file_locations !== null) {
            return $this->file_locations;
        }
        $image_id = $this->product->get_image_id();
        if ($image_id) {
            $images[] = get_attached_file($image_id);
        }
        foreach ($this->product->get_gallery_image_ids() as $attachment_id) {
            $file = get_attached_file($attachment_id);
            if (\is_string($file) && !empty($file)) {
                $images[] = get_attached_file($attachment_id);
            }
        }
        if (!empty($images)) {
            $this->file_locations = $images;
        }
        return $this->file_locations;
    }
}
