import Foundation

extension Array where Element == Card {
    /// Returns the suffix of face-up cards from the end of the array.
    var faceUpSuffix: [Card] {
        var result: [Card] = []
        for card in reversed() {
            guard card.isFaceUp else { break }
            result.insert(card, at: 0)
        }
        return result
    }

    /// Returns the top card (last element), or nil if empty.
    var topCard: Card? { last }
}

extension Array {
    /// Returns the longest suffix where all elements satisfy the predicate.
    func trailingSuffix(while predicate: (Element) -> Bool) -> ArraySlice<Element> {
        var startIndex = endIndex
        for index in indices.reversed() {
            guard predicate(self[index]) else { break }
            startIndex = index
        }
        return self[startIndex..<endIndex]
    }
}
