import { FmodeParse, WxworkCorp, WxworkSDK } from "fmode-ng/core"

const Parse = FmodeParse.with("nova")
export async function getGroupChat(gid:string){
    let query = new Parse.Query("GroupChat")
    let group = await query.get(gid);

    let cid = group?.get("company")?.id || localStorage.getItem("company")
    let wwcorp = new WxworkCorp(cid);
    let needSave = false;
    // group = await wwsdk.syncGroupChat(group.toJSON());
    if(!group?.get("joinUrl")){
        needSave = true;
        let config_id1 = (await wwcorp.externalContact.groupChat.addJoinWay({
          scene:1,
          chat_id_list:[group.get("chat_id")],
        }))?.config_id
        let joinUrl = (await wwcorp.externalContact.groupChat.getJoinWay(config_id1))?.join_way
        group.set("joinUrl",joinUrl)
        needSave = true
      }
      if(!group?.get("joinQrcode")){
        needSave = true;
        let config_id2 = (await wwcorp.externalContact.groupChat.addJoinWay({
          scene:2,
          chat_id_list:[group.get("chat_id")],
        }))?.config_id
        let joinQrcode = (await wwcorp.externalContact.groupChat.getJoinWay(config_id2))?.join_way
        group.set("joinQrcode",joinQrcode)
        needSave = true
      }
    if(needSave){
        group.save();
    }
    console.log("ggg",group)
    return group
}