import { __awaiter, __generator } from "tslib";
import { useState } from 'react';
import { useEffectAsync } from '../../react-hooks';
import { getArePluginsRegistered } from '../../register-plugin';
import { getConfig } from '../../services/config';
var wait = function (duration) {
    if (duration === void 0) { duration = 100; }
    return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
        return [2 /*return*/, new Promise(function (resolve) { return setTimeout(resolve, duration); })];
    }); });
};
export function useConfig() {
    var _this = this;
    var _a = useState(undefined), config = _a[0], setConfig = _a[1];
    useEffectAsync(function () { return __awaiter(_this, void 0, void 0, function () {
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = setConfig;
                    return [4 /*yield*/, getConfig()];
                case 1:
                    _a.apply(void 0, [_b.sent()]);
                    return [2 /*return*/];
            }
        });
    }); }, []);
    return config;
}
export function usePluginsRegistered() {
    var _this = this;
    var _a = useState(false), arePluginsRegistered = _a[0], setArePluginsRegistered = _a[1];
    useEffectAsync(function () { return __awaiter(_this, void 0, void 0, function () {
        var areAvailable;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, getArePluginsRegistered()];
                case 1:
                    areAvailable = _a.sent();
                    // FIXME: make sure all plugins are registered and remove wait
                    return [4 /*yield*/, wait(100)];
                case 2:
                    // FIXME: make sure all plugins are registered and remove wait
                    _a.sent();
                    setArePluginsRegistered(areAvailable);
                    return [2 /*return*/];
            }
        });
    }); }, []);
    return arePluginsRegistered;
}
export function useLoading(_a) {
    var _this = this;
    var config = _a.config, arePluginsRegistered = _a.arePluginsRegistered;
    var _b = useState(true), isLoading = _b[0], setIsLoading = _b[1];
    useEffectAsync(function () { return __awaiter(_this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            if (Boolean(!config || !arePluginsRegistered)) {
                return [2 /*return*/];
            }
            setIsLoading(false);
            return [2 /*return*/];
        });
    }); }, [config, arePluginsRegistered]);
    return isLoading;
}
//# sourceMappingURL=hooks.js.map