"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _idHooks = _interopRequireDefault(require("parsec-hooks/lib/idHooks"));

var _jigsawCaptchaJs = _interopRequireDefault(require("jigsaw-captcha-js"));

var _react = _interopRequireWildcard(require("react"));

var _one = require("remax/one");

var _rpxToPx = _interopRequireDefault(require("../rpx-to-px"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
var _default = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
  var imgs = _ref.imgs,
      _ref$width = _ref.width,
      width = _ref$width === void 0 ? 620 : _ref$width,
      _ref$height = _ref.height,
      height = _ref$height === void 0 ? 310 : _ref$height,
      onFail = _ref.onFail,
      onRefresh = _ref.onRefresh,
      _onSuccess = _ref.onSuccess;
  var id = (0, _idHooks.default)();
  var preImg = (0, _react.useRef)('');
  var init = (0, _react.useCallback)(function () {
    var el = document.getElementById(id);
    (el === null || el === void 0 ? void 0 : el.firstChild) && (el === null || el === void 0 ? void 0 : el.removeChild(el.firstChild));

    _jigsawCaptchaJs.default.init({
      el: el,
      imgs: imgs,
      width: (0, _rpxToPx.default)(width),
      height: (0, _rpxToPx.default)(height),
      onFail: onFail,
      onRefresh: onRefresh,
      onSuccess: function onSuccess() {
        _onSuccess === null || _onSuccess === void 0 ? void 0 : _onSuccess();
        setTimeout(function () {
          init();
        }, 1000);
      }
    });
  }, [height, id, imgs, onFail, onRefresh, _onSuccess, width]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      reset: init
    };
  });
  (0, _react.useEffect)(function () {
    if (JSON.stringify(imgs) === preImg.current) return;
    preImg.current = JSON.stringify(imgs);
    init();
  }, [height, id, imgs, init, onFail, onRefresh, _onSuccess, width]);
  return /*#__PURE__*/_react.default.createElement(_one.View, {
    id: id
  });
});

exports.default = _default;