function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure undefined"); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

// @ts-nocheck
import React from 'react';
import Draggable from 'react-draggable';
import cn from 'classnames';
import message from "antd/es/message";
import Tooltip from "antd/es/tooltip";
import Row from "antd/es/row";
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { ConfigConsumer } from './config-provider';
import defaultLocale from '../locale/zh_CN';
import { ArrowLeftOutlined, ArrowRightOutlined, PlusOutlined, MinusOutlined, CloseOutlined } from '@ant-design/icons'; // import "@whalecloud/fdx-theme-fdx/Imageviewer.less";
// 加法

var accAdd = function accAdd(arg1, arg2) {
  var r1;
  var r2;
  var newArg1;
  var newArg2;

  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }

  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }

  var c = Math.abs(r1 - r2);
  var m = Math.pow(10, Math.max(r1, r2));

  if (c > 0) {
    var cm = Math.pow(10, c);

    if (r1 > r2) {
      newArg1 = Number(arg1.toString().replace(".", ""));
      newArg2 = Number(arg2.toString().replace(".", "")) * cm;
    } else {
      newArg1 = Number(arg1.toString().replace(".", "")) * cm;
      newArg2 = Number(arg2.toString().replace(".", ""));
    }
  } else {
    newArg1 = Number(arg1.toString().replace(".", ""));
    newArg2 = Number(arg2.toString().replace(".", ""));
  }

  return (newArg1 + newArg2) / m;
}; // 减法


var accSub = function accSub(arg1, arg2) {
  var r1;
  var r2;

  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }

  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }

  var m = Math.pow(10, Math.max(r1, r2)); // 动态控制精度长度

  var n = r1 >= r2 ? r1 : r2;
  return ((arg1 * m - arg2 * m) / m).toFixed(n);
};

var Imageviewer = /*#__PURE__*/function (_React$Component) {
  _inherits(Imageviewer, _React$Component);

  var _super = _createSuper(Imageviewer);

  function Imageviewer(props) {
    var _this;

    _classCallCheck(this, Imageviewer);

    _this = _super.call(this, props);

    _this.prev = function () {
      var _this$props$data = _this.props.data,
          data = _this$props$data === void 0 ? [] : _this$props$data;
      var index = _this.state.index;

      if (index === 0 && data.length > 0) {
        return false;
      }

      if (index > 0 && data.length > 0) {
        _this.setState({
          index: index - 1
        });
      }

      _this.backPosition();
    };

    _this.next = function () {
      var _this$props$data2 = _this.props.data,
          data = _this$props$data2 === void 0 ? [] : _this$props$data2;
      var index = _this.state.index;

      if (index === data.length - 1 && data.length > 0) {
        return false;
      }

      if (index < data.length - 1 && data.length > 0) {
        _this.setState({
          index: index + 1
        });
      }

      _this.backPosition();
    };

    _this.backPosition = function () {
      setTimeout(function () {
        _this.setState({
          scale: 1
        });

        _this.setState({
          defaultPosition: {
            x: 0,
            y: 0
          }
        });
      }, 500);
    };

    _this.onToOut = function () {
      var scale = _this.state.scale;

      if (scale > 0.2) {
        _this.setState({
          scale: accSub(scale, 0.2)
        });
      } else {
        message.warning("已缩小到最小级别");
      }
    };

    _this.onToIn = function () {
      var scale = _this.state.scale;

      if (scale < 3) {
        _this.setState({
          scale: accAdd(scale, 0.2)
        });
      } else {
        message.warning("已放大到最大级别");
      }
    };

    _this.handleStart = function () {
      _this.setState({
        defaultPosition: null
      });
    };

    _this.handleClose = function () {
      var _this$props$onClose = _this.props.onClose,
          onClose = _this$props$onClose === void 0 ? function () {} : _this$props$onClose;

      _this.setState({
        scale: 1
      });

      onClose();
    };

    _this.state = {
      scale: 1,
      defaultPosition: null,
      index: 0
    };
    return _this;
  }

  _createClass(Imageviewer, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      this.setState({
        index: nextProps.index
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$data3 = _this$props.data,
          data = _this$props$data3 === void 0 ? [] : _this$props$data3,
          shadeStyle = _this$props.shadeStyle,
          imageStyle = _this$props.imageStyle,
          visible = _this$props.visible;
      var _this$state = this.state,
          scale = _this$state.scale,
          defaultPosition = _this$state.defaultPosition,
          index = _this$state.index;
      return /*#__PURE__*/React.createElement(ConfigConsumer, null, function (_ref) {
        _objectDestructuringEmpty(_ref);

        return /*#__PURE__*/React.createElement(LocaleReceiver, {
          componentName: "ImageViewer",
          defaultLocale: defaultLocale.ImageViewer
        }, function (locale) {
          return /*#__PURE__*/React.createElement("div", {
            className: "imageviewer",
            style: {
              display: "".concat(visible ? "block" : "none")
            }
          }, /*#__PURE__*/React.createElement("div", {
            className: "shadeWrapper",
            style: _objectSpread({}, shadeStyle)
          }, /*#__PURE__*/React.createElement("div", {
            className: "inner"
          }, /*#__PURE__*/React.createElement(Draggable, {
            defaultPosition: {
              x: 0,
              y: 0
            },
            position: defaultPosition,
            scale: 1,
            onStart: _this2.handleStart
          }, /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("div", {
            className: cn({
              "imageWrapper": true,
              "hide": false
            }),
            style: {
              transform: "scale(".concat(scale, ")")
            }
          }, /*#__PURE__*/React.createElement("img", {
            className: "image",
            id: "TargetImage",
            src: data[index],
            alt: data[index],
            style: _objectSpread({
              width: "100%"
            }, imageStyle)
          }))))), /*#__PURE__*/React.createElement(Row, {
            type: "flex",
            className: "operateTool"
          }, /*#__PURE__*/React.createElement(Tooltip, {
            title: locale.previousOne
          }, /*#__PURE__*/React.createElement("div", {
            onClick: _this2.prev
          }, /*#__PURE__*/React.createElement(ArrowLeftOutlined, {
            translate: true
          }))), /*#__PURE__*/React.createElement(Tooltip, {
            title: locale.nextOne
          }, /*#__PURE__*/React.createElement("div", {
            onClick: _this2.next
          }, /*#__PURE__*/React.createElement(ArrowRightOutlined, {
            translate: true
          }))), /*#__PURE__*/React.createElement(Tooltip, {
            title: locale.zoomIn
          }, /*#__PURE__*/React.createElement("div", {
            onClick: _this2.onToIn
          }, /*#__PURE__*/React.createElement(PlusOutlined, {
            translate: true
          }))), /*#__PURE__*/React.createElement(Tooltip, {
            title: locale.zoomOut
          }, /*#__PURE__*/React.createElement("div", {
            onClick: _this2.onToOut
          }, /*#__PURE__*/React.createElement(MinusOutlined, {
            translate: true
          })))), /*#__PURE__*/React.createElement("div", {
            className: "closeBtnWrapper",
            onClick: _this2.handleClose
          }, /*#__PURE__*/React.createElement(CloseOutlined, {
            translate: true,
            style: {
              fontSize: "24px"
            }
          }))));
        });
      });
    }
  }]);

  return Imageviewer;
}(React.Component);

Imageviewer.defaultProps = {
  data: [],
  onClose: function onClose() {},
  shadeStyle: {},
  imageStyle: {},
  wrapperStyle: {},
  innerStyle: {},
  scale: 1,
  visible: false
};
;
export default Imageviewer;