import { useAppDispatch, useAppSelector } from "@/app/store"
import { deleteProduct, selectProducts, updateProduct } from "@/features/product/productSlice"
import { Product } from "@/lib/product";
import { FlatList, SafeAreaView, StyleSheet, Text } from "react-native";
import { ProductEditorItem } from "./ProductEditorItem";

export const ProductEditor = ({}) => {
    const products = useAppSelector(selectProducts) as Product [];

    const dispatch = useAppDispatch();
    
    function onProductDeleted(product_id: string) {
        dispatch(deleteProduct(product_id));
    }

    function onProductUpdated(product_id: string, product: Product) {
        dispatch(updateProduct(product));
    }


    return (
        <SafeAreaView>
            <h1 style={styles.h1}>Edit Products</h1>
            <FlatList
            data={products}
            renderItem={
                ({item}) => {
                    return (
                        <ProductEditorItem
                        product={item}
                        onProductDeleted={onProductDeleted}
                        onProductUpdated={onProductUpdated}
                        />
                    )
                }
            }
            />
        </SafeAreaView>
    )
}

const styles = StyleSheet.create({
    h1: {
        textAlign: "center",
        fontFamily: "sans-serif"
    },
    product: {
        
    }
})