"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavigationProvider = NavigationProvider;
exports.useNavigation = useNavigation;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const types_1 = require("../types");
function navigationReducer(state, action) {
    switch (action.type) {
        case "SET_VIEW":
            return {
                ...state,
                previousView: state.view,
                view: action.view,
                mode: "normal",
                modeOriginView: null,
                ...(action.preserveSelection
                    ? {}
                    : {
                        selectedListIndex: 0,
                        selectedCardIndex: 0,
                        listScrollOffset: 0,
                    }),
            };
        case "GO_BACK":
            return {
                ...state,
                view: state.previousView || "home",
                previousView: null,
                mode: "normal",
                selectedListIndex: 0,
                selectedCardIndex: 0,
                listScrollOffset: 0,
            };
        case "SELECT_LIST":
            return { ...state, selectedListIndex: action.index, selectedCardIndex: 0 };
        case "SELECT_CARD":
            return { ...state, selectedCardIndex: action.index };
        case "SET_LIST_SCROLL":
            return { ...state, listScrollOffset: action.offset };
        case "SET_MODE":
            return {
                ...state,
                mode: action.mode,
                // When entering an editing mode, snapshot the origin view.
                // If originView is explicitly provided (from CardDetailView), use that.
                // When transitioning between modes (e.g. toggle-checklist → add-checkitem),
                // preserve the existing modeOriginView.
                // When returning to normal, clear it.
                ...(action.mode !== "normal"
                    ? {
                        modeOriginView: action.originView || state.modeOriginView || state.view,
                    }
                    : { modeOriginView: null }),
            };
        case "SET_SEARCH":
            return { ...state, searchQuery: action.query };
        case "SET_STATUS":
            return { ...state, statusMessage: action.message };
        case "SET_CONFIRM":
            return { ...state, confirmAction: action.action };
        case "RESET_SELECTION":
            return { ...state, selectedListIndex: 0, selectedCardIndex: 0, listScrollOffset: 0 };
        default:
            return state;
    }
}
const initialState = {
    view: "home",
    previousView: null,
    modeOriginView: null,
    selectedListIndex: 0,
    selectedCardIndex: 0,
    listScrollOffset: 0,
    mode: "normal",
    searchQuery: "",
    statusMessage: null,
    confirmAction: null,
};
const NavigationContext = (0, react_1.createContext)(null);
function NavigationProvider({ children, config, }) {
    const [state, dispatch] = (0, react_1.useReducer)(navigationReducer, initialState);
    return ((0, jsx_runtime_1.jsx)(NavigationContext.Provider, { value: { state, dispatch, config: config || types_1.DEFAULT_TUI_CONFIG }, children: children }));
}
function useNavigation() {
    const ctx = (0, react_1.useContext)(NavigationContext);
    if (!ctx) {
        throw new Error("useNavigation must be used within NavigationProvider");
    }
    return ctx;
}
