import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect } from "react";
import { define as schema_define, observable } from "@tachybase/schema";
import { Modal, message, notification } from "antd";
import mitt from "mitt";
import { useApp } 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_NoticeLevel = /*#__PURE__*/ function(NoticeLevel) {
    NoticeLevel["INFO"] = "info";
    NoticeLevel["WARNING"] = "warning";
    NoticeLevel["SUCCESS"] = "success";
    NoticeLevel["ERROR"] = "error";
    return NoticeLevel;
}({});
var NoticesManager_NoticeType = /*#__PURE__*/ function(NoticeType) {
    NoticeType["STATUS"] = "status";
    NoticeType["TOAST"] = "toast";
    NoticeType["NOTIFICATION"] = "notification";
    NoticeType["CUSTOM"] = "custom";
    NoticeType["MODAL"] = "modal";
    return NoticeType;
}({});
var NoticesManager_NoticeDuration = /*#__PURE__*/ function(NoticeDuration) {
    NoticeDuration["Short"] = "SHORT";
    return NoticeDuration;
}({});
const NoticeManagerContext = /*#__PURE__*/ createContext(null);
const NoticeManagerProvider = (param)=>{
    let { children } = param;
    const app = useApp();
    return /*#__PURE__*/ jsx(NoticeManagerContext.Provider, {
        value: {
            manager: app.noticeManager
        },
        children: children
    });
};
const useNoticeManager = ()=>useContext(NoticeManagerContext);
const useNoticeSub = (name, handler)=>{
    const { manager } = useNoticeManager();
    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.notification(data.title, data.content, data.level, data.duration);
        else if ("modal" === data.type) this.modal(data.title, data.content, data.level, data.duration);
        else if ("toast" === data.type) this.toast(data.content, data.level, data.duration);
        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) {
        let duration = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 3;
        message[level]({
            content,
            duration
        });
    }
    notification(title, content, level) {
        let duration = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : null, placement = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : 'topRight';
        notification[level]({
            message: title,
            description: content,
            placement,
            duration
        });
    }
    modal(title, content, level) {
        let duration = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : null, options = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : {};
        const modal = Modal[level]({
            title,
            content,
            destroyOnClose: true,
            maskClosable: false,
            ...options
        });
        if (!duration) duration = 30;
        setTimeout(()=>{
            null == modal || modal.destroy();
        }, 1000 * duration);
    }
    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 = mitt();
        schema_define(this, {
            currentStatus: observable.ref
        });
        setInterval(()=>{
            const now = Date.now();
            if (now - this.currentStatusUpdatedAt > 30000) this.currentStatus = '';
        }, 1000);
    }
}
export { NoticesManager_NoticeDuration as NoticeDuration, NoticesManager_NoticeLevel as NoticeLevel, NoticeManager, NoticeManagerContext, NoticeManagerProvider, NoticesManager_NoticeType as NoticeType, useNoticeManager, useNoticeSub };
