//
//  MainViewViewModel.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/14/23.
//

import Combine
import Foundation

class WorkoutDetailViewModel: ObservableObject {
    enum WorkoutDetailViewModelStatus {
        case loading
        case showWorkout(Workout)
    }
    
    @Published var status: WorkoutDetailViewModelStatus
    let isPreview: Bool
    
    init(workout: Workout, status: WorkoutDetailViewModelStatus? = nil, isPreview: Bool) {
        self.status = .loading
        self.isPreview = isPreview
        
        if let passedStatus = status {
            self.status = passedStatus
        } else {
            WorkoutDetailFetchable(workoutID: workout.id).fetch(completion: { result in
                switch result {
                case .success(let model):
                    DispatchQueue.main.async {
                        self.status = .showWorkout(model)
                    }
                case .failure(let failure):
                    fatalError("failed \(failure.localizedDescription)")
                }
            })
        }
    }
}
