<?php

declare(strict_types=1);

namespace Autogedal\Reduceri\Plugin\ShopbySeo;

use Amasty\ShopbySeo\Controller\Router as SeoRouter;
use Magento\Framework\App\RequestInterface;

class AllowReduceriSeoUrls
{
    public function __construct(
        private readonly RequestInterface $request
    ) {}

    /**
     * Allow ShopbySeo Router to parse filter slugs from /reduceri/{url-key}/{slug} URLs.
     *
     * skipIdentifier() returns true (skip) when the base path has no entry in url_rewrite.
     * Our /reduceri/* paths are handled by a custom router with no rewrites, so without
     * this plugin the SEO Router would skip them and filter links would fall back to
     * query-param format.
     */
    public function aroundSkipIdentifier(
        SeoRouter $subject,
        \Closure $proceed,
        string $identifier
    ): bool {
        if (preg_match('#^/?reduceri/[^/]+$#', $identifier)) {
            return false;
        }

        return $proceed($identifier);
    }
}
