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

-- | Packed Compact Index discovery does not promise admission.
module Ecluse.Core.Registry.RubyGems.Directory (
    GemDirectory (..),
    DirectoryRead (..),
    directoryWeight,
    directorySteps,
    directoryMergeWorkspace,
    mergeDirectories,
) where

import Data.ByteString qualified as BS
import Data.ByteString.Char8 qualified as B8

import Ecluse.Core.Registry.JsonStream (Steps)
import Ecluse.Core.Registry.RubyGems.Directory.Internal (DirectoryRead (..), GemDirectory (..), applyChanges, directoryNameCeiling, directoryStepsWithin)
import Ecluse.Core.Server.MemoryModel (decodedDocumentCharge)

-- | Packed bytes and fixed bookkeeping share the metadata retention pool.
directoryWeight :: GemDirectory -> Int
directoryWeight :: GemDirectory -> Int
directoryWeight GemDirectory
directory = ByteString -> Int
BS.length (GemDirectory -> ByteString
directoryBytes GemDirectory
directory) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
256

-- | Repeated rows apply additions and removals before packing the final snapshot. A malformed row is dropped on its own.
directorySteps :: (Monad m) => (Int -> m ()) -> Steps m DirectoryRead
directorySteps :: forall (m :: * -> *).
Monad m =>
(Int -> m ()) -> Steps m DirectoryRead
directorySteps = Int -> (Int -> m ()) -> Steps m DirectoryRead
forall (m :: * -> *).
Monad m =>
Int -> (Int -> m ()) -> Steps m DirectoryRead
directoryStepsWithin Int
directoryNameCeiling

-- | Expand version tokens only for names present in both source directories.
mergeDirectories :: GemDirectory -> GemDirectory -> GemDirectory
mergeDirectories :: GemDirectory -> GemDirectory -> GemDirectory
mergeDirectories publicDirectory :: GemDirectory
publicDirectory@(GemDirectory ByteString
public) privateDirectory :: GemDirectory
privateDirectory@(GemDirectory ByteString
private)
    | ByteString -> Bool
BS.null ByteString
private = GemDirectory
publicDirectory
    | ByteString -> Bool
BS.null ByteString
public = GemDirectory
privateDirectory
    | Bool
otherwise = ByteString -> GemDirectory
GemDirectory ([ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString] -> [ByteString]
merge (ByteString -> [ByteString]
B8.lines ByteString
public) (ByteString -> [ByteString]
B8.lines ByteString
private)))
  where
    merge :: [ByteString] -> [ByteString] -> [ByteString]
merge [] [ByteString]
right = (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n") [ByteString]
right
    merge [ByteString]
left [] = (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n") [ByteString]
left
    merge left :: [ByteString]
left@(ByteString
a : [ByteString]
as) right :: [ByteString]
right@(ByteString
b : [ByteString]
bs) = case ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (ByteString -> ByteString
nameOf ByteString
a) (ByteString -> ByteString
nameOf ByteString
b) of
        Ordering
LT -> (ByteString
a ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n") ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString] -> [ByteString] -> [ByteString]
merge [ByteString]
as [ByteString]
right
        Ordering
GT -> (ByteString
b ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n") ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString] -> [ByteString] -> [ByteString]
merge [ByteString]
left [ByteString]
bs
        Ordering
EQ -> (ByteString -> ByteString
nameOf ByteString
a ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString -> ByteString -> ByteString
applyChanges (ByteString -> ByteString
versionsOf ByteString
a) (ByteString -> ByteString
versionsOf ByteString
b) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" \n") ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString] -> [ByteString] -> [ByteString]
merge [ByteString]
as [ByteString]
bs
    nameOf :: ByteString -> ByteString
nameOf = (Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
32)
    versionsOf :: ByteString -> ByteString
versionsOf = (Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
32) (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
BS.drop Int
1 (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Bool) -> ByteString -> ByteString
BS.dropWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
32)

-- | Reserve only the largest colliding history before expanding either token set.
directoryMergeWorkspace :: GemDirectory -> GemDirectory -> Int
directoryMergeWorkspace :: GemDirectory -> GemDirectory -> Int
directoryMergeWorkspace (GemDirectory ByteString
public) (GemDirectory ByteString
private) = Int -> [ByteString] -> [ByteString] -> Int
go Int
0 (ByteString -> [ByteString]
B8.lines ByteString
public) (ByteString -> [ByteString]
B8.lines ByteString
private)
  where
    go :: Int -> [ByteString] -> [ByteString] -> Int
go Int
peak [] [ByteString]
_ = Int
peak
    go Int
peak [ByteString]
_ [] = Int
peak
    go Int
peak left :: [ByteString]
left@(ByteString
a : [ByteString]
as) right :: [ByteString]
right@(ByteString
b : [ByteString]
bs) = case ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
32) ByteString
a) ((Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
32) ByteString
b) of
        Ordering
LT -> Int -> [ByteString] -> [ByteString] -> Int
go Int
peak [ByteString]
as [ByteString]
right
        Ordering
GT -> Int -> [ByteString] -> [ByteString] -> Int
go Int
peak [ByteString]
left [ByteString]
bs
        Ordering
EQ -> Int -> [ByteString] -> [ByteString] -> Int
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
peak (ByteString -> Int
decodedDocumentCharge ByteString
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
decodedDocumentCharge ByteString
b)) [ByteString]
as [ByteString]
bs