var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React, { useCallback, useContext, useEffect, useState, useMemo } from 'react';
import { includes, omit, pipe, values, identity } from 'ramda';
import { useActionsHook } from '../../contexts/ActionsHookContext';
import { useMdmListener } from '../../contexts/MdmModuleContext';
var EntityLoadingIndicationContext = React.createContext({
    isLoading: false,
    isEmptyState: false,
    updateEntityLoadingState: identity,
    updateEntitiesNavigationLoadingState: identity,
    resetLoadingState: identity
});
var defaultLoadingState = { entitiesNavigation: false, entity: false, entityCleansing: false };
var useEntityLoadingIndicationHook = function () {
    var _a = useState(defaultLoadingState), loadingState = _a[0], setLoadingState = _a[1];
    var updateEntityLoadingState = useCallback(function (loading) {
        setLoadingState(function (prevLoadingState) { return (__assign(__assign({}, prevLoadingState), { entity: loading })); });
    }, []);
    var updateEntitiesNavigationLoadingState = useCallback(function (loading) {
        setLoadingState(function (prevLoadingState) { return (__assign(__assign({}, prevLoadingState), { entitiesNavigation: loading })); });
    }, []);
    var updateEntityCleansingLoadingState = useCallback(function (loading) {
        setLoadingState(function (prevLoadingState) { return (__assign(__assign({}, prevLoadingState), { entityCleansing: loading })); });
    }, []);
    var resetLoadingState = useCallback(function () {
        setLoadingState(defaultLoadingState);
    }, []);
    var listenToActions = useActionsHook();
    var cleanseLoadingListener = useMdmListener('cleanseLoadingListener');
    var listenerCallback = useMemo(function () { return cleanseLoadingListener(updateEntityCleansingLoadingState); }, [cleanseLoadingListener, updateEntityCleansingLoadingState]);
    useEffect(function () {
        var stopListeningActions = listenToActions(listenerCallback);
        return function () { return stopListeningActions(); };
    }, [listenerCallback, listenToActions]);
    var isLoading = Object.values(loadingState).includes(true);
    var isEmptyState = pipe(omit(['entityCleansing']), values, includes(true))(loadingState);
    return {
        isLoading: isLoading,
        isEmptyState: isEmptyState,
        updateEntityLoadingState: updateEntityLoadingState,
        updateEntitiesNavigationLoadingState: updateEntitiesNavigationLoadingState,
        updateEntityCleansingLoadingState: updateEntityCleansingLoadingState,
        resetLoadingState: resetLoadingState
    };
};
export var EntityLoadingIndicationProvider = function (_a) {
    var children = _a.children;
    var value = useEntityLoadingIndicationHook();
    return React.createElement(EntityLoadingIndicationContext.Provider, { value: value }, children);
};
export var useEntityLoadingIndication = function () {
    return useContext(EntityLoadingIndicationContext);
};
