"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useDataSource", {
    enumerable: true,
    get: function() {
        return useDataSource;
    }
});
const _react = require("react");
const _types = require("./shared/types");
const _utils = require("./utils");
const useDataSource = (data)=>{
    const [searchKey, setSearchKey] = (0, _react.useState)("");
    const [sortCondition, setSortCondition] = (0, _react.useState)({});
    const onSearch = (key)=>{
        setSearchKey(key);
    };
    const onSort = (key)=>{
        setSortCondition(key);
    };
    const dataSource = (0, _react.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(_utils.sortFunc[sortCondition.key](sortCondition.direction ?? _types.SortDirection.ASC));
        }
        return formattedData;
    }, [
        data,
        searchKey,
        sortCondition.key,
        sortCondition.direction
    ]);
    return [
        dataSource,
        {
            searchKey,
            onSearch,
            onSort
        }
    ];
};

//# sourceMappingURL=useDataSource.js.map