import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import Notification from "../../components/notification";
import NotificationItem from "../../components/notification/notification-item";
const REMOVE_DELAY = 500;
export var Severity;
(function (Severity) {
    Severity["Error"] = "error";
    Severity["Warning"] = "warning";
    Severity["Message"] = "success";
})(Severity || (Severity = {}));
const defaultState = {
    notifications: [],
    timeout: 15000,
};
const defaultContext = {
    ...defaultState,
    notify: () => {
        throw new Error("notify");
    },
    sendError: () => {
        throw new Error("error");
    },
    sendMessage: () => {
        throw new Error("message");
    },
    hide: () => {
        throw new Error("hide");
    },
};
const Context = React.createContext(defaultContext);
Context.displayName = "NotificationsContext";
export const useNotifications = () => React.useContext(Context);
export const NotificationsCtx = Context;
export class NotificationsProvider extends React.Component {
    state = defaultState;
    changeNumber = 0;
    latestHide = 0;
    notify = (el) => {
        const id = ++this.changeNumber;
        const severity = el.severity ?? Severity.Message;
        const notification = { ...el, id, visible: true, severity };
        !el.isInfinite &&
            setTimeout(() => {
                this.close(notification);
            }, el.timeout ? el.timeout : this.state.timeout);
        this.setState({
            notifications: [...this.state.notifications, notification],
        });
    };
    sendError = (el) => {
        const content = this.props.decode && typeof el.content === "string"
            ? this.props.decode(el.content)
            : el.content;
        this.notify({ ...el, content, severity: Severity.Error });
    };
    sendMessage = (el) => {
        this.notify({ ...el, severity: Severity.Message });
    };
    hide = (notification) => {
        const notifications = [...this.state.notifications];
        if (notification.id) {
            const res = notifications.find((el) => el.id === notification.id);
            if (res) {
                res.visible = false;
                this.latestHide = Date.now();
                this.setState({ notifications });
            }
        }
    };
    remove = (notification) => {
        const notifications = [...this.state.notifications];
        if (Date.now() - this.latestHide < REMOVE_DELAY) {
            setTimeout(() => {
                this.remove(notification);
            }, REMOVE_DELAY);
        }
        else if (notification.id) {
            const i = notifications.findIndex((el) => el.id === notification.id);
            if (i > -1) {
                notifications.splice(i, 1);
                this.setState({ notifications });
            }
        }
    };
    close = (notification) => {
        this.hide(notification);
        setTimeout(() => {
            this.remove(notification);
        }, REMOVE_DELAY);
    };
    render() {
        return (_jsxs(Context.Provider, { value: {
                ...this.state,
                notify: this.notify,
                sendError: this.sendError,
                sendMessage: this.sendMessage,
                hide: this.hide,
            }, children: [this.props.children, _jsx(Notification.Wrapper, { theme: this.props.theme, children: this.state.notifications.map((notification, i) => {
                        return (_jsx(NotificationItem, { theme: this.props.theme, onClose: () => this.remove(notification), severity: notification.severity, visible: notification.visible ? true : false, href: notification.href ? notification.href : undefined, onClick: notification.onClick, children: _jsx(Notification.Text, { theme: this.props.theme, children: React.isValidElement(notification.content)
                                    ? React.cloneElement(notification.content, {
                                        close: () => this.remove(notification),
                                    })
                                    : notification.content }) }, i));
                    }) })] }));
    }
}
//# sourceMappingURL=index.js.map