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 React, { useImperativeHandle, useState, forwardRef, useRef, useMemo } from 'react';
import Delete from '@pisell/icon/es/Delete';
import { PisellModal, VirtualKeyboard, PisellToast } from '@pisell/materials';
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import localeTexts from "./locales";
import { request } from "../../utils";
import "./index.less";
import { usePinVerify } from "./hooks";
var PinVerifyModal = function PinVerifyModal(props, ref) {
  var _context$engine, _context$appHelper, _context$appHelper2, _context$appHelper2$i, _context$appHelper3, _modalParamsRef$curre2, _modalParamsRef$curre3, _modalParamsRef$curre4;
  var context = useEngineContext();
  locales.init(localeTexts, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || 'en');
  request.setRequest(context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request);

  // 判断是否是终端环境
  var isTerminal = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$i = _context$appHelper2.isTerminal) === null || _context$appHelper2$i === void 0 ? void 0 : _context$appHelper2$i.call(_context$appHelper2);
  var platform = context === null || context === void 0 || (_context$appHelper3 = context.appHelper) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.constants) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.platform;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var modalParamsRef = useRef({});
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    pin = _useState4[0],
    setPin = _useState4[1];
  var _usePinVerify = usePinVerify(),
    verifyPin = _usePinVerify.verifyPin,
    loading = _usePinVerify.loading,
    verifyInfo = _usePinVerify.verifyInfo,
    setVerifyInfo = _usePinVerify.setVerifyInfo;
  var KeyboardItems = useMemo(function () {
    return [{
      value: 1,
      label: '1',
      disabled: false,
      type: 'text'
    }, {
      value: 2,
      label: '2',
      disabled: false,
      type: 'text'
    }, {
      value: 3,
      label: '3',
      disabled: false,
      type: 'text'
    }, {
      value: 4,
      label: '4',
      disabled: false,
      type: 'text'
    }, {
      value: 5,
      label: '5',
      disabled: false,
      type: 'text'
    }, {
      value: 6,
      label: '6',
      disabled: false,
      type: 'text'
    }, {
      value: 7,
      label: '7',
      disabled: false,
      type: 'text'
    }, {
      value: 8,
      label: '8',
      disabled: false,
      type: 'text'
    }, {
      value: 9,
      label: '9',
      disabled: false,
      type: 'text'
    }, {
      value: 'delete',
      label: /*#__PURE__*/React.createElement(Delete, {
        className: "virtual-keyboard-input-delete-icon"
      }),
      disabled: false,
      type: 'text'
    }, {
      value: 0,
      label: '0',
      disabled: false,
      type: 'text'
    }, {
      value: 'verify',
      label: locales.getText('pisell2.pin.verify.modal.keyboard.verify'),
      disabled: false,
      type: 'primary'
    }];
  }, [locales]);
  var onOpen = function onOpen(params) {
    var _props$onOpen;
    modalParamsRef.current = params;
    setVerifyInfo({
      failed_count: (params === null || params === void 0 ? void 0 : params.failed_count) || 0,
      check_max_count: (params === null || params === void 0 ? void 0 : params.check_max_count) || 0
    });
    setVisible(true);
    (_props$onOpen = props.onOpen) === null || _props$onOpen === void 0 || _props$onOpen.call(props);
  };
  var onClose = function onClose() {
    var _props$onClose;
    modalParamsRef.current = {};
    setPin('');
    setVerifyInfo(null);
    setVisible(false);
    (_props$onClose = props.onClose) === null || _props$onClose === void 0 || _props$onClose.call(props);
  };
  useImperativeHandle(ref, function () {
    return {
      open: onOpen,
      close: onClose
    };
  });
  var handleChange = function handleChange(e) {
    if (e === 'delete') {
      setPin(pin.slice(0, -1));
      return;
    } else if (e === 'verify') {
      handleVerify();
      return;
    } else {
      if (pin.length < 4) {
        setPin(pin + e);
      }
    }
  };
  var handleVerify = function handleVerify() {
    if (loading) {
      return;
    }
    if (pin.length === 4) {
      var _modalParamsRef$curre;
      var wallet_detail_id = (_modalParamsRef$curre = modalParamsRef.current) === null || _modalParamsRef$curre === void 0 ? void 0 : _modalParamsRef$curre.wallet_detail_id;
      verifyPin({
        wallet_detail_id: wallet_detail_id,
        pin: pin
      }, function (params) {
        var _props$onSuccess;
        (_props$onSuccess = props.onSuccess) === null || _props$onSuccess === void 0 || _props$onSuccess.call(props, params);
        onClose();
      });
    } else {
      PisellToast({
        content: locales.getText('pisell2.pin.verify.modal.keyboard.title'),
        duration: 2
      });
    }
  };
  return /*#__PURE__*/React.createElement(PisellModal, {
    platform: isTerminal ? 'h5' : undefined,
    visible: visible,
    title: locales.getText('pisell2.pin.verify.modal.title'),
    headerDivider: false,
    footer: null,
    onClose: onClose,
    maskClosable: false,
    onCancel: onClose,
    destroyOnClose: true,
    mobileModalHeight: isTerminal ? undefined : 'auto'
  }, /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-user"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-user-avatar"
  }, /*#__PURE__*/React.createElement("img", {
    src: ((_modalParamsRef$curre2 = modalParamsRef.current.customer) === null || _modalParamsRef$curre2 === void 0 ? void 0 : _modalParamsRef$curre2.cover) || 'https://file.mypisell.com/static/default_product.png',
    alt: "avatar",
    className: "pin-verify-modal-content-user-avatar-img"
  })), /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-user-right"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-user-right-name"
  }, ((_modalParamsRef$curre3 = modalParamsRef.current.customer) === null || _modalParamsRef$curre3 === void 0 ? void 0 : _modalParamsRef$curre3.display_name) || ((_modalParamsRef$curre4 = modalParamsRef.current.customer) === null || _modalParamsRef$curre4 === void 0 ? void 0 : _modalParamsRef$curre4.nickname)), /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-user-right-wallet"
  }, locales.getText('pisell2.pin.verify.modal.wallet'), modalParamsRef.current.wallet_code))), /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-keyboard"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-keyboard-title"
  }, locales.getText('pisell2.pin.verify.modal.keyboard.title')), /*#__PURE__*/React.createElement(VirtualKeyboard, {
    style: {
      width: '300px'
    },
    keyboardProps: {
      keydown: false,
      items: KeyboardItems,
      onChange: handleChange
    },
    virtualKeyInputProps: {
      value: pin,
      onChange: function onChange(e) {
        setPin(e.target.value);
      },
      placeholder: '0000',
      style: {
        height: '45px',
        fontSize: '36px'
      }
    }
  }), verifyInfo !== null && verifyInfo !== void 0 && verifyInfo.check_max_count ? /*#__PURE__*/React.createElement("div", {
    className: "pin-verify-modal-content-keyboard-desc"
  }, locales.getText('pisell2.pin.verify.modal.keyboard.desc'), verifyInfo === null || verifyInfo === void 0 ? void 0 : verifyInfo.remaining_count, "/", verifyInfo === null || verifyInfo === void 0 ? void 0 : verifyInfo.check_max_count) : null)));
};
export default /*#__PURE__*/forwardRef(PinVerifyModal);