<?php
declare(strict_types=1);

namespace Ekesto\CacheInvalidator\Integrations\Forms;

/**
 * Normalizes storage-only escaping artifacts in scanned form reference text.
 */
final class FormsScanTextNormalizer
{
    /**
     * Normalize one scanned text payload.
     *
     * Representation cleanup only:
     * - safe unescaping of common slash-escaped quotes and slashes
     * - safe normalization of common JSON unicode quote/slash escapes
     *
     * This must remain a single-pass, idempotent, deterministic transform and
     * must not perform semantic decoding or speculative multi-pass unescaping.
     *
     * @param string $text Raw scanned text.
     * @return string
     */
    public static function normalize(string $text): string
    {
        if ($text === '') {
            return '';
        }

        return strtr($text, [
            '\\u0022' => '"',
            '\\u0027' => "'",
            '\\u002f' => '/',
            '\\u002F' => '/',
            '\\"' => '"',
            "\\'" => "'",
            '\\/' => '/',
        ]);
    }
}
