'use strict';

var SignalR = require('@microsoft/signalr');
var elementPlus = require('element-plus');
var request = require('./request.js');

function _interopNamespaceDefault(e) {
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var SignalR__namespace = /*#__PURE__*/_interopNamespaceDefault(SignalR);

const initSignalR = () => {
  const token = request.getToken();
  if (!token) return null;
  const connection = new SignalR__namespace.HubConnectionBuilder().configureLogging(SignalR__namespace.LogLevel.Warning).withUrl(`${window.__env__.VITE_API_URL}/hubs/onlineUser?token=${token}`, {
    transport: SignalR__namespace.HttpTransportType.WebSockets,
    skipNegotiation: true
  }).withAutomaticReconnect({
    nextRetryDelayInMilliseconds: () => {
      return 5e3;
    }
  }).build();
  connection.keepAliveIntervalInMilliseconds = 15 * 1e3;
  connection.serverTimeoutInMilliseconds = 30 * 1e3;
  connection.start().then(() => {
  }).catch((err) => {
    console.error("SignalR \u8FDE\u63A5\u5931\u8D25:", err);
  });
  connection.onclose(async () => {
  });
  connection.onreconnecting(() => {
    elementPlus.ElNotification({
      title: "\u63D0\u793A",
      message: "\u8FDE\u63A5\u5DF2\u65AD\u5F00  >>>>>",
      type: "error",
      position: "bottom-right"
    });
  });
  connection.onreconnected(() => {
    elementPlus.ElNotification({
      title: "\u63D0\u793A",
      message: "\u8FDE\u63A5\u5DF2\u6062\u590D  >>>>>",
      type: "success",
      position: "bottom-right"
    });
  });
  connection.on("OnlineUserList", () => {
  });
  connection.on("ReceiveMessage", (message) => {
    var tmpMsg = `<div style="white-space: pre-wrap;">${message.message}<div><br/>`;
    tmpMsg += `<p style="color:#808080; font-size:10px;float:right"> ${message.sendUserName}  ${message.sendTime}<p>`;
    elementPlus.ElNotification({
      title: `${message.title}`,
      message: tmpMsg,
      type: message.messageType.toString().toLowerCase(),
      position: "top-right",
      dangerouslyUseHTMLString: true,
      duration: 5e3
    });
  });
  return connection;
};
const getSignalR = () => {
  return window.__SIGNALR_INSTANCE__;
};
if (!window.__SIGNALR_INSTANCE__) {
  const instance = initSignalR();
  if (instance) {
    window.__SIGNALR_INSTANCE__ = instance;
  }
}
if (!window.__SIGNALR_INSTANCE__) {
  const instance = initSignalR();
  if (instance) {
    window.__SIGNALR_INSTANCE__ = instance;
  } else {
  }
}
const signalR = {
  on: (event, callback) => {
    const instance = getSignalR();
    if (instance) {
      instance.on(event, callback);
    } else {
      console.warn(`SignalR \u672A\u8FDE\u63A5\uFF0C\u65E0\u6CD5\u76D1\u542C\u4E8B\u4EF6: ${event}`);
    }
  },
  off: (event, callback) => {
    const instance = getSignalR();
    if (instance) {
      instance.off(event, callback);
    }
  },
  send: (method, ...args) => {
    const instance = getSignalR();
    if (instance) {
      return instance.invoke(method, ...args);
    } else {
      console.warn("SignalR \u672A\u8FDE\u63A5\uFF0C\u65E0\u6CD5\u53D1\u9001\u6D88\u606F");
      return Promise.reject("SignalR \u672A\u8FDE\u63A5");
    }
  },
  stop: () => {
    const instance = getSignalR();
    if (instance) {
      return instance.stop();
    }
    return Promise.resolve();
  }
};
const restartSignalR = async () => {
  const instance = getSignalR();
  if (instance) {
    await instance.stop();
  }
  const newInstance = initSignalR();
  if (newInstance) {
    window.__SIGNALR_INSTANCE__ = newInstance;
  }
};

exports.restartSignalR = restartSignalR;
exports.signalR = signalR;
