<?php
// Copyright 1999-2026. WebPros International GmbH. All rights reserved.

namespace Webpros\WptkWpPlugin\WpToolkit\Waf\Extensions;

class ActiveRulesVulnerabilityMap
{
    /**
     * @var array<int, string>
     */
    private $convertedMap;

    /**
     * @param $activeRules array<string, {
     *                     id: int,
     *                     rules: array,
     *                     }
     */
    public function __construct(array $activeRules = [])
    {
        $this->convertedMap = $this->convertMap($activeRules);
    }

    /**
     * @param int $ruleId
     *
     * @return string|null
     */
    public function getVulnerabilityId($ruleId)
    {
        if (isset($this->convertedMap[$ruleId])) {
            return $this->convertedMap[$ruleId];
        }

        return null;
    }

    /**
     * @param $activeRules array<string, {
     *                     id: int,
     *                     rules: array,
     *                     }
     *
     * @return array<int, string>
     */
    private function convertMap(array $activeRules)
    {
        $map = [];

        foreach ($activeRules as $vulnerabilityId => $rule) {
            $map[$rule['id']] = $vulnerabilityId;
        }

        return $map;
    }
}
