import Foundation
import HealthKit

extension HKWorkout {

    var distance: HKQuantity? {
        statistics(for: .init(distanceType))?.maximumQuantity()
    }

    private var distanceType: HKQuantityTypeIdentifier {
        switch workoutActivityType {
        case .running, .walking, .hiking: return .distanceWalkingRunning
        case .snowboarding, .snowSports, .downhillSkiing, .crossCountrySkiing: return .distanceDownhillSnowSports
        case .cycling, .handCycling: return .distanceCycling
        case .swimming, .paddleSports, .underwaterDiving: return .distanceSwimming
        case .wheelchairRunPace, .wheelchairWalkPace: return .distanceWheelchair
        default:
            return .distanceWalkingRunning
        }
    }
}
