import { App, Plugin } from 'vue';
import { WsPluginOptions, WsConfig } from './types';
import { initWs } from './core/instance';

const WsPlugin: Plugin = {
  install(app: App, options: WsPluginOptions = {}) {
    const mergedConfig: WsConfig = {
      url: 'wss://echo.websocket.org',
      protocols: 'chat',
      reconnectAttempts: 5,
      reconnectDelay: 3000,
      autoConnect: false,
      ...options.globalConfig
    };
    // 初始化并挂载到app
    console.log('初始化ws', mergedConfig);
    app.config.globalProperties.$ws = initWs(mergedConfig);
  }
};

export default WsPlugin;
