"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Loader = exports.LoaderModuleInit = exports.LoaderContext = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
require("./loader.scss");
;
var LoaderModuleReducer = function (state, _a) {
    var type = _a[0], data = _a[1];
    if (type == "start") {
        var list = tslib_1.__spreadArray([], state.list, true);
        var key_1 = data.key || ((data.module || "") + ":" + (data.type || ""));
        var index = list.findIndex(function (item) { return item.key == key_1; });
        if (index < 0)
            list.push({ key: key_1, module: data.module || "", type: data.type || "", status: "Start" });
        return tslib_1.__assign(tslib_1.__assign({}, state), { list: list, active: !!list.length });
    }
    ;
    if (type == "success") {
        var list = tslib_1.__spreadArray([], state.list, true);
        var key_2 = data.key || ((data.module || "") + ":" + (data.type || ""));
        var index = list.findIndex(function (item) { return item.key == key_2; });
        if (index < 0)
            return state;
        list.splice(index, 1);
        return tslib_1.__assign(tslib_1.__assign({}, state), { list: list, active: !!list.length });
    }
    ;
    if (type == "failed") {
        var list = tslib_1.__spreadArray([], state.list, true);
        var key_3 = data.key || ((data.module || "") + ":" + (data.type || ""));
        var index = list.findIndex(function (item) { return item.key == key_3; });
        if (index < 0)
            return state;
        list.splice(index, 1);
        return tslib_1.__assign(tslib_1.__assign({}, state), { list: list, active: !!list.length });
    }
    ;
    return state;
};
var LoaderContextDefault = {
    list: [],
    active: false,
    dispatch: function () { },
    start: function (data) { },
    success: function (data) { },
    failed: function (data) { },
};
exports.LoaderContext = react_1.default.createContext(LoaderContextDefault);
var LoaderModuleInit = function () {
    var _a = (0, react_1.useReducer)(LoaderModuleReducer, {
        list: [],
        active: false,
    }), state = _a[0], dispatch = _a[1];
    var start = function (data) {
        dispatch(["start", data]);
    };
    var failed = function (data) {
        dispatch(["failed", data]);
    };
    var success = function (data) {
        dispatch(["success", data]);
    };
    return { state: state, dispatch: dispatch, active: state.active, list: state.list, start: start, success: success, failed: failed, context: exports.LoaderContext };
};
exports.LoaderModuleInit = LoaderModuleInit;
var Loader = function (props) {
    var module = (0, react_1.useContext)(exports.LoaderContext);
    var active = props.active === undefined ? module.active : props.active;
    var size = props.size ? v_eris_1.Common.uint(props.size) : 176;
    return (react_1.default.createElement("div", { className: v_eris_1.Props.className("eris-loader", { active: active, container: props.container }), style: { transform: active ? "scale(calc(" + size + " / 248))" : "scale( 0.0 )" } },
        react_1.default.createElement("div", null,
            react_1.default.createElement("div", { className: "eris-loader-1" }),
            react_1.default.createElement("div", { className: "eris-loader-2" }),
            react_1.default.createElement("div", { className: "eris-loader-3" }),
            react_1.default.createElement("div", { className: "eris-loader-4" }),
            react_1.default.createElement("div", { className: "eris-loader-5" }))));
};
exports.Loader = Loader;
//# sourceMappingURL=loader.js.map