import Foundation

typealias Trick = [Card]

typealias Hand = [Card]

extension Array where Element == Card {

    var unplayable: [PlayableCard] {
        map { $0.unplayable }
    }

    var playable: [PlayableCard] {
        map { $0.playable }
    }

    var points: Int {
        map { $0.points }
        .reduce(0, +)
    }

    func highCardIndex(forGame game: GameType) -> Int {
        game.sortingType.highCardIndex(cards: self)
    }

    func sortedCards(forGame game: GameType) -> [Card] {
        sortedCards(order: game.sortingType)
    }

    func sortedCards(order: CardOrder.Type) -> [Card] {
        order.sort(self)
    }

    func consecutiveTrumps(for game: GameType) -> Int {
        game.sortingType.consecutiveTrumps(self)
    }

    func trumpCount(for game: GameType) -> Int {
        game.sortingType.trumpCount(self)
    }

    func suitCount(_ suit: Card.Suit, in game: GameType) -> Int {
        filter { card in
            card.suit == suit && !game.sortingType.isTrump(card)
        }.count
    }

    /**
     Split cards into chunks to assign them to players.
     - Note: The array must contain a multiple of the `size` parameter
     */
    func split(intoChunksOf size: Int) -> [Hand] {
        stride(from: 0, to: count, by: size).map { i in
            Array(self[i..<i+4])
        }
    }

    var canOfferWedding: Bool {
        NormalCardOrder.trumpCount(self) == 1
    }
}
