import { __assign, __spreadArray } from "tslib";
import React, { useReducer, useContext } from "react";
import { Common, Props } from "v-eris";
import "./loader.scss";
;
var LoaderModuleReducer = function (state, _a) {
    var type = _a[0], data = _a[1];
    if (type == "start") {
        var list = __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 __assign(__assign({}, state), { list: list, active: !!list.length });
    }
    ;
    if (type == "success") {
        var list = __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 __assign(__assign({}, state), { list: list, active: !!list.length });
    }
    ;
    if (type == "failed") {
        var list = __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 __assign(__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) { },
};
export var LoaderContext = React.createContext(LoaderContextDefault);
export var LoaderModuleInit = function () {
    var _a = 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: LoaderContext };
};
export var Loader = function (props) {
    var module = useContext(LoaderContext);
    var active = props.active === undefined ? module.active : props.active;
    var size = props.size ? Common.uint(props.size) : 176;
    return (React.createElement("div", { className: Props.className("eris-loader", { active: active, container: props.container }), style: { transform: active ? "scale(calc(" + size + " / 248))" : "scale( 0.0 )" } },
        React.createElement("div", null,
            React.createElement("div", { className: "eris-loader-1" }),
            React.createElement("div", { className: "eris-loader-2" }),
            React.createElement("div", { className: "eris-loader-3" }),
            React.createElement("div", { className: "eris-loader-4" }),
            React.createElement("div", { className: "eris-loader-5" }))));
};
//# sourceMappingURL=loader.js.map