import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_mitt__ from "mitt";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "./hooks/index.mjs";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
var NoticesManager_rslib_entry_NoticeLevel = /*#__PURE__*/ function(NoticeLevel) {
    NoticeLevel["INFO"] = "info";
    NoticeLevel["WARNING"] = "warning";
    NoticeLevel["SUCCESS"] = "success";
    NoticeLevel["ERROR"] = "error";
    return NoticeLevel;
}({});
var NoticesManager_rslib_entry_NoticeType = /*#__PURE__*/ function(NoticeType) {
    NoticeType["STATUS"] = "status";
    NoticeType["TOAST"] = "toast";
    NoticeType["NOTIFICATION"] = "notification";
    NoticeType["CUSTOM"] = "custom";
    return NoticeType;
}({});
var NoticesManager_rslib_entry_NoticeDuration = /*#__PURE__*/ function(NoticeDuration) {
    NoticeDuration["Short"] = "SHORT";
    return NoticeDuration;
}({});
const NoticeManagerContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
const NoticeManagerProvider = (param)=>{
    let { children } = param;
    const app = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useApp)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(NoticeManagerContext.Provider, {
        value: {
            manager: app.noticeManager
        },
        children: children
    });
};
const useNoticeManager = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(NoticeManagerContext);
const useNoticeSub = (name, handler)=>{
    const { manager } = useNoticeManager();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        manager.emitter.on(name, handler);
        return ()=>{
            manager.emitter.off(name, handler);
        };
    }, [
        manager.emitter,
        name,
        handler
    ]);
};
class NoticeManager {
    on(data) {
        if ("notification" === data.type) this[data.type](data.title, data.content, data.level);
        else if ("custom" === data.type) this.emitter.emit(data.eventType, data.event);
        else this[data.type](data.content, data.level);
    }
    status(content, level, duration) {
        this.currentStatus = content;
        this.currentStatusUpdatedAt = Date.now();
    }
    toast(content, level) {
        __WEBPACK_EXTERNAL_MODULE_antd__.message[level](content);
    }
    notification(title, content, level) {
        __WEBPACK_EXTERNAL_MODULE_antd__.notification[level]({
            message: title,
            description: content
        });
    }
    constructor(app){
        _define_property(this, "app", void 0);
        _define_property(this, "ws", void 0);
        _define_property(this, "emitter", void 0);
        _define_property(this, "currentStatus", void 0);
        _define_property(this, "currentStatusUpdatedAt", void 0);
        this.app = app;
        this.currentStatus = '';
        this.currentStatusUpdatedAt = Date.now();
        this.ws = app.ws;
        this.emitter = (0, __WEBPACK_EXTERNAL_MODULE_mitt__["default"])();
        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.define)(this, {
            currentStatus: __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observable.ref
        });
        setInterval(()=>{
            const now = Date.now();
            if (now - this.currentStatusUpdatedAt > 30000) this.currentStatus = '';
        }, 1000);
    }
}
export { NoticesManager_rslib_entry_NoticeDuration as NoticeDuration, NoticesManager_rslib_entry_NoticeLevel as NoticeLevel, NoticeManager, NoticeManagerContext, NoticeManagerProvider, NoticesManager_rslib_entry_NoticeType as NoticeType, useNoticeManager, useNoticeSub };
