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 { BizDialog, IconFont } from '@zgfe/business-lib';
import { Button, Space, Switch, Tooltip } from 'antd';
import React, { useEffect, useState } from 'react';
import { LinkQrCode } from "../scanCode";
import { classPrefix } from "./types";
var Controller = function Controller(_ref) {
  var client = _ref.client,
    phoneSize = _ref.phoneSize,
    container = _ref.container,
    status = _ref.status,
    onChange = _ref.onChange,
    onChangeShowControl = _ref.onChangeShowControl;
  var _useState = useState(100),
    _useState2 = _slicedToArray(_useState, 2),
    scale = _useState2[0],
    setScale = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    fresh = _useState4[0],
    setFresh = _useState4[1];
  var _useState5 = useState(localStorage.getItem('showAppVisualControl') ? true : false),
    _useState6 = _slicedToArray(_useState5, 2),
    showControl = _useState6[0],
    setShowControl = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    haveAutoFullscreen = _useState8[0],
    sethaveAutoFullscreen = _useState8[1];
  var _useState9 = useState(false),
    _useState10 = _slicedToArray(_useState9, 2),
    showControlTip = _useState10[0],
    setShowControlTip = _useState10[1];
  var _useState11 = useState(true),
    _useState12 = _slicedToArray(_useState11, 2),
    showLinkTip = _useState12[0],
    setShowLinkTip = _useState12[1];
  var step = 10;
  var max = 120;
  var min = 30;
  function onPageData() {
    setFresh(false);
  }
  useEffect(function () {
    setShowLinkTip(true);
  }, [status]);
  useEffect(function () {
    if (onChange) onChange(scale);
  }, [scale]);
  useEffect(function () {
    if (onChangeShowControl) onChangeShowControl(showControl);
  }, [showControl]);
  useEffect(function () {
    setShowControlTip(!Boolean(localStorage.getItem('showAppVisualControlTip')));
    if (!client) return;
    client.on('pageData', onPageData);
    return function () {
      client.removeListener('pageData', onPageData);
    };
  }, []);
  function onScale(flag) {
    var val = scale + step * flag;
    if (val < min) {
      setScale(min);
      return;
    }
    if (val > max) {
      setScale(max);
      return;
    }
    setScale(val);
  }
  function onFresh() {
    if (fresh) return;
    setFresh(true);
    client === null || client === void 0 ? void 0 : client.emit('fetchPageData');
  }
  function onFull() {
    if (!container || !container.current) return;
    var containerRect = container.current.getBoundingClientRect();
    var containerStyle = getComputedStyle(container.current);
    var paddingLeft = containerStyle.paddingLeft.match(/\d*/) ? Number(containerStyle.paddingLeft.match(/\d*/)[0]) : 0;
    var paddingRight = containerStyle.paddingRight.match(/\d*/) ? Number(containerStyle.paddingRight.match(/\d*/)[0]) : 0;
    var paddingTop = containerStyle.paddingTop.match(/\d*/) ? Number(containerStyle.paddingTop.match(/\d*/)[0]) : 0;
    var paddingBottom = containerStyle.paddingBottom.match(/\d*/) ? Number(containerStyle.paddingBottom.match(/\d*/)[0]) : 0;
    var width = containerRect.width - paddingLeft - paddingRight;
    var height = containerRect.height - paddingTop - paddingBottom - 6; // 6为防止溢出的误差值，随便定义的
    var targetScale = Math.ceil(Math.min(width / ((phoneSize === null || phoneSize === void 0 ? void 0 : phoneSize.width) || width), height / ((phoneSize === null || phoneSize === void 0 ? void 0 : phoneSize.height) || height)) * 100);
    setScale(targetScale);
  }
  useEffect(function () {
    if (haveAutoFullscreen || !phoneSize.height || !phoneSize.width) return;
    sethaveAutoFullscreen(true);
    onFull();
  }, [phoneSize]);
  var control = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Switch, {
    size: "small",
    onChange: function onChange(checked) {
      if (checked) {
        localStorage.setItem('showAppVisualControl', 'true');
      } else {
        localStorage.removeItem('showAppVisualControl');
      }
      setShowControl(checked);
    },
    defaultChecked: showControl
  }), "\u4E8B\u4EF6\u63A7\u5236\u5668");
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-controller")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "shuaxin",
    className: "".concat(classPrefix, "-fresh ").concat(fresh ? 'loading' : ''),
    onClick: onFresh
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-status ").concat(status),
    onClick: function onClick() {
      if (status === 'success') return;
      setShowLinkTip(true);
    }
  }, status === 'success' ? '设备已连接' : /*#__PURE__*/React.createElement(React.Fragment, null, "\u754C\u9762\u540C\u6B65\u6682\u505C", /*#__PURE__*/React.createElement(Tooltip, {
    placement: "bottom",
    title: /*#__PURE__*/React.createElement(React.Fragment, null, "\u56E0\u60A8\u5F53\u524D\u6B63\u5728\u5B9A\u4E49\u4E8B\u4EF6\uFF0C\u6240\u4EE5\u4E24\u7AEF\u4FE1\u606F\u65E0\u6CD5\u540C\u6B65\uFF0C\u8BF7\u4FDD\u5B58\u5B9A\u4E49\u5185\u5BB9\u6216\u5173\u95ED\u53F3\u4FA7\u5B9A\u4E49\u7A97\u53E3\u540E\uFF0C\u7535\u8111\u7AEF\u9875\u9762\u5C06\u4E0E\u624B\u673A\u7AEF\u540C\u6B65")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishiicon",
    style: {
      marginLeft: '5px'
    }
  })))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-scal")
  }, /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(IconFont, {
    className: scale <= min ? 'disable' : '',
    type: "shanchu1",
    onClick: onScale.bind(undefined, -1)
  }), /*#__PURE__*/React.createElement("a", {
    style: {
      cursor: 'pointer',
      color: 'inherit'
    },
    onClick: setScale.bind(undefined, 100)
  }, "".concat(scale, "%")), /*#__PURE__*/React.createElement(IconFont, {
    className: scale >= max ? 'disable' : '',
    type: "tianjia",
    onClick: onScale.bind(undefined, 1)
  }), /*#__PURE__*/React.createElement(IconFont, {
    type: "quanpingzhanshi",
    onClick: onFull
  }))), /*#__PURE__*/React.createElement(Space, {
    size: 16
  }, showControl && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Space, {
    align: "center"
  }, /*#__PURE__*/React.createElement("span", {
    className: "element defined",
    style: {
      borderWidth: '1px'
    }
  }), "\u5DF2\u57CB\u70B9\u4E8B\u4EF6"), /*#__PURE__*/React.createElement(Space, {
    align: "baseline"
  }, /*#__PURE__*/React.createElement("span", {
    className: "element definable",
    style: {
      borderWidth: '1px'
    }
  }), "\u53EF\u57CB\u70B9\u4E8B\u4EF6"), /*#__PURE__*/React.createElement(Space, {
    align: "baseline"
  }, /*#__PURE__*/React.createElement("span", {
    className: "element defining",
    style: {
      borderWidth: '1px'
    }
  }), "\u6B63\u5728\u57CB\u70B9\u4E8B\u4EF6")), /*#__PURE__*/React.createElement(Space, null, showControlTip ? /*#__PURE__*/React.createElement(Tooltip, {
    overlay: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", null, "\uD83D\uDC4F \u6B22\u8FCE\u4F7F\u7528\u53EF\u89C6\u5316\u57CB\u70B9"), /*#__PURE__*/React.createElement("div", null, "\u6253\u5F00\u4E8B\u4EF6\u63A7\u5236\u5668\uFF0C\u5F00\u59CB\u5B9A\u4E49\u53EF\u89C6\u5316\u4E8B\u4EF6\u5427!"), /*#__PURE__*/React.createElement(Button, {
      type: "primary",
      onClick: function onClick() {
        setShowControlTip(false);
        localStorage.setItem('showAppVisualControlTip', 'true');
      }
    }, "\u6211\u77E5\u9053\u4E86")),
    open: true,
    placement: "bottomLeft",
    color: "#fff",
    overlayClassName: "".concat(classPrefix, "-guide-tip")
  }, control) : control)), status !== 'success' && showLinkTip && /*#__PURE__*/React.createElement(BizDialog, {
    open: true,
    closable: true,
    footer: null,
    onCancel: function onCancel() {
      setShowLinkTip(false);
    },
    className: "".concat(classPrefix, "-relink")
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(IconFont, {
    type: "cuowu"
  }), "\u8FDE\u63A5\u65AD\u5F00\uFF0C\u8BF7\u626B\u7801\u91CD\u65B0\u8FDE\u63A5\u8BBE\u5907"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-toqrcode")
  }, /*#__PURE__*/React.createElement(LinkQrCode, {
    token: client === null || client === void 0 ? void 0 : client.getToken()
  }))));
};
export default Controller;