"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _routes = require("./routes");
var _jsxRuntime = require("react/jsx-runtime");
// Import types and route registry

const OxyRouter = ({
  oxyServices,
  initialScreen,
  onClose,
  onAuthenticated,
  theme,
  adjustSnapPoints,
  navigationRef,
  containerWidth
}) => {
  const [currentScreen, setCurrentScreen] = (0, _react.useState)(initialScreen);
  const [screenHistory, setScreenHistory] = (0, _react.useState)([initialScreen]);
  // Store props per screen for correct restoration on back
  const [screenPropsMap, setScreenPropsMap] = (0, _react.useState)({
    [initialScreen]: {}
  });

  // Update snap points when the screen changes
  (0, _react.useEffect)(() => {
    if (_routes.routes[currentScreen] && typeof adjustSnapPoints === 'function') {
      adjustSnapPoints(_routes.routes[currentScreen].snapPoints);
    }
  }, [currentScreen, adjustSnapPoints]);

  // Memoized navigation methods
  const navigate = (0, _react.useCallback)((screen, props = {}) => {
    if (__DEV__) console.log('OxyRouter: navigate called', screen, props);
    if (_routes.routes[screen]) {
      if (__DEV__) console.log('OxyRouter: screen found in routes');
      setCurrentScreen(screen);
      setScreenHistory(prev => [...prev, screen]);
      setScreenPropsMap(prev => ({
        ...prev,
        [screen]: props
      }));
    } else {
      console.error(`OxyRouter: Screen "${screen}" not found in routes:`, Object.keys(_routes.routes));
      if (process.env.NODE_ENV !== 'production') {
        console.error(`Screen "${screen}" not found`);
      }
    }
  }, []);
  const goBack = (0, _react.useCallback)(() => {
    setScreenHistory(prev => {
      if (prev.length > 1) {
        const newHistory = [...prev];
        newHistory.pop();
        const previousScreen = newHistory[newHistory.length - 1];
        setCurrentScreen(previousScreen);
        return newHistory;
      } else {
        if (onClose) onClose();
        return prev;
      }
    });
  }, [onClose]);

  // Expose the navigate function to the parent component
  (0, _react.useEffect)(() => {
    if (navigationRef) {
      navigationRef.current = navigate;
      if (__DEV__) console.log('OxyRouter: navigationRef set');
    }
    return () => {
      if (navigationRef) {
        navigationRef.current = null;
        if (__DEV__) console.log('OxyRouter: navigationRef cleared');
      }
    };
  }, [navigate, navigationRef]);

  // Expose the navigate method to the parent component (OxyProvider)
  (0, _react.useEffect)(() => {
    const handleNavigationEvent = event => {
      if (event && event.detail) {
        if (typeof event.detail === 'string') {
          navigate(event.detail);
        } else if (typeof event.detail === 'object' && event.detail.screen) {
          const {
            screen,
            props
          } = event.detail;
          navigate(screen, props || {});
        }
      }
    };
    let intervalId = null;
    if (typeof document !== 'undefined' && document.addEventListener) {
      document.addEventListener('oxy:navigate', handleNavigationEvent);
    } else {
      intervalId = setInterval(() => {
        const globalNav = globalThis.oxyNavigateEvent;
        if (globalNav && globalNav.screen) {
          navigate(globalNav.screen, globalNav.props || {});
          globalThis.oxyNavigateEvent = null;
        }
      }, 100);
    }
    return () => {
      if (typeof document !== 'undefined' && document.removeEventListener) {
        document.removeEventListener('oxy:navigate', handleNavigationEvent);
      }
      if (intervalId) {
        clearInterval(intervalId);
      }
    };
  }, [navigate]);

  // Render the current screen component
  const renderScreen = () => {
    const CurrentScreen = _routes.routes[currentScreen]?.component;
    if (!CurrentScreen) {
      if (process.env.NODE_ENV !== 'production') {
        console.error(`Screen "${currentScreen}" not found`);
      }
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.errorContainer
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(CurrentScreen, {
      oxyServices: oxyServices,
      navigate: navigate,
      goBack: goBack,
      onClose: onClose,
      onAuthenticated: onAuthenticated,
      theme: theme,
      containerWidth: containerWidth,
      ...(screenPropsMap[currentScreen] || {})
    });
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: styles.container,
    children: renderScreen()
  });
};
const styles = _reactNative.StyleSheet.create({
  container: {
    flex: 1
  },
  errorContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    minHeight: 100
  }
});
var _default = exports.default = OxyRouter;
//# sourceMappingURL=OxyRouter.js.map