"use strict";
const common_vendor = require("../common/vendor.js");
const TUIKit_utils_typeCheck = require("./utils/type-check.js");
const TUIKit_utils_env = require("./utils/env.js");
const TUIKit_plugins_extensionServer_callkit = require("./plugins/extension-server/callkit.js");
class TUIChatKit {
  constructor() {
    this.TUIComponents = {};
    this.TUIPlugins = {};
    this.TUICore = common_vendor.R;
    this.TUIChatEngine = common_vendor.qt;
    this.TUIGlobal = common_vendor.i;
    this.SDKAppID = 0;
    this.TUIGlobal._isTIMCallKit = true;
    common_vendor.R.registerEvent(common_vendor.E.TUILogin.EVENT.LOGIN_STATE_CHANGED, common_vendor.E.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS, this);
    common_vendor.R.registerEvent(common_vendor.E.TUITranslate.EVENT.LANGUAGE_CHANGED, common_vendor.E.TUITranslate.EVENT_SUB_KEY.CHANGE_SUCCESS, this);
    common_vendor.R.registerService(common_vendor.E.TUITranslatePlugin.SERVICE.NAME, 1);
    common_vendor.R.registerService(common_vendor.E.TUIVoiceToTextPlugin.SERVICE.NAME, 1);
  }
  /**
   * Listen for the success notification of TUILogin.login
   */
  onNotifyEvent(eventName, subKey, params) {
    if (eventName === common_vendor.E.TUILogin.EVENT.LOGIN_STATE_CHANGED) {
      switch (subKey) {
        case common_vendor.E.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS:
          this.login();
          break;
      }
    }
    if (eventName === common_vendor.E.TUITranslate.EVENT.LANGUAGE_CHANGED) {
      switch (subKey) {
        case common_vendor.E.TUITranslate.EVENT_SUB_KEY.CHANGE_SUCCESS:
          if (params == null ? void 0 : params.language) {
            common_vendor.Wt.changeLanguage(params.language);
          }
          break;
      }
    }
  }
  /**
   * init
   */
  init() {
    if (TUIChatKit.isInitialized) {
      return;
    }
    TUIChatKit.isInitialized = true;
    if (TUIKit_utils_env.isApp) {
      new TUIKit_plugins_extensionServer_callkit.CallkitPluginServer();
    }
    common_vendor.i.TUIComponents = this.TUIComponents;
    common_vendor.i.TUIPlugins = this.TUIPlugins;
    common_vendor.index.__f__("warn", "at TUIKit/server.ts:79", "[TUIChatKit]: init success.");
  }
  /**
   * login
   */
  login() {
    const { chat, SDKAppID, userID, userSig } = common_vendor.A.getContext();
    this.SDKAppID = SDKAppID;
    this.TUIChatEngine.login({
      chat,
      SDKAppID,
      userID,
      userSig
    });
    this.chat = chat;
    return this.TUIChatEngine;
  }
  /**
   * Single component mounting
   *
   * @param {string} componentName
   * @param {any} component
   * @param {any=} env
   * @returns {TUICore} mounted instance
   */
  component(componentName, component, env) {
    var _a, _b;
    if ((_a = this == null ? void 0 : this.TUIComponents) == null ? void 0 : _a.componentName) {
      common_vendor.index.__f__(
        "warn",
        "at TUIKit/server.ts:108",
        `[TUIChatKit]: ${(_b = this == null ? void 0 : this.TUIComponents) == null ? void 0 : _b.componentName} component has already been applied to target TUIChatEngine.`
      );
    } else {
      this.TUIComponents[componentName] = component;
      env == null ? void 0 : env.component(componentName, component);
    }
    if (env) {
      !common_vendor.i.Vue && (common_vendor.i.Vue = env);
    }
    return this.TUIChatEngine;
  }
  /**
   * Component list mount
   *
   * @param {object} components
   * @param {any=} env: Vue2/Vue3 environment
   * @returns {TUICore} mounted instance
   */
  components(components, env) {
    var _a;
    if (!components || !TUIKit_utils_typeCheck.isObject(components)) {
      common_vendor.index.__f__("warn", "at TUIKit/server.ts:130", "[TUIChatKit]: components is empty or not object.");
    } else {
      (_a = Object == null ? void 0 : Object.keys(components)) == null ? void 0 : _a.forEach((key) => {
        this.component(key, components[key], env);
      });
    }
    return this.TUIChatEngine;
  }
  /**
   * Plugin Injection
   *
   * @param {any} TUIPlugin
   * @param {any} options
   * @returns {TUICore} mounted instance
   */
  use(TUIPluginName, TUIPlugin, options) {
    if (!this.TUICore) {
      common_vendor.index.__f__(
        "warn",
        "at TUIKit/server.ts:148",
        `[TUIChatKit]: Plugin ${this.TUIPlugins[TUIPluginName]} can't be used before init.`
      );
      return;
    }
    if (this.TUIPlugins[TUIPluginName]) {
      common_vendor.index.__f__(
        "warn",
        "at TUIKit/server.ts:154",
        `[TUIChatKit]: Plugin ${this.TUIPlugins[TUIPluginName]} has already been applied to target TUIChatEngine.`
      );
    } else if (TUIPlugin && TUIKit_utils_typeCheck.isFunction(TUIPlugin == null ? void 0 : TUIPlugin.plugin)) {
      this.TUIPlugins[TUIPluginName] = TUIPlugin;
      TUIPlugin == null ? void 0 : TUIPlugin.plugin(this, options);
    } else if (TUIKit_utils_typeCheck.isFunction(TUIPlugin)) {
      this.TUIPlugins[TUIPluginName] = TUIPlugin;
      TUIPlugin(this, options);
    } else {
      common_vendor.index.__f__(
        "warn",
        "at TUIKit/server.ts:164",
        '[TUIChatKit]: A plugin must either be a function or an object with an "plugin" function.' + this.TUIPlugins[TUIPluginName] + "does not comply with the above rules."
      );
    }
    return this.TUIChatEngine;
  }
}
exports.TUIChatKit = TUIChatKit;
//# sourceMappingURL=../../.sourcemap/mp-weixin/TUIKit/server.js.map
