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

-- | Archive member paths and distribution metadata coordinates.
module Ecluse.Core.Registry.PyPI.Archive.Path (metadataPath) where

import Data.Text qualified as T
import Ecluse.Core.Package (PackageName)
import Ecluse.Core.Registry (ParseError, parseRefusal)
import Ecluse.Core.Registry.PyPI.Project (DistributionKind (..), FileCoordinate (..), fileCoordinate)
import Ecluse.Core.Version (Version)

-- | Select only the admitted distribution metadata directory.
metadataPath :: PackageName -> Version -> DistributionKind -> [Text] -> Either ParseError Bool
metadataPath :: PackageName
-> Version -> DistributionKind -> [Text] -> Either ParseError Bool
metadataPath PackageName
package Version
version DistributionKind
kind [Text]
parts = case (DistributionKind
kind, [Text]
parts) of
    (DistributionKind
Wheel, [Text
root, Text
"METADATA"]) | Just Text
stem <- Text -> Text -> Maybe Text
T.stripSuffix Text
".dist-info" Text
root -> Text -> Either ParseError Bool
matchesIdentity Text
stem
    (DistributionKind
Sdist, [Text
root, Text
"PKG-INFO"]) -> Text -> Either ParseError Bool
matchesIdentity Text
root
    (DistributionKind, [Text])
_ -> Bool -> Either ParseError Bool
forall a b. b -> Either a b
Right Bool
False
  where
    matchesIdentity :: Text -> Either ParseError Bool
matchesIdentity Text
stem = case PackageName -> Text -> Maybe FileCoordinate
fileCoordinate PackageName
package (Text
stem Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".tar.gz") of
        Just FileCoordinate
coordinate | FileCoordinate -> Version
fcVersion FileCoordinate
coordinate Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
version -> Bool -> Either ParseError Bool
forall a b. b -> Either a b
Right Bool
True
        Maybe FileCoordinate
_ -> Text -> Either ParseError Bool
forall a. Text -> Either ParseError a
parseRefusal Text
"archive metadata directory disagrees with admitted identity"