import React, { useEffect } from 'react';
import Styled from 'styled-components/native';
import { FlatList } from 'react-native';

const emptyCallback = () => {};

export default SearchList = ({
    onEndReachedThreshold=0.5, onEndReached=emptyCallback, onRefresh=emptyCallback, refreshing=false, 
    setPage=()=>{}, page=1, pages=10, keyExtractor=(item, index)=>{ return index+'' }, ...rest
}) => {
    return (
        <FlatList keyExtractor={keyExtractor} {...rest} onEndReachedThreshold={0.5} 
            onEndReached={() => {

            if(refreshing) return;
            if((typeof page === 'number' ? page : 1) >= pages) return;
            console.log('onEndReached');

            if(typeof page !== 'number') setPage(1);
            else setPage(page + 1);

        }} refreshing={refreshing} onRefresh={() => {

            if(refreshing) return;
            console.log('onRefresh');

            setPage('refresh-'+Math.random());
        }} />
    );
};

export const SearchListContainer = Styled.View`
    flex: 1;
    background-color: #fff;
`;