<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Loader\Plugin;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Hookable\Hookable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportManagerViewAction;
class PluginLinksLoaderAction implements Hookable
{
    public const SUPPORT_PAGE = 'https://wpdesk.link/export-support';
    public const SUPPORT_PAGE_PL = 'https://wpdesk.link/export-support-pl';
    public const DOCS_PAGE = 'https://wpdesk.link/export-docs';
    public const DOCS_PAGE_PL = 'https://wpdesk.link/export-docs-pl';
    private Config $config;
    private PluginHelper $plugin_helper;
    public function __construct(Config $config, PluginHelper $helper)
    {
        $this->config = $config;
        $this->plugin_helper = $helper;
    }
    public function hooks()
    {
        add_filter('plugin_action_links_' . \plugin_basename($this->config->get_param('plugin.file')->get()), [$this, 'links_filter']);
    }
    public function links_filter(array $links): array
    {
        $docs_url = self::DOCS_PAGE;
        $support_url = self::SUPPORT_PAGE;
        if (self::is_pl_lang()) {
            $docs_url = self::DOCS_PAGE_PL;
            $support_url = self::SUPPORT_PAGE_PL;
        }
        $plugin_links = ['<a target="_blank" href="' . esc_url($this->plugin_helper->generate_url_by_view(ExportManagerViewAction::class)) . '">' . \esc_attr__('Settings', 'dropshipping-export-free') . '</a>', '<a target="_blank" href="' . esc_url($docs_url) . '">' . \esc_attr__('Docs', 'dropshipping-export-free') . '</a>'];
        $urls_to_replace = ['https://www.wpdesk.pl/support/', 'https://www.wpdesk.net/support'];
        $links = array_map(function ($value) use ($urls_to_replace, $support_url) {
            return \str_replace($urls_to_replace, $support_url, $value);
        }, $links);
        return array_merge($plugin_links, $links);
    }
    public static function is_pl_lang(): bool
    {
        return \get_locale() === 'pl_PL';
    }
}
