//
//  Array+Extensions.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 03/03/2024.
//

import Foundation

extension Array {
    func chunked(into size: Int) -> [[Element]] {
        return stride(from: 0, to: count, by: size).map {
            Array(self[$0 ..< Swift.min($0 + size, count)])
        }
    }
    
    func anySatisfy(_ p: (Element) -> Bool) -> Bool {
        return first(where: { p($0) }) != nil
        //return !self.allSatisfy { !p($0) }
    }
}

extension Array where Element: Equatable {
    
    /// Remove first collection element that is equal to the given `object` or `element`:
    mutating func remove(elements: [Element]) {
        elements.forEach {
            if let index = firstIndex(of: $0) {
                remove(at: index)
            }
        }
    }
}

extension Array where Element: CustomStringConvertible {
    func customJoined(separator: String, lastSeparator: String) -> String {
        switch count {
        case 0:
            return ""
        case 1:
            return "\(self[0])"
        case 2:
            return "\(self[0]) \(lastSeparator) \(self[1])"
        default:
            let firstPart = dropLast().map { "\($0)" }.joined(separator: ", ")
            let lastPart = "\(lastSeparator) \(last!)"
            return "\(firstPart) \(lastPart)"
        }
    }
}

