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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { ajax, BizGlobalDataContext, SocketClient } from '@zgfe/business-lib';
import { notification, Skeleton } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import apis from "../apis";
import "./index.less";
export var LinkQrCode = function LinkQrCode(_ref) {
  var token = _ref.token;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    url = _useState2[0],
    setUrl = _useState2[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp;
  function getCode() {
    ajax(apis.getQrCode, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        socketToken: token,
        origin: 2
      }
    }).then(function (res) {
      if (!res) return;
      setUrl(res.data.base64Str);
    }).catch(function (err) {
      console.error('获取二维码失败', err);
    });
  }
  useEffect(function () {
    if (!token) return;
    getCode();
  }, [token]);
  if (!url || !token) {
    return /*#__PURE__*/React.createElement(Skeleton.Image, {
      style: {
        width: '176px',
        height: '176px'
      },
      active: true
    });
  }
  return /*#__PURE__*/React.createElement("img", {
    src: "data:image/png;base64,".concat(url),
    width: "160",
    height: "160"
  });
};
var VisualScanCode = function VisualScanCode(_ref2) {
  var socketUrl = _ref2.socketUrl,
    onScan = _ref2.onScan;
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    qrToken = _useState4[0],
    setQrToken = _useState4[1];
  var _useContext2 = useContext(BizGlobalDataContext),
    currentApp = _useContext2.currentApp;
  var _useState5 = useState(),
    _useState6 = _slicedToArray(_useState5, 2),
    client = _useState6[0],
    setClient = _useState6[1];
  var classPrefix = 'visual-scan';
  function afterScan(res) {
    // 109007 为扫码成功
    if (Number(res === null || res === void 0 ? void 0 : res.code) !== 109007) return;
    setClient(function (socket) {
      socket === null || socket === void 0 ? void 0 : socket.close();
      return undefined;
    });
    if (onScan) onScan();
  }
  function onExist() {
    notification.error({
      message: '已有其他客户端在对当前应用进行可视化埋点,请关闭后重试'
    });
  }
  function initSocket() {
    var socket = new SocketClient("".concat(socketUrl || location.origin.replace('http', 'ws'), "/eventtracking/ws"), currentApp === null || currentApp === void 0 ? void 0 : currentApp.appKey, {
      appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
      origin: 3
    });
    socket.openLog(true);
    setClient(socket);
    socket.on('unknownCode', afterScan);
    socket.on('exist', onExist);
    socket.onConnected(function (token) {
      setQrToken(token);
    });
  }
  useEffect(function () {
    return function () {
      client === null || client === void 0 ? void 0 : client.close();
    };
  }, [client]);
  useEffect(function () {
    initSocket();
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u624B\u673A\u626B\u4E00\u626B\u5FEB\u901F\u8FDE\u63A5"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tocode")
  }, /*#__PURE__*/React.createElement(LinkQrCode, {
    token: qrToken
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip-title")
  }, "\u64CD\u4F5C\u6B65\u9AA4"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip-line")
  }, "1\u3001\u96C6\u6210SDK\u540E\u6253\u5F00\u5168\u57CB\u70B9\u5F00\u5173\uFF0C\u5373\u53EF\u91C7\u96C6\u9875\u9762\u8BBF\u95EE\u3001\u9875\u9762\u70B9\u51FB\uFF1B"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip-line")
  }, "2\u3001\u5B89\u88C5\u8981\u5206\u6790\u7684\u5E94\u7528\uFF0C\u624B\u673A\u626B\u4E00\u626B\u8FDE\u63A5\u5E94\u7528\uFF0C\u8FDE\u63A5\u6210\u529F\u540E\u53EF\u8FDB\u884C\u53EF\u89C6\u5316\u57CB\u70B9\uFF1B")));
};
export default VisualScanCode;