/**
 * Navigation State Machine
 *
 * XState v5 machine for TUI navigation and screen management.
 * Provides exhaustive type checking and prevents impossible states.
 */
import { assign, createMachine } from "xstate";
/**
 * Navigation state machine for TUI screens.
 *
 * States:
 * - dashboard: Main flow list view
 * - addWizard: Multi-step add flow wizard
 *   - pathStep: Enter repository path
 *   - nameStep: Enter optional display name
 *   - confirmStep: Review and confirm
 * - confirmAction: Confirm destructive action (remove)
 * - exiting: Final state (app exits)
 */
export const navigationMachine = createMachine({
    id: "navigation",
    initial: "dashboard",
    context: {
        selectedFlowId: undefined,
        wizardData: { path: undefined, name: undefined },
        confirmAction: undefined
    },
    // XState v5 TypeScript Integration: The 'types' property with 'as' casting is required
    // by XState v5 for proper type inference. This is an intentional exception as there is no alternative XState API that
    // provides equivalent type safety. See: https://stately.ai/docs/typescript
    types: {
        context: {},
        events: {}
    },
    states: {
        dashboard: {
            on: {
                SELECT_FLOW: {
                    actions: assign({
                        selectedFlowId: ({ event }) => event.flowId
                    })
                },
                OPEN_ADD_WIZARD: {
                    target: "addWizard",
                    actions: assign({
                        wizardData: () => ({ path: undefined, name: undefined })
                    })
                },
                CONFIRM_REMOVE: {
                    target: "confirmAction",
                    actions: assign({
                        confirmAction: ({ event }) => ({
                            type: "remove",
                            flowId: event.flowId,
                            flowName: event.flowName
                        })
                    })
                },
                EXIT: { target: "exiting" }
            }
        },
        addWizard: {
            initial: "pathStep",
            on: {
                // Handle COMPLETE at addWizard level since component manages its own step state
                COMPLETE: { target: "#navigation.dashboard" }
            },
            states: {
                pathStep: {
                    on: {
                        SET_PATH: {
                            actions: assign({
                                wizardData: ({ context, event }) => ({
                                    ...context.wizardData,
                                    path: event.path
                                })
                            })
                        },
                        NEXT: {
                            target: "nameStep",
                            guard: ({ context }) => context.wizardData.path != null
                        },
                        CANCEL: { target: "#navigation.dashboard" }
                    }
                },
                nameStep: {
                    on: {
                        SET_NAME: {
                            actions: assign({
                                wizardData: ({ context, event }) => ({
                                    ...context.wizardData,
                                    name: event.name
                                })
                            })
                        },
                        NEXT: { target: "confirmStep" },
                        BACK: { target: "pathStep" },
                        CANCEL: { target: "#navigation.dashboard" }
                    }
                },
                confirmStep: {
                    on: {
                        BACK: { target: "nameStep" },
                        CANCEL: { target: "#navigation.dashboard" }
                    }
                }
            }
        },
        confirmAction: {
            on: {
                CONFIRM: {
                    target: "dashboard",
                    actions: assign({
                        confirmAction: () => undefined
                    })
                },
                CANCEL: {
                    target: "dashboard",
                    actions: assign({
                        confirmAction: () => undefined
                    })
                }
            }
        },
        exiting: {
            type: "final"
        }
    }
});
//# sourceMappingURL=navigation.js.map