"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateBottomSheetState = exports.subscribeToBottomSheetState = exports.showBottomSheet = exports.setBottomSheetRef = exports.managerShowBottomSheet = exports.managerGoBack = exports.managerCloseBottomSheet = exports.getBottomSheetState = exports.closeBottomSheet = exports.bottomSheetStore = void 0;
var _routes = require("./routes");
var _vanilla = require("zustand/vanilla");
/**
 * Bottom Sheet Manager - Pure state management module
 * 
 * Uses Zustand (vanilla) for robust state management without React dependencies.
 * This ensures the manager can be imported safely anywhere.
 */

// Initial state
const initialState = {
  currentScreen: null,
  screenProps: {},
  currentStep: undefined,
  navigationHistory: [],
  isOpen: false
};

// Create vanilla store
const bottomSheetStore = exports.bottomSheetStore = (0, _vanilla.createStore)(() => initialState);

// Use a generic ref type to avoid importing React types

let bottomSheetRef = null;

/**
 * Set the bottom sheet ref so showBottomSheet can control it
 */
const setBottomSheetRef = ref => {
  bottomSheetRef = ref;
};

/**
 * Update the bottom sheet state
 * (Kept for backward compatibility, but prefer using store directly if possible)
 */
exports.setBottomSheetRef = setBottomSheetRef;
const updateBottomSheetState = updates => {
  bottomSheetStore.setState(state => ({
    ...state,
    ...updates
  }));
};

/**
 * Subscribe to bottom sheet state changes
 * (Wrapper around store.subscribe for backward compatibility)
 */
exports.updateBottomSheetState = updateBottomSheetState;
const subscribeToBottomSheetState = listener => {
  return bottomSheetStore.subscribe(listener);
};

/**
 * Get the current bottom sheet state
 * (Wrapper around store.getState for backward compatibility)
 */
exports.subscribeToBottomSheetState = subscribeToBottomSheetState;
const getBottomSheetState = () => {
  return bottomSheetStore.getState();
};

/**
 * Show the bottom sheet with a specific screen (internal - no route validation)
 */
exports.getBottomSheetState = getBottomSheetState;
const managerShowBottomSheet = (screen, props, options) => {
  const currentState = bottomSheetStore.getState();
  const addToHistory = options?.addToHistory !== false; // Default to true

  // If adding to history and there's a current screen, push it to history
  if (addToHistory && currentState.currentScreen) {
    const historyEntry = {
      screen: currentState.currentScreen,
      props: {
        ...currentState.screenProps
      },
      step: currentState.currentStep
    };

    // We need to create a new array for immutability
    const newHistory = [...currentState.navigationHistory, historyEntry];
    bottomSheetStore.setState({
      navigationHistory: newHistory
    });
  }

  // Determine the new step
  const newStep = options?.step ?? (props?.initialStep !== undefined ? props.initialStep : addToHistory ? undefined : currentState.currentStep);
  bottomSheetStore.setState({
    currentScreen: screen,
    screenProps: props || {},
    currentStep: newStep !== null && newStep !== undefined ? newStep : undefined,
    isOpen: true
  });

  // Present the sheet after state update
  if (bottomSheetRef?.current) {
    bottomSheetRef.current.present();
  }
};

/**
 * Close the bottom sheet (internal)
 */
exports.managerShowBottomSheet = managerShowBottomSheet;
const managerCloseBottomSheet = () => {
  bottomSheetStore.setState({
    currentScreen: null,
    screenProps: {},
    currentStep: undefined,
    navigationHistory: [],
    isOpen: false
  });
  if (bottomSheetRef?.current) {
    bottomSheetRef.current.dismiss();
  }
};

/**
 * Go back in navigation history
 * Returns true if back navigation was successful, false if history is empty
 */
exports.managerCloseBottomSheet = managerCloseBottomSheet;
const managerGoBack = () => {
  const currentState = bottomSheetStore.getState();

  // If there's history, pop and navigate to previous screen
  if (currentState.navigationHistory.length > 0) {
    const previous = currentState.navigationHistory[currentState.navigationHistory.length - 1];
    const newHistory = currentState.navigationHistory.slice(0, -1);
    bottomSheetStore.setState({
      currentScreen: previous.screen,
      screenProps: previous.props,
      currentStep: previous.step,
      navigationHistory: newHistory,
      isOpen: true
    });
    return true;
  }
  return false;
};

/**
 * Public API for showing bottom sheets
 */
exports.managerGoBack = managerGoBack;
const showBottomSheet = screenOrConfig => {
  let screen;
  let props = {};
  if (typeof screenOrConfig === 'string') {
    screen = screenOrConfig;
  } else {
    screen = screenOrConfig.screen;
    props = screenOrConfig.props || {};
  }
  if (!(0, _routes.isValidRoute)(screen)) {
    if (__DEV__) {
      console.warn(`[BottomSheetAPI] Invalid route: ${screen}`);
    }
    return;
  }
  managerShowBottomSheet(screen, props);
};

/**
 * Public API for closing bottom sheets
 */
exports.showBottomSheet = showBottomSheet;
const closeBottomSheet = () => {
  managerCloseBottomSheet();
};
exports.closeBottomSheet = closeBottomSheet;
//# sourceMappingURL=bottomSheetManager.js.map