//
//  RequestRange.swift
//  Pachyderm
//
//  Created by Shadowfacts on 9/9/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import Foundation

public enum RequestRange: Sendable {
    case `default`
    case count(Int)
    /// Chronologically immediately before the given ID
    case before(id: String, count: Int?)
    /// Chronologically immediately after the given ID
    case after(id: String, count: Int?)
    
    public func withCount(_ count: Int) -> Self {
        switch self {
        case .default, .count(_):
            return .count(count)
        case .before(id: let id, count: _):
            return .before(id: id, count: count)
        case .after(id: let id, count: _):
            return .after(id: id, count: count)
        }
    }
}

extension RequestRange {
    var queryParameters: [Parameter] {
        switch self {
        case .default:
            return []
        case let .count(count):
            return ["limit" => count]
        case let .before(id, count):
            return ["max_id" => id, "count" => count]
        case let .after(id, count):
            return ["min_id" => id, "count" => count]
        }
    }
}
