"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDefaultConfiguration = exports.ClusterDefaultConfigurationProvider = exports.ClusterDefaultConfigurationContext = void 0;
const tslib_1 = require("tslib");
const pick_js_1 = tslib_1.__importDefault(require("lodash-es/pick.js"));
const react_1 = tslib_1.__importStar(require("react"));
const apis_1 = require("../../services/apis");
const reducer = (state, action) => {
    let newState;
    switch (action.type) {
        case 'request':
            newState = { status: 'loading', data: {} };
            break;
        case 'success':
            newState = { status: 'succeeded', data: action.data };
            break;
        case 'failure':
            newState = { status: 'failed', data: action.data };
            break;
        default:
            newState = state;
            break;
    }
    return newState;
};
exports.ClusterDefaultConfigurationContext = react_1.default.createContext({});
const ClusterDefaultConfigurationProvider = ({ children, errorUI, loadingUI, }) => {
    const [state, dispatch] = react_1.default.useReducer(reducer, { status: 'idle', data: {} });
    (0, react_1.useEffect)(() => {
        let mounted = true;
        const fetchAndSetDefaultConfiguration = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            try {
                if (mounted)
                    dispatch({ type: 'request' });
                const { data } = yield apis_1.ClustersAPI.getDefaultConfig();
                if (mounted)
                    dispatch({ type: 'success', data });
            }
            catch (_a) {
                if (mounted)
                    dispatch({ type: 'failure', data: {} });
            }
        });
        void fetchAndSetDefaultConfiguration();
        return () => {
            mounted = false;
        };
    }, []);
    const render = (state) => {
        let result = { ui: loadingUI, value: state.data };
        switch (state.status) {
            case 'idle':
            case 'loading':
                break;
            case 'failed':
                result = Object.assign(Object.assign({}, result), { ui: errorUI });
                break;
            case 'succeeded':
                result = Object.assign(Object.assign({}, result), { ui: children });
                break;
        }
        return result;
    };
    const { ui, value } = render(state);
    return (react_1.default.createElement(exports.ClusterDefaultConfigurationContext.Provider, { value: value }, ui));
};
exports.ClusterDefaultConfigurationProvider = ClusterDefaultConfigurationProvider;
const useDefaultConfiguration = (keys) => {
    // TODO(mlibra): There is no more the need to pick just selected values here, we can pass all the retrieved data
    return (0, pick_js_1.default)((0, react_1.useContext)(exports.ClusterDefaultConfigurationContext), keys);
};
exports.useDefaultConfiguration = useDefaultConfiguration;
//# sourceMappingURL=ClusterDefaultConfigurationContext.js.map