"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Notifications = exports.NotificationsModuleInit = exports.NotificationsContext = exports.NotificationsContextDefault = void 0;
exports.NotificationsReducer = NotificationsReducer;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
require("./notifications.scss");
;
function NotificationsReducer(state, _a) {
    var type = _a[0], data = _a[1], lang = _a[2];
    if (type == "alert") {
        var appended = [];
        var index = state.index;
        if (!Array.isArray(data))
            data = [data];
        for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
            var line = data_1[_i];
            if (!line)
                continue;
            if (Array.isArray(line.tokens)) {
                for (var _b = 0, _c = line.tokens; _b < _c.length; _b++) {
                    var item = _c[_b];
                    appended.push({
                        token: v_eris_1.Common.token(),
                        type: "alert",
                        code: line.code || 406,
                        desc: lang.get(item),
                        readed: false,
                        index: index++
                    });
                }
                ;
            }
            else {
                appended.push({
                    token: v_eris_1.Common.token(),
                    type: "alert",
                    code: line.code || 406,
                    desc: line.desc || line.description || "",
                    readed: false,
                    index: index++
                });
            }
            ;
        }
        ;
        var list = tslib_1.__spreadArray(tslib_1.__spreadArray([], (appended.reverse()), true), state.list, true);
        list = list.splice(0, 5);
        return tslib_1.__assign(tslib_1.__assign({}, state), { index: index, list: list });
    }
    else if (type == "read") {
        var list = tslib_1.__spreadArray([], state.list, true);
        var index = list.findIndex(function (f) { return f.token == data; });
        if (index > -1)
            list[index].readed = true;
        return tslib_1.__assign(tslib_1.__assign({}, state), { list: list });
    }
    else if (type == "emptyReaded") {
        var list = [];
        for (var _d = 0, _e = state.list; _d < _e.length; _d++) {
            var item = _e[_d];
            if (item.readed)
                continue;
            list.push(item);
        }
        ;
        return tslib_1.__assign(tslib_1.__assign({}, state), { list: list });
    }
    ;
    return state;
}
;
exports.NotificationsContextDefault = {
    state: {},
    list: [],
    dispatch: function () { },
    alert: function (data) { },
    read: function (token) { },
    emptyReaded: function () { },
    index: 0,
};
exports.NotificationsContext = react_1.default.createContext(exports.NotificationsContextDefault);
var NotificationsModuleInit = function () {
    var _a = (0, react_1.useReducer)(NotificationsReducer, {
        index: 1,
        list: [],
    }), state = _a[0], dispatch = _a[1];
    var lang = (0, react_1.useContext)(v_eris_1.LangContext);
    var alert = function (data) {
        dispatch(["alert", data, lang]);
    };
    var read = function (token) {
        dispatch(["read", token, lang]);
    };
    var emptyReaded = function (token) {
        dispatch(["emptyReaded", lang]);
    };
    return { state: state, dispatch: dispatch, list: state.list, index: state.index, alert: alert, read: read, emptyReaded: emptyReaded, context: exports.NotificationsContext };
};
exports.NotificationsModuleInit = NotificationsModuleInit;
var Notifications = function (props) {
    var lang = (0, react_1.useContext)(v_eris_1.LangContext);
    var module = (0, react_1.useContext)(exports.NotificationsContext);
    var _a = (0, react_1.useState)(false), hovered = _a[0], setHovered = _a[1];
    var childrenElem = v_eris_1.useAnimation.Expand(hovered);
    var asyncSaveRef = (0, react_1.useRef)(module.index);
    var asyncSaveRefTimer = (0, react_1.useRef)(undefined);
    (0, react_1.useEffect)(function () {
        if (asyncSaveRef.current == module.index)
            return;
        setHovered(true);
        clearTimeout(asyncSaveRefTimer.current);
        asyncSaveRefTimer.current = setTimeout(function () {
            asyncSaveRef.current = module.index;
            setHovered(false);
        }, 3500);
    }, [module.index]);
    var freshNotices = (0, react_1.useMemo)(function () {
        var result = 0;
        for (var _i = 0, _a = module.list; _i < _a.length; _i++) {
            var item = _a[_i];
            if (item.readed)
                continue;
            result++;
        }
        ;
        return result;
    }, [module.list]);
    var content = (0, react_1.useMemo)(function () {
        return (react_1.default.createElement("div", { className: v_eris_1.Props.className("eris-notifications", { hovered: hovered, hidden: !module.list.length }), onMouseOver: function (e) {
                clearTimeout(asyncSaveRefTimer.current);
                setHovered(true);
            }, onMouseOut: function (e) {
                clearTimeout(asyncSaveRefTimer.current);
                setHovered(false);
            } },
            react_1.default.createElement("div", { className: "eris-notifications-frame", ref: childrenElem }, module.list.map(function (item) {
                return (react_1.default.createElement("div", { className: v_eris_1.Props.className("eris-notifications-line", { alert: !item.readed }), key: item.token, onMouseOver: function (e) { module.read(item.token); } },
                    react_1.default.createElement("div", { className: "eris-notifications-line-index" }, item.index),
                    react_1.default.createElement("div", { className: "eris-notifications-line-desc" }, item.desc),
                    react_1.default.createElement("div", { className: "eris-notifications-line-code" },
                        react_1.default.createElement("span", null, "code"),
                        react_1.default.createElement("span", null, item.code))));
            })),
            react_1.default.createElement("div", { className: "eris-notifications-button" },
                react_1.default.createElement(v_eris_1.Icons.notification, null),
                react_1.default.createElement(v_eris_1.Space, null),
                lang.get("Core::Notifications"),
                react_1.default.createElement(v_eris_1.Space, null),
                react_1.default.createElement("span", { className: v_eris_1.Props.className("eris-notifications-length", { alert: freshNotices > 0 }) }, "( " + (freshNotices) + " )"))));
    }, [module.list, hovered]);
    return (props.container ? (react_1.default.createElement("div", { className: "eris-notifications-container" }, content)) : (content));
};
exports.Notifications = Notifications;
//# sourceMappingURL=notifications.js.map