import SwiftUI

/// Persists the user's selected task completion animation type.
/// Observed by task views to determine which animation to play after completing a task.
final class AnimationPreference: ObservableObject {
    static let shared = AnimationPreference()

    @AppStorage("selectedTaskAnimation") private var storedValue: String = TaskAnimationType.implode.rawValue

    /// The currently selected animation type, persisted across launches.
    var selectedAnimation: TaskAnimationType {
        get { TaskAnimationType(rawValue: storedValue) ?? .implode }
        set {
            storedValue = newValue.rawValue
            objectWillChange.send()
        }
    }

    /// Duration to wait for the celebration animation before moving the task.
    /// Returns 0 for `.none` or when Reduce Motion is enabled.
    func animationDuration(reduceMotion: Bool) -> Double {
        if reduceMotion || selectedAnimation == .none {
            return 0
        }
        return 2.2
    }

    private init() {}
}
