import { wxController } from "@/controllers/wxController";
import { Dict_Apis } from "@/dict";
import { initEditor } from "@/modules/editor";
import { defineComponent } from "vue";

export default defineComponent(() => {
  const editor = initEditor();
  const params = new URLSearchParams(location.href.split("?")[1]);
  const id = params.get("id");

  const wxSdk = new wxController();
  const url = `${Dict_Apis.promotion}/wechat/share?`;
  wxSdk.init(url);

  editor.actions.switchMode("preview");

  if (id) {
    editor.actions.initDesign(id);

    editor.actions.on("initDesign:success", () => {
      const data = editor.store.designData;
      document.title = data.title;
      const shareData = {
        title: data.title,
        link: location.href,
        imgUrl: data.thumbnail || "",
        desc: data.desc,
      };
      wxSdk.setShareData(shareData);
      wxSdk.setShare(shareData);
    });
  }

  return () => <editor.components.Preview />;
});
