"use strict";

import { isValidRoute } from './routes';
import { createStore } from '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
export const bottomSheetStore = 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
 */
export const setBottomSheetRef = ref => {
  bottomSheetRef = ref;
};

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

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

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

/**
 * Show the bottom sheet with a specific screen (internal - no route validation)
 */
export 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)
 */
export 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
 */
export 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
 */
export const showBottomSheet = screenOrConfig => {
  let screen;
  let props = {};
  if (typeof screenOrConfig === 'string') {
    screen = screenOrConfig;
  } else {
    screen = screenOrConfig.screen;
    props = screenOrConfig.props || {};
  }
  if (!isValidRoute(screen)) {
    if (__DEV__) {
      console.warn(`[BottomSheetAPI] Invalid route: ${screen}`);
    }
    return;
  }
  managerShowBottomSheet(screen, props);
};

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