"use strict";
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useView = void 0;
var react_1 = require("react");
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var use_deep_compare_effect_1 = __importDefault(require("use-deep-compare-effect"));
var use_immer_1 = require("use-immer");
var useListener_1 = require("../useListener");
function useView(_a) {
    var _b = _a.resolves, resolvesOriginal = _b === void 0 ? {} : _b, _c = _a.firstLoad, firstLoad = _c === void 0 ? true : _c;
    var _d = react_1.useState(resolvesOriginal), resolves = _d[0], setResolves = _d[1];
    use_deep_compare_effect_1.default(function () {
        setResolves(resolvesOriginal);
    }, [resolvesOriginal]);
    var _e = use_immer_1.useImmerReducer(function (prevState, newState) { return (__assign(__assign({}, prevState), newState)); }, {
        starting: true,
        errorLoadData: false,
        criticalError: false,
    }), statusInfo = _e[0], setStatusInfo = _e[1];
    var getParamsResolve = react_1.useRef({});
    var registerResolveParams = function registerResolveParams(resolveName, fn) {
        getParamsResolve.current[resolveName] = fn;
    };
    var _f = useListener_1.useListener(), registerOnInit = _f[0], callOnInit = _f[1];
    var _g = useListener_1.useListener(), registerOnInitError = _g[0], callOnInitError = _g[1];
    var $destroy = react_1.useRef(new rxjs_1.Subject()).current;
    function initialize() { }
    function loadResolves() {
        new rxjs_1.Observable(function (observer) {
            try {
                var params = {};
                if (Object.values(resolves).length === 0 || !firstLoad) {
                    observer.next({});
                    observer.unsubscribe();
                    return;
                }
                var keys_1 = Object.keys(resolves);
                rxjs_1.forkJoin(Object.values(resolves).map(function (f, i) {
                    return f(getParamsResolve.current[keys_1[i]] ? getParamsResolve.current[keys_1[i]]() : {}).pipe(operators_1.takeUntil($destroy));
                }))
                    .pipe(operators_1.takeUntil($destroy))
                    .subscribe(function (results) {
                    var keysI = Object.keys(resolves);
                    observer.next(Object.fromEntries(results.map(function (e, i) { return [keysI[i], results[i]]; })));
                    observer.unsubscribe();
                }, function (err) {
                    observer.error(err);
                    observer.unsubscribe();
                });
            }
            catch (e) {
                setStatusInfo({
                    starting: false,
                    criticalError: true,
                });
            }
        }).subscribe(function (r) {
            setStatusInfo({
                starting: false,
                errorLoadData: false,
            });
            callOnInit(r);
        }, function (err) {
            setStatusInfo({
                starting: false,
                errorLoadData: true,
            });
            callOnInitError(err);
        });
    }
    function reloadPage(wait1s) {
        if (wait1s === void 0) { wait1s = true; }
        setStatusInfo({
            starting: true,
            errorLoadData: false,
            criticalError: false,
        });
        if (wait1s) {
            setTimeout(function () { return loadResolves(); }, 1000);
        }
        else {
            loadResolves();
        }
    }
    react_1.useEffect(function () {
        initialize();
        loadResolves();
        return function () {
            $destroy.next({});
        };
    }, []);
    return __assign(__assign({}, statusInfo), { statusInfo: statusInfo,
        setStatusInfo: setStatusInfo,
        registerOnInit: registerOnInit,
        registerOnInitError: registerOnInitError,
        getParamsResolve: getParamsResolve,
        registerResolveParams: registerResolveParams,
        $destroy: $destroy,
        reloadPage: reloadPage });
}
exports.useView = useView;
//# sourceMappingURL=useView.js.map