"use strict";

import React, { createContext, memo, useCallback, useContext, useMemo } from 'react';
import { View, Text, Pressable, Animated, ScrollView } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { usePressAnimation } from "../hooks/usePressAnimation.js";
import { borderRadius, space } from "../styles/tokens.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const TabsContext = /*#__PURE__*/createContext({
  value: '',
  onValueChange: () => {},
  variant: 'underline'
});
const TabsBarComponent = ({
  value,
  onValueChange,
  variant = 'underline',
  children,
  style,
  testID
}) => {
  const theme = useTheme();
  const contextValue = useMemo(() => ({
    value,
    onValueChange,
    variant
  }), [value, onValueChange, variant]);
  const containerStyle = useMemo(() => {
    const base = {
      flexDirection: 'row',
      alignItems: 'center'
    };
    switch (variant) {
      case 'underline':
        base.borderBottomWidth = 1;
        base.borderBottomColor = theme.colors.borderLight;
        break;
      case 'filled':
        base.backgroundColor = theme.colors.backgroundSecondary;
        base.borderRadius = borderRadius.sm;
        base.padding = 2;
        break;
      case 'outlined':
        base.borderWidth = 1;
        base.borderColor = theme.colors.border;
        base.borderRadius = borderRadius.sm;
        base.padding = 2;
        break;
    }
    return base;
  }, [variant, theme]);
  return /*#__PURE__*/_jsx(TabsContext.Provider, {
    value: contextValue,
    children: /*#__PURE__*/_jsx(ScrollView, {
      horizontal: true,
      showsHorizontalScrollIndicator: false,
      contentContainerStyle: [containerStyle, style],
      testID: testID,
      children: children
    })
  });
};
const TabComponent = ({
  value,
  label,
  icon,
  disabled = false,
  style,
  textStyle
}) => {
  const theme = useTheme();
  const {
    value: selectedValue,
    onValueChange,
    variant
  } = useContext(TabsContext);
  const isSelected = value === selectedValue;
  const {
    scaleAnim,
    onPressIn,
    onPressOut
  } = usePressAnimation(0.97);
  const handlePress = useCallback(() => {
    if (!disabled) {
      onValueChange(value);
    }
  }, [value, disabled, onValueChange]);
  const tabStyle = useMemo(() => {
    const base = {
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'center',
      paddingHorizontal: space.lg,
      paddingVertical: space.sm,
      gap: space.xs
    };
    switch (variant) {
      case 'underline':
        if (isSelected) {
          base.borderBottomWidth = 2;
          base.borderBottomColor = theme.colors.primary;
          base.marginBottom = -1; // overlap container border
        }
        break;
      case 'filled':
        if (isSelected) {
          base.backgroundColor = theme.colors.card;
          base.borderRadius = borderRadius.xs + 2;
          base.shadowColor = theme.colors.shadow;
          base.shadowOffset = {
            width: 0,
            height: 1
          };
          base.shadowOpacity = 0.1;
          base.shadowRadius = 2;
          base.elevation = 1;
        }
        break;
      case 'outlined':
        if (isSelected) {
          base.backgroundColor = theme.colors.primary;
          base.borderRadius = borderRadius.xs + 2;
        }
        break;
    }
    return base;
  }, [variant, isSelected, theme]);
  const labelStyle = useMemo(() => {
    const base = {
      fontSize: 14,
      fontWeight: isSelected ? '600' : '500'
    };
    if (variant === 'outlined' && isSelected) {
      base.color = theme.colors.primaryForeground;
    } else if (isSelected) {
      base.color = theme.colors.primary;
    } else {
      base.color = theme.colors.textSecondary;
    }
    return base;
  }, [variant, isSelected, theme]);
  return /*#__PURE__*/_jsx(Animated.View, {
    style: {
      transform: [{
        scale: scaleAnim
      }]
    },
    children: /*#__PURE__*/_jsxs(Pressable, {
      style: [tabStyle, disabled && {
        opacity: 0.4
      }, style],
      onPress: handlePress,
      onPressIn: onPressIn,
      onPressOut: onPressOut,
      disabled: disabled,
      accessibilityRole: "tab",
      accessibilityState: {
        selected: isSelected,
        disabled
      },
      children: [icon, /*#__PURE__*/_jsx(Text, {
        style: [labelStyle, textStyle],
        children: label
      })]
    })
  });
};
const TabPanelComponent = ({
  value,
  children,
  style
}) => {
  const {
    value: selectedValue
  } = useContext(TabsContext);
  if (value !== selectedValue) return null;
  return /*#__PURE__*/_jsx(View, {
    style: style,
    children: children
  });
};
export const TabsBar = /*#__PURE__*/memo(TabsBarComponent);
TabsBar.displayName = 'TabsBar';
export const Tab = /*#__PURE__*/memo(TabComponent);
Tab.displayName = 'Tab';
export const TabPanel = /*#__PURE__*/memo(TabPanelComponent);
TabPanel.displayName = 'TabPanel';
//# sourceMappingURL=Tabs.js.map