//
//  BaseNavigationManager.swift
//  Allovoisins
//
//  Created by Victor on 06/12/2024.
//  Copyright © 2024 AlloVoisins. All rights reserved.
//

import SwiftUI

protocol NavigationRoute: Hashable {}

class BaseNavigationManager<Route: NavigationRoute>: ObservableObject {
    @Published var navigationPath = NavigationPath()
    @Published private var routeStack: [Route] = []
    @Published var shouldDismissToRoot: Bool = false
    @Published var shouldDismiss: Bool = false

    var currentRoute: Route? {
        routeStack.last
    }

    var canGoBack: Bool {
        !routeStack.isEmpty && !navigationPath.isEmpty
    }

    func navigateTo(_ route: Route) {
        navigationPath.append(route)
        routeStack.append(route)
    }

    func goBack() {
        if canGoBack {
            navigationPath.removeLast()
            routeStack.removeLast()
        } else {
            shouldDismiss = true
        }
    }

    func resetNavigation() {
        navigationPath = NavigationPath()
        routeStack = []
    }

    func dismissToRoot() {
        shouldDismissToRoot = true
    }
}
