import { initEditor } from "@/modules/editor";
import { isPc } from "@queenjs/utils";
import { defineComponent } from "vue";

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

  editor.actions.switchMode("preview");

  if (id) {
    editor.actions.initDesign(id);
    editor.controls.wxCtrl.setup(window.location.href);

    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,
      };
      editor.controls.wxCtrl.setShareData(shareData);
      editor.controls.wxCtrl.setShare(shareData);
    });
  }
  // fetch("https://restapi.amap.com/v3/ip?key=6f53b2e09f72ad63423b2da6e08b25d7").then(response=>{
  //    return response.json();
  // }).then(data=>{
  //   console.log(data);
  // })

  return () => (
    <div class="flex items-center justify-center h-100vh bg-gray-100">
      <div class={isPc() ? `h-668px scrollbar` : `h-full`}>
        <editor.components.Preview />
      </div>
    </div>
  );
});
