import {hideMessage, showMessage} from "../dialog/message";
import {exportLayout} from "../layout/util";
import {isMobile} from "./functions";

export const processMessage = (response: IWebSocketData) => {
    if ("msg" === response.cmd) {
        showMessage(response.msg, response.data.closeTimeout, response.code === 0 ? "info" : "error");
        return false;
    }
    if ("cmsg" === response.cmd) {
        hideMessage();
        const progressElement = document.getElementById("progress");
        if (progressElement) {
            progressElement.remove();
        }
        return false;
    }
    if ("reloadui" === response.cmd) {
        if (isMobile()) {
            window.location.reload();
        } else {
            exportLayout(true);
        }
        return false;
    }

    // 小于 0 为提示：-2 提示；-1 报错，大于 0 的错误需处理，等于 0 的为正常操作
    if (response.code < 0) {
        showMessage(response.msg, response.data ? (response.data.closeTimeout || 0) : 0, response.code === -1 ? "error" : "info");
        return false;
    }

    return response;
};
