function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useEffect, useState, useRef, useCallback } from 'react';
import * as ReactDOMClient from 'react-dom/client';
import * as ReactDOM from 'react-dom';

// 将 React 暴露到全局作用域，供第三方 SDK 使用

if (!React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) {
  React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = {
    ReactCurrentOwner: {
      current: null
    }
  };
}

// 只有在全局 React 不存在时才设置
if (typeof window !== 'undefined') {
  if (!window.React) {
    window.React = React;
  }
  if (!window.ReactDOM) {
    // 合并 ReactDOM 和 ReactDOMClient 的功能，确保新的 API 来自正确的模块
    window.ReactDOM = _objectSpread(_objectSpread({}, ReactDOM), {}, {
      // 明确从 react-dom/client 导入新的 API
      createRoot: ReactDOMClient.createRoot,
      hydrateRoot: ReactDOMClient.hydrateRoot
    });
  }
}

// 扩展 Window 接口以支持动态属性访问

/**
 * 自定义 useInterval hook，用于轮询检查远程组件是否加载完成
 * @param callback 回调函数
 * @param delay 轮询间隔时间（毫秒）
 * @returns 清除定时器的函数
 */
function useInterval(callback, delay) {
  var intervalRef = useRef(null);
  var savedCallback = useRef();

  // 保存最新的回调函数
  useEffect(function () {
    savedCallback.current = callback;
  }, [callback]);

  // 清除定时器的函数，使用 useCallback 优化性能
  var clear = useCallback(function () {
    if (intervalRef.current !== null) {
      clearInterval(intervalRef.current);
      intervalRef.current = null;
    }
  }, []);

  // 设置和清理定时器
  useEffect(function () {
    var tick = function tick() {
      if (savedCallback.current) {
        savedCallback.current();
      }
    };
    intervalRef.current = setInterval(tick, delay);
    return clear;
  }, [delay, clear]);
  return clear;
}
function load(url, name) {
  var id = "__umd_component__".concat(name, "_");
  if (document.getElementById(id)) {
    return;
  }
  var script = document.createElement('script');
  script.async = true;
  script.src = url;
  script.id = id;
  script.crossOrigin = 'anonymous';
  // attach it to DOM
  document.body.appendChild(script);
}

/**
 * a hook to use Remote React Component.
 * @param url component CDN url
 * @param name global name
 * @return global variable
 */
export function useRemoteComponent(url, name) {
  var _window;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    Component = _useState2[0],
    setComponent = _useState2[1];
  var _useState3 = useState(true),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  useEffect(function () {
    load(url, name);
  }, [url, name]);
  var clear = useInterval(function () {
    if (window[name]) {
      setComponent(window[name]);
      setLoading(false);
      clear();
    }
  }, 100);
  if ((_window = window) !== null && _window !== void 0 && _window[name]) {
    return [window[name], false];
  }
  return [Component, loading];
}
function getJSSDKURL(options) {
  var packageName = options.packageName,
    version = options.version,
    _options$file = options.file,
    file = _options$file === void 0 ? 'dist/sdk.js' : _options$file;
  var jssdkVersion = version;
  var jssdkCdnUrl = jssdkVersion.includes('dev') ? "https://g.alipay.com/".concat(packageName, "@").concat(jssdkVersion, "/").concat(file) : "https://gw.alipayobjects.com/render/p/yuyan_npm/".concat(packageName.replace('/', '_'), "/").concat(jssdkVersion, "/").concat(file);
  return jssdkCdnUrl;
}
function useRemoteSdk(options) {
  var _options$sdkInstanceN = options.sdkInstanceName,
    sdkInstanceName = _options$sdkInstanceN === void 0 ? 'sdk' : _options$sdkInstanceN;

  // dev 版本只能走 g.alipay.com 域名，外网千万不能配
  var jssdkCdnUrl = getJSSDKURL(options);
  var _useRemoteComponent = useRemoteComponent(jssdkCdnUrl, sdkInstanceName),
    _useRemoteComponent2 = _slicedToArray(_useRemoteComponent, 2),
    sdk = _useRemoteComponent2[0],
    sdkLoading = _useRemoteComponent2[1];
  return {
    sdk: sdk,
    loading: sdkLoading
  };
}
export function useVisionsnapSdk(visionsnapVersion) {
  return useRemoteSdk({
    packageName: '@alipay/visionsnap-client',
    version: visionsnapVersion,
    sdkInstanceName: 'VisionSnapSDK',
    file: 'dist/sdk.js'
  });
}
export var requestProxy = {
  getAuthToken: "https://www.weavefox.cn/api/visionsnap/auth_token",
  getImportMaps: "https://www.weavefox.cn/api/visionsnap/import_maps"
};