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; }
import { useRef, useState } from 'react';
import { useRequest } from 'ahooks';
import { PisellToast } from '@pisell/materials';
import useEngineContext from "../../hooks/useEngineContext";
import { request } from "../../utils";
export var usePinVerify = function usePinVerify() {
  var _context$appHelper;
  var context = useEngineContext();
  var apiPrefix = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.constants) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.apiPrefix;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    verifyInfo = _useState2[0],
    setVerifyInfo = _useState2[1];
  var verifySuccessRef = useRef(function () {});

  /**
   * @title: 设置验证信息
   * @description 设置验证信息，如果check_max_count为0，则设置为null，剩余次数为check_max_count - failed_count
   * @param data 验证信息
   */
  var _setVerifyInfo = function _setVerifyInfo(data) {
    var _ref = data || {},
      failed_count = _ref.failed_count,
      check_max_count = _ref.check_max_count;
    if (!check_max_count) {
      setVerifyInfo(null);
      return;
    }
    setVerifyInfo({
      check_max_count: check_max_count,
      remaining_count: check_max_count - (failed_count || 0)
    });
  };
  var verifyPinApi = function verifyPinApi(data) {
    return request.getRequest().post("".concat(apiPrefix, "/wallet/detail/pin/validate"), data, {
      fullResult: true
    });
  };
  var _verifyPin = useRequest(verifyPinApi, {
    manual: true,
    onSuccess: function onSuccess(res) {
      if ((res === null || res === void 0 ? void 0 : res.code) === 200) {
        var _ref2 = (res === null || res === void 0 ? void 0 : res.data) || {},
          status = _ref2.status,
          msg = _ref2.msg,
          failed_count = _ref2.failed_count,
          check_max_count = _ref2.check_max_count;
        _setVerifyInfo({
          failed_count: failed_count,
          check_max_count: check_max_count
        });
        // 状态 success:成功 locked:锁定 failed:失败
        PisellToast({
          content: msg,
          duration: 2
        });
        if (status === 'success') {
          var _verifySuccessRef$cur;
          (_verifySuccessRef$cur = verifySuccessRef.current) === null || _verifySuccessRef$cur === void 0 || _verifySuccessRef$cur.call(verifySuccessRef, res === null || res === void 0 ? void 0 : res.data);
        }
      }
    }
  });
  var verifyPin = function verifyPin(data, onSuccess) {
    verifySuccessRef.current = onSuccess;
    _verifyPin.run(data);
  };
  return {
    verifyPin: verifyPin,
    loading: _verifyPin.loading,
    verifyInfo: verifyInfo,
    setVerifyInfo: _setVerifyInfo
  };
};