function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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, { useState, useEffect, useCallback, useRef, forwardRef, useImperativeHandle } from 'react';
import { Button } from 'antd';
import { useIntl } from 'dumi';
import "./index.less";
var DEFAULT_COUNTING_TIME = 60;
var CountDownButton = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var initialCounting = _ref.counting,
    en = _ref.en,
    sendText = _ref.sendText,
    onSendClick = _ref.onSendClick,
    buttonProps = _ref.buttonProps,
    onCountDownEnd = _ref.onCountDownEnd;
  var _useIntl = useIntl(),
    formatMessage = _useIntl.formatMessage;
  var _useState = useState(!!initialCounting),
    _useState2 = _slicedToArray(_useState, 2),
    counting = _useState2[0],
    setCounting = _useState2[1];
  var _useState3 = useState(DEFAULT_COUNTING_TIME),
    _useState4 = _slicedToArray(_useState3, 2),
    countingTime = _useState4[0],
    setCountingTime = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    hasSend = _useState6[0],
    setHasSend = _useState6[1];
  var countTimer = useRef(null);
  var reset = useCallback(function () {
    setCounting(false);
    setCountingTime(DEFAULT_COUNTING_TIME);
    if (countTimer.current) {
      clearInterval(countTimer.current);
      countTimer.current = null;
    }
  }, [countTimer]);
  var doCounting = useCallback(function () {
    setCountingTime(DEFAULT_COUNTING_TIME);
    setCounting(true);
    countTimer.current = setInterval(function () {
      setCountingTime(function (prevTime) {
        if (prevTime === 1) {
          reset();
          onCountDownEnd === null || onCountDownEnd === void 0 || onCountDownEnd();
          return DEFAULT_COUNTING_TIME;
        }
        return prevTime - 1;
      });
    }, 1000);
  }, [reset]);
  useEffect(function () {
    if (initialCounting) {
      doCounting();
    }
    return function () {
      if (countTimer.current) {
        clearInterval(countTimer.current);
      }
    };
  }, [initialCounting]);
  var handleSendClick = function handleSendClick() {
    if (onSendClick) {
      onSendClick(function () {
        doCounting();
        setHasSend(true);
      });
    }
  };
  var renderSendCodeTxt = function renderSendCodeTxt() {
    if (counting) {
      return /*#__PURE__*/React.createElement("span", {
        className: "code-count-text"
      }, formatMessage({
        id: 'login.countdown.resend'
      }, {
        time: countingTime
      }));
    }
    if (hasSend) {
      return formatMessage({
        id: 'login.countdown.resend.simple'
      });
    }
    return sendText;
  };
  useImperativeHandle(ref, function () {
    return {
      resetCountDown: function resetCountDown() {
        reset();
      }
    };
  });
  return /*#__PURE__*/React.createElement("span", {
    className: "countdown-btn"
  }, /*#__PURE__*/React.createElement(Button, _extends({
    onClick: handleSendClick,
    className: "code-send-button",
    disabled: counting
  }, buttonProps), /*#__PURE__*/React.createElement("span", {
    className: "code-send-text"
  }, renderSendCodeTxt())));
});
export default CountDownButton;