import { useMemo, useState } from "react";
import { SortDirection } from "./shared/types";
import { sortFunc } from "./utils";
export const useDataSource = (data)=>{
    const [searchKey, setSearchKey] = useState("");
    const [sortCondition, setSortCondition] = useState({});
    const onSearch = (key)=>{
        setSearchKey(key);
    };
    const onSort = (key)=>{
        setSortCondition(key);
    };
    const dataSource = useMemo(()=>{
        let formattedData = [
            ...data ?? []
        ];
        if (searchKey) {
            formattedData = formattedData?.filter((item)=>new RegExp(searchKey, "i").test(item.symbol));
        }
        if (typeof sortCondition.key !== "undefined" && sortCondition.direction !== 0) {
            formattedData?.sort(sortFunc[sortCondition.key](sortCondition.direction ?? SortDirection.ASC));
        }
        return formattedData;
    }, [
        data,
        searchKey,
        sortCondition.key,
        sortCondition.direction
    ]);
    return [
        dataSource,
        {
            searchKey,
            onSearch,
            onSort
        }
    ];
};

//# sourceMappingURL=useDataSource.js.map