"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    TableContext: function() {
        return TableContext;
    },
    TableProvider: function() {
        return TableProvider;
    },
    defaultSortter: function() {
        return defaultSortter;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const TableContext = /*#__PURE__*/ (0, _react.createContext)({});
const defaultSortter = (r1, r2, sortOrder, key)=>{
    if (sortOrder === "asc") {
        return Number(r1[key]) - Number(r2[key]);
    }
    return Number(r2[key]) - Number(r1[key]);
};
const TableProvider = (props)=>{
    const [sortKey, setSortKey] = (0, _react.useState)();
    const [expandedRowKeys, setExpandedRowKeys] = (0, _react.useState)([]);
    // const [sortOrder, setSortOrder] = useState<SortOrder>();
    const getLeftFixedColumnsWidth = (index)=>{
        return props.columns.reduce((acc, cur, i)=>{
            if (i < index && cur.fixed === "left") {
                return acc + (cur.width || 0);
            }
            return acc;
        }, 0);
    };
    const getRightFixedColumnsWidth = (index)=>{
        return props.columns.reduce((acc, cur, i)=>{
            if (i > index && cur.fixed === "right") {
                return acc + (cur.width || 0);
            }
            return acc;
        }, 0);
    };
    const getLeftFixedColumnsPosition = ()=>{
        let left = 0;
        for(let index = 0; index < props.columns.length; index++){
            const element = props.columns[index];
            if (element.fixed !== "left") {
                break;
            } else {
                left += element.width || 0;
            }
        }
        return left;
    };
    const getRightFixedColumnsPosition = ()=>{
        let right = 0;
        for(let index = props.columns.length - 1; index >= 0; index--){
            const element = props.columns[index];
            if (element.fixed !== "right") {
                break;
            } else {
                right += element.width || 0;
            }
        }
        return right;
    };
    const dataSource = (0, _react.useMemo)(()=>{
        if (!props.dataSource) {
            return [];
        }
        if (!sortKey || !sortKey[0]) {
            return props.dataSource || [];
        }
        // sort by onSort function
        return [
            ...props.dataSource
        ].sort((r1, r2)=>{
            if (typeof sortKey[0] === "string") {
                const col = props.columns.find((col)=>col.dataIndex === sortKey[0]);
                let sorttor = typeof col?.onSort === "function" ? col.onSort : defaultSortter;
                return sorttor(r1, r2, sortKey[1], sortKey[0]);
            }
            return 0;
        });
    }, [
        props.dataSource,
        sortKey
    ]);
    const toggleExpandRow = (0, _react.useCallback)((key)=>{
        setExpandedRowKeys((prev)=>{
            if (prev[0] === key) {
                return [];
            }
            return [
                key
            ];
        // if (prev.includes(key)) {
        //   return prev.filter((k) => k !== key);
        // }
        // return [...prev, key];
        });
    }, []);
    const onSort = (key)=>{
        setSortKey((prev)=>{
            if (prev?.[0] === key) {
                if (prev?.[1] === "asc") {
                    return undefined;
                }
                return [
                    key,
                    "asc"
                ];
            }
            return [
                key,
                "desc"
            ];
        });
    };
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(TableContext.Provider, {
        value: {
            columns: props.columns,
            dataSource: dataSource,
            sortKey: sortKey?.[0],
            sortOrder: sortKey?.[1],
            canExpand: props.canExpand,
            expandedRowKeys,
            toggleExpandRow,
            onSort: onSort,
            getLeftFixedColumnsWidth,
            getRightFixedColumnsWidth,
            getLeftFixedColumnsPosition,
            getRightFixedColumnsPosition
        },
        children: props.children
    });
};

//# sourceMappingURL=tableContext.js.map