"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.promise.js");
require("core-js/modules/es.array.flat.js");
require("core-js/modules/es.array.unscopables.flat.js");
var _react = _interopRequireWildcard(require("react"));
var _form = _interopRequireDefault(require("../../../form"));
var _input = _interopRequireDefault(require("../../../input"));
var _button = _interopRequireDefault(require("../../../button"));
var _createField = _interopRequireDefault(require("../../create-field"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }
const ProFormCaptcha = /*#__PURE__*/_react.default.forwardRef((props, ref) => {
  const [count, setCount] = (0, _react.useState)(props.countDown || 60);
  const [timing, setTiming] = (0, _react.useState)(false);
  const [loading, setLoading] = (0, _react.useState)();
  // 这么写是为了防止restProps中 带入 onChange, defaultValue, rules props tabUtil
  const {
    // eslint-disable-next-line
    rules,
    // eslint-disable-next-line
    name,
    phoneName,
    fieldProps,
    captchaTextRender = (paramsTiming, paramsCount) => {
      return paramsTiming ? `${paramsCount} 秒后重新获取` : '获取验证码';
    },
    captchaProps,
    value,
    onChange,
    ...restProps
  } = props;
  const onGetCaptcha = (0, _react.useCallback)(async mobile => {
    try {
      setLoading(true);
      await restProps.onGetCaptcha(mobile);
      setLoading(false);
      setTiming(true);
    } catch (error) {
      setLoading(false);
      // eslint-disable-next-line no-console
      console.log(error);
    }
  }, []);
  (0, _react.useEffect)(() => {
    let interval = 0;
    const {
      countDown
    } = props;
    if (timing) {
      interval = window.setInterval(() => {
        setCount(preSecond => {
          if (preSecond <= 1) {
            setTiming(false);
            clearInterval(interval);
            // 重置秒数
            return countDown || 60;
          }
          return preSecond - 1;
        });
      }, 1000);
    }
    return () => clearInterval(interval);
  }, [timing]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_form.default.Item, {
    noStyle: true,
    shouldUpdate: true,
    children: ({
      getFieldValue,
      validateFields
    }) => /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      style: {
        ...(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.style),
        display: 'flex',
        alignItems: 'center'
      },
      ref: ref,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_input.default, {
        ...fieldProps,
        style: {
          flex: 1,
          transition: 'width .3s',
          marginRight: 8
        },
        value: value,
        onChange: onChange
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.default, {
        style: {
          display: 'block'
        },
        disabled: timing,
        loading: loading,
        ...captchaProps,
        onClick: async () => {
          try {
            if (phoneName) {
              await validateFields([phoneName].flat(1));
              const mobile = getFieldValue([phoneName].flat(1));
              await onGetCaptcha(mobile);
            } else {
              await onGetCaptcha('');
            }
          } catch (error) {
            // eslint-disable-next-line no-console
            console.log(error);
          }
        },
        children: captchaTextRender(timing, count)
      })]
    })
  });
});
ProFormCaptcha.displayName = 'ProFormCaptcha';
var _default = (0, _createField.default)(ProFormCaptcha);
exports.default = _default;