<?php

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

use WC_Product;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
class UrlProductFieldValue implements ProductFieldValue, ProductFieldStringValue
{
    public const KEY = 'product_url';
    public const LANG_FILTER = 'wpdesk_dropshipping_csv_encode_string';
    private ?string $value = null;
    private WC_Product $product;
    public function __construct(WC_Product $product)
    {
        $this->product = $product;
    }
    public static function get_key(): string
    {
        return self::KEY;
    }
    public static function get_name(): string
    {
        return esc_attr__('Product URL', 'dropshipping-export-free');
    }
    public function has_string_value(): bool
    {
        return \is_string($this->get_field_value());
    }
    public function get_string_value(): ?string
    {
        return $this->get_field_value();
    }
    private function get_field_value(): ?string
    {
        if ($this->value !== null) {
            return $this->value;
        }
        global $sitepress;
        $permalink = $this->product->get_permalink();
        if ($sitepress instanceof \DropshippingExportFreeVendor\SitePress) {
            $default_lang = apply_filters(self::LANG_FILTER, get_locale(), $permalink);
            $permalink = apply_filters('wpml_permalink', $permalink, $default_lang);
        }
        $this->value = htmlspecialchars($permalink);
        return $this->value;
    }
}
