import AgoraRTC, { IAgoraRTCClient, IAgoraRTCRemoteUser } from "agora-rtc-sdk-ng"
let client: IAgoraRTCClient;
type optionsType = {
  appId: string;
  channel: string;
  token: string;
  uid: number;
};
const options: optionsType = {
  appId: "",
  channel: "",
  token: "",
  uid: 0,
};
export const agora = {
  // 初始化
  init: async () => {
    client = AgoraRTC.createClient({ mode: "live", codec: "vp8" });
  },
  // 设置参数
  setOptions: <K extends keyof optionsType>(key: K, value: optionsType[K]) => {
    options[key] = value;
  },
  // 加入频道
  join: async () => {
    await client.join(
      options.appId,
      options.channel,
      options.token,
      options.uid
    );
  },
  // 离开频道
  leave: async () => {
    await client.leave();
  },
  // 监听
  on: ({ userPublished }: any) => {
    client.on("user-published", async (user: IAgoraRTCRemoteUser, mediaType: 'video' | 'audio') => {
      await client.subscribe(user, mediaType)
      await client.setRemoteDefaultVideoStreamType(1)
      userPublished(user, mediaType)
    })
  }
}