import { useSiteData } from 'dumi';
import React, { useMemo } from 'react';
import { useSnapshot } from 'valtio';
import { AIChatStore } from "../../../../model/AIChat";
import CodeRunner from "dumi/theme/slots/CodeRunner";
import { wrap2VisionSnap } from "./generateCode";
import { requestProxy, useVisionsnapSdk } from "../../../../hooks/useVisionsnapSdk";
import { ErrorBoundary } from 'react-error-boundary';
import Loading from "dumi/theme/slots/Loading";
import { ErrorFallback } from "../../../../builtins/Playground";
import { useAntVConfig } from "../../../../hooks/useProducts";
function TaskBox() {
  var _themeConfig$ai, _themeConfig$ai2;
  var _useAntVConfig = useAntVConfig(),
    _useAntVConfig$data = _useAntVConfig.data,
    _useAntVConfig$data2 = _useAntVConfig$data === void 0 ? {
      VisionSnapVersion: '3.5.12'
    } : _useAntVConfig$data,
    version = _useAntVConfig$data2.VisionSnapVersion;
  var snap = useSnapshot(AIChatStore);
  var _useSiteData = useSiteData(),
    themeConfig = _useSiteData.themeConfig;
  var demoId = useMemo(function () {
    return crypto.randomUUID();
  }, [snap.codeBlock]);
  var exampleTopics = useMemo(function () {
    return [{
      icon: '',
      title: {},
      id: snap.anonymousUserId,
      examples: [
      // 这是一个 Example 对象
      {
        icon: '',
        title: {},
        id: snap.activeSessionId,
        api: 'https://example.com/api/pie-chart',
        // 必须的 api 属性
        demos: [
        // 这是一个 Demo 对象
        {
          id: demoId,
          screenshot: '',
          title: {},
          filename: 'index.tsx',
          // 必须的 filename 属性
          source: snap.codeBlock
        }]
      }]
    }];
  }, [demoId, snap.activeSessionId, snap.anonymousUserId, snap.codeBlock]);
  var _useVisionsnapSdk = useVisionsnapSdk(version),
    sdk = _useVisionsnapSdk.sdk,
    loading = _useVisionsnapSdk.loading;
  if (loading) {
    return /*#__PURE__*/React.createElement(Loading, null);
  }
  var wrappedVisionSnapCode = wrap2VisionSnap(snap.codeBlock);
  var handleEsmLoadFailed = function handleEsmLoadFailed(err) {
    var _err$data;
    AIChatStore.errorMsg = ((_err$data = err.data) === null || _err$data === void 0 || (_err$data = _err$data.error) === null || _err$data === void 0 || (_err$data = _err$data.split('\n')) === null || _err$data === void 0 ? void 0 : _err$data[0]) || JSON.stringify(err) || err.message;
  };
  if (themeConfig.isAntVSite || ((_themeConfig$ai = themeConfig.ai) === null || _themeConfig$ai === void 0 ? void 0 : _themeConfig$ai.codeRunner) === "VisionSnap" || !((_themeConfig$ai2 = themeConfig.ai) !== null && _themeConfig$ai2 !== void 0 && _themeConfig$ai2.codeRunner) || !wrappedVisionSnapCode.modules["/package.json"].code.includes("@antv/f2")) {
    return /*#__PURE__*/React.createElement(ErrorBoundary, {
      FallbackComponent: ErrorFallback
    }, /*#__PURE__*/React.createElement(sdk.VisionPreview, {
      id: "visionIframe",
      bizCode: "antv",
      style: {
        height: '100vh'
      },
      userId: "antv",
      displayMode: "code-and-preview",
      initialView: "preview",
      theme: "light",
      code: wrappedVisionSnapCode,
      requestProxy: requestProxy,
      isStreaming: false,
      proxyOptions: {
        isWAN: true
      },
      src: "https://www.weavefox.cn/_visionsnap_render".concat(wrappedVisionSnapCode.modules["/package.json"].code.includes("vue") ? '_vue' : '', "/index.html?version=").concat(version, "&enableInspector=1"),
      onEsmLoadFailed: handleEsmLoadFailed,
      previewZoomConfig: {
        defaultZoomMode: 100
      }
    }));
  } else {
    return /*#__PURE__*/React.createElement(CodeRunner, {
      isPlayground: true,
      showAI: false,
      showEditor: false,
      size: 0.5,
      topic: snap.anonymousUserId,
      example: snap.activeSessionId,
      demo: demoId,
      exampleTopics: exampleTopics
    });
  }
}
export default TaskBox;