"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useSort", {
    enumerable: true,
    get: function() {
        return useSort;
    }
});
const _react = require("react");
const _types = require("./shared/types");
const useSort = (value, readLastSortCondition)=>{
    const sessionSortKey = sessionStorage.getItem("default_sort_key") || "vol";
    const sessionSortDirection = sessionStorage.getItem("default_sort_derection") || "2";
    const initSortKey = readLastSortCondition ? value || sessionSortKey : undefined;
    const initDirection = readLastSortCondition ? (0, _types.parseSortDirection)(sessionSortDirection) || _types.SortDirection.DESC : _types.SortDirection.NONE;
    // @ts-ignore
    const [sortKey, setSortKey] = (0, _react.useState)(initSortKey);
    const [direction, setDirection] = (0, _react.useState)(initDirection);
    const onSort = (value)=>{
        if (value === sortKey) {
            setDirection((d)=>{
                let result;
                if (d === _types.SortDirection.NONE) {
                    result = _types.SortDirection.DESC;
                } else if (d === _types.SortDirection.DESC) {
                    result = _types.SortDirection.ASC;
                } else {
                    result = _types.SortDirection.NONE;
                }
                sessionStorage.setItem("default_sort_derection", result.toString());
                return result;
            });
        } else {
            setSortKey(value);
            setDirection(_types.SortDirection.DESC);
            sessionStorage.setItem("default_sort_key", value);
            sessionStorage.setItem("default_sort_derection", _types.SortDirection.DESC.toString());
        }
    };
    const currentValue = (0, _react.useMemo)(()=>({
            key: sortKey,
            direction: direction
        }), [
        sortKey,
        direction
    ]);
    return {
        sortKey,
        direction,
        value: currentValue,
        onSort
    };
};

//# sourceMappingURL=useSort.js.map