-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | OSV export names and native version grammars for each advisory compile pass.
module Ecluse.Core.Osv.Ecosystem (
    OsvEcosystem (..),
    osvEcosystemFor,
    osvEcosystemNamed,
) where

import Ecluse.Core.Ecosystem (Ecosystem (Cargo, Npm, PyPI, RubyGems), ecosystemName, parseEcosystem)

-- | The two spellings, the version grammar, and the fan-out bound one compile pass needs.
data OsvEcosystem = OsvEcosystem
    { OsvEcosystem -> Text
osvExportDirectory :: Text
    -- ^ The export directory and affected-package ecosystem spelling at osv.dev.
    , OsvEcosystem -> Text
osvWireName :: Text
    -- ^ The internal name on the compiled artifact and its checked metadata row.
    , OsvEcosystem -> Maybe Ecosystem
osvEcosystemTag :: Maybe Ecosystem
    -- ^ The native version grammar, absent for an unknown export.
    , OsvEcosystem -> Int
osvMaxAdvisoryFanOut :: Int
    -- ^ The per-advisory fan-out alarm threshold. It never discards evidence.
    }
    deriving stock (OsvEcosystem -> OsvEcosystem -> Bool
(OsvEcosystem -> OsvEcosystem -> Bool)
-> (OsvEcosystem -> OsvEcosystem -> Bool) -> Eq OsvEcosystem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OsvEcosystem -> OsvEcosystem -> Bool
== :: OsvEcosystem -> OsvEcosystem -> Bool
$c/= :: OsvEcosystem -> OsvEcosystem -> Bool
/= :: OsvEcosystem -> OsvEcosystem -> Bool
Eq, Int -> OsvEcosystem -> ShowS
[OsvEcosystem] -> ShowS
OsvEcosystem -> String
(Int -> OsvEcosystem -> ShowS)
-> (OsvEcosystem -> String)
-> ([OsvEcosystem] -> ShowS)
-> Show OsvEcosystem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OsvEcosystem -> ShowS
showsPrec :: Int -> OsvEcosystem -> ShowS
$cshow :: OsvEcosystem -> String
show :: OsvEcosystem -> String
$cshowList :: [OsvEcosystem] -> ShowS
showList :: [OsvEcosystem] -> ShowS
Show)

-- One advisory of the npm export names a few hundred ranges, and a feed no one has measured
-- borrows this bound.
npmAdvisoryFanOut :: Int
npmAdvisoryFanOut :: Int
npmAdvisoryFanOut = Int
256

-- The largest advisory of today's PyPI export names 2459 ranges, so no ordinary one trips this.
pypiAdvisoryFanOut :: Int
pypiAdvisoryFanOut :: Int
pypiAdvisoryFanOut = Int
4096

-- | Pair the OSV export directory with the internal ecosystem name and version grammar.
osvEcosystemFor :: Ecosystem -> OsvEcosystem
osvEcosystemFor :: Ecosystem -> OsvEcosystem
osvEcosystemFor Ecosystem
eco =
    OsvEcosystem
        { osvExportDirectory :: Text
osvExportDirectory = Text
exportDirectory
        , osvWireName :: Text
osvWireName = Ecosystem -> Text
ecosystemName Ecosystem
eco
        , osvEcosystemTag :: Maybe Ecosystem
osvEcosystemTag = Ecosystem -> Maybe Ecosystem
forall a. a -> Maybe a
Just Ecosystem
eco
        , osvMaxAdvisoryFanOut :: Int
osvMaxAdvisoryFanOut = Int
fanOut
        }
  where
    exportDirectory :: Text
exportDirectory = case Ecosystem
eco of
        Ecosystem
Npm -> Text
"npm"
        Ecosystem
PyPI -> Text
"PyPI"
        Ecosystem
RubyGems -> Text
"RubyGems"
        Ecosystem
Cargo -> Text
"crates.io"

    fanOut :: Int
fanOut = case Ecosystem
eco of
        Ecosystem
Npm -> Int
npmAdvisoryFanOut
        Ecosystem
PyPI -> Int
pypiAdvisoryFanOut
        Ecosystem
RubyGems -> Int
npmAdvisoryFanOut
        Ecosystem
Cargo -> Int
npmAdvisoryFanOut

-- | Resolve an internal ecosystem name, retaining unknown export names without a version grammar.
osvEcosystemNamed :: Text -> OsvEcosystem
osvEcosystemNamed :: Text -> OsvEcosystem
osvEcosystemNamed Text
name = OsvEcosystem
-> (Ecosystem -> OsvEcosystem) -> Maybe Ecosystem -> OsvEcosystem
forall b a. b -> (a -> b) -> Maybe a -> b
maybe OsvEcosystem
unserved Ecosystem -> OsvEcosystem
osvEcosystemFor (Text -> Maybe Ecosystem
parseEcosystem Text
name)
  where
    unserved :: OsvEcosystem
unserved =
        OsvEcosystem
            { osvExportDirectory :: Text
osvExportDirectory = Text
name
            , osvWireName :: Text
osvWireName = Text
name
            , osvEcosystemTag :: Maybe Ecosystem
osvEcosystemTag = Maybe Ecosystem
forall a. Maybe a
Nothing
            , osvMaxAdvisoryFanOut :: Int
osvMaxAdvisoryFanOut = Int
npmAdvisoryFanOut
            }