import { Ionicons } from "@expo/vector-icons"; import BottomSheet, { BottomSheetBackdrop, // Import BottomSheetBackdrop BottomSheetBackdropProps, BottomSheetView, } from "@gorhom/bottom-sheet"; import Checkbox from "expo-checkbox"; import React, { useCallback, useRef, useState } from "react"; import { I18nManager, ImageSourcePropType, StyleSheet, Text, TextStyle, TouchableOpacity, View, ViewStyle, } from "react-native"; import { ThemeType, useTheme } from "../../theme"; // Import ThemeType import { BrandIcon } from "../BrandIcon"; import { AppButtonProps, Button } from "../Button"; import PointsRangeSelector from "./PointsRangeSelector"; // ... (Keep existing type definitions: SortOption, Brand, FilterResult) ... type SortOption = { name: string; value: string; id: number; checked?: boolean; }; export type Brand = { id: string; name: string; logo: ImageSourcePropType; checked?: boolean; }; export type FilterResult = { sort: SortOption | undefined; pointsRange: { min: number; max: number }; selectedBrands: Brand[]; }; interface FiltersProps { isRTL?: boolean; sortOptions?: SortOption[]; sortSectionTitle?: string; sectionTitleStyle?: TextStyle; sortSectionStyle?: ViewStyle; rangeMinLimit?: number; rangeMaxLimit?: number; rangeInitialMin?: number; rangeInitialMax?: number; showPointsRange?: boolean; pointsRangeSectionTitle?: string; pointsRangeContainerStyle?: ViewStyle; pointsRangeInputsContainerStyle?: ViewStyle; pointsRangeCustomInputContainerStyle?: ViewStyle; pointsRangeCustomInputFieldStyle?: ViewStyle; pointsRangeSliderContainerStyle?: ViewStyle; pointsRangeMultiSliderContainerStyle?: ViewStyle; pointsRangeTrackStyle?: ViewStyle; pointsRangeSelectedTrackStyle?: ViewStyle; pointsRangeUnselectedTrackStyle?: ViewStyle; pointsRangeMarkerStyle?: ViewStyle; pointsRangeMarkerEnabledStyle?: ViewStyle; pointsRangeMarkerDisabledStyle?: ViewStyle; pointsRangeMarkerDotStyle?: ViewStyle; pointsRangeInputProps?: any; pointsRangeMultiSliderProps?: any; brands?: Brand[]; brandContainerStyle?: ViewStyle; brandActiveIcon?: React.ReactNode; brandActiveIconStyle?: ViewStyle; brandSectionTile?: string; onActionButtonPress?: (result: FilterResult) => void; containerStyle?: ViewStyle; headerStyle?: ViewStyle; titleStyle?: TextStyle; resetTextStyle?: TextStyle; sectionStyle?: ViewStyle; optionRowStyle?: ViewStyle; optionTextStyle?: TextStyle; checkboxColor?: string; applyButtonStyle?: ViewStyle; applyButtonTextStyle?: TextStyle; bottomSheetStyle?: ViewStyle; bottomSheetBackgroundStyle?: ViewStyle; headerTitleText?: string; headerResetText?: string; bottomSheetIndex?: number; handleIndicatorStyle?: ViewStyle; applyButtonText?: string; overlayColor?: string; onClose?: () => void; visible?: boolean; applyButtonComponentProps?: Partial< Omit >; } const Filters: React.FC = ({ isRTL: isRTLProp, sortOptions = [], sortSectionTitle = "Sort", sectionTitleStyle, sortSectionStyle, onActionButtonPress, rangeInitialMax = 1000, rangeInitialMin = 0, rangeMaxLimit = 1000, rangeMinLimit = 0, pointsRangeContainerStyle, pointsRangeInputsContainerStyle, pointsRangeCustomInputContainerStyle, pointsRangeCustomInputFieldStyle, pointsRangeSliderContainerStyle, pointsRangeMultiSliderContainerStyle, pointsRangeTrackStyle, pointsRangeSelectedTrackStyle, pointsRangeUnselectedTrackStyle, pointsRangeMarkerStyle, pointsRangeMarkerEnabledStyle, pointsRangeMarkerDisabledStyle, pointsRangeMarkerDotStyle, pointsRangeInputProps, pointsRangeMultiSliderProps, containerStyle, headerStyle, titleStyle, resetTextStyle, sectionStyle, optionRowStyle, optionTextStyle, checkboxColor: checkboxColorProp, applyButtonStyle, applyButtonTextStyle, bottomSheetStyle, bottomSheetIndex = 0, handleIndicatorStyle, brands = [], brandContainerStyle, brandActiveIcon, showPointsRange = true, pointsRangeSectionTitle = "Points range", brandActiveIconStyle, brandSectionTile = "Brands", applyButtonText = "Apply Filter", headerResetText = "Reset All", headerTitleText = "Filter", onClose, visible = true, applyButtonComponentProps, bottomSheetBackgroundStyle }) => { const { theme, isRTL: themeIsRTL } = useTheme(); const isRTL = isRTLProp ?? themeIsRTL ?? I18nManager.isRTL; const bottomSheetRef = useRef(null); const styles = createStyles(theme, isRTL); const checkboxColor = checkboxColorProp ?? theme.primary; const handleSheetChanges = useCallback( (index: number) => { if (index === -1) { if (onClose) { onClose(); } } }, [onClose] ); const [resetCounter, setResetCounter] = useState(0); const [checkedItem, setCheckedItem] = useState( sortOptions.find((option) => option.checked)?.id || null ); const [selectedBrands, setSelectedBrands] = useState>( brands.reduce((acc, option) => { acc[option.id] = option.checked || false; return acc; }, {} as Record) ); const [pointsRange, setPointsRange] = useState<{ min: number; max: number }>({ min: rangeInitialMin, max: rangeInitialMax, }); const handleCheckboxChange = (id: number) => { setCheckedItem(id); }; const handleBrandSelect = (id: string) => { setSelectedBrands((prevState) => ({ ...prevState, [id]: !prevState[id], })); }; const closeBottomSheet = useCallback( () => { bottomSheetRef.current?.close(); }, [ /* onClose */ ] ); const handleApplyFilter = () => { if (onActionButtonPress) { const selectedSortOption = sortOptions.find( (option) => option.id == checkedItem ); const selectedBrandsList = brands.filter( (brand) => selectedBrands[brand.id] ); const filterResult: FilterResult = { sort: selectedSortOption, pointsRange: pointsRange, selectedBrands: selectedBrandsList, }; onActionButtonPress(filterResult); } closeBottomSheet(); }; const handleResetAll = () => { const resetBrands = brands.reduce((acc, option) => { acc[option.id] = false; return acc; }, {} as Record); setCheckedItem(null); setSelectedBrands(resetBrands); setPointsRange({ min: rangeMinLimit, max: rangeInitialMax }); setResetCounter((prev) => prev + 1); }; const renderBackdrop = useCallback( (props: BottomSheetBackdropProps) => ( ), [] ); return ( null} style={bottomSheetStyle} backgroundStyle={[{ backgroundColor: theme.surface }, bottomSheetBackgroundStyle]} handleIndicatorStyle={[styles.handleIndicator, handleIndicatorStyle]} enablePanDownToClose={true} backdropComponent={renderBackdrop} > {headerTitleText} {headerResetText} {sortOptions && sortOptions.length > 0 && ( {sortSectionTitle} {sortOptions.map((option) => ( handleCheckboxChange(option.id)} /> {option.name} ))} )} {showPointsRange && ( {pointsRangeSectionTitle} { setPointsRange({ min, max }); }} /> )} {brands && brands.length > 0 && ( {brandSectionTile} {brands.map((brand) => ( handleBrandSelect(brand.id.toString())} brandLogo={brand.logo} brandName={brand.name} selectionIndicatorIcon={ brandActiveIcon || ( ) } selectionIndicatorStyle={brandActiveIconStyle} /> ))} )}