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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useRef, useEffect, useState, useCallback, useContext } from 'react';
import { Loading3QuartersOutlined } from '@ant-design/icons';
import { notification } from 'antd';
import { useRequest } from 'ahooks';
import { GlobalTaskContext } from "../context";
import { ConfigContext } from "../../config-provider/context";
var ItemUI = function ItemUI(props) {
  var componentContext = useContext(ConfigContext);
  var uiLocale = componentContext.uiLocale;
  return /*#__PURE__*/React.createElement("div", {
    className: "item"
  }, /*#__PURE__*/React.createElement("span", {
    className: "filename"
  }, props.fileName), props.status == 1 ? uiLocale.FileExport.popoDownload : /*#__PURE__*/React.createElement(Loading3QuartersOutlined, {
    spin: true
  }));
};

// 单条记录
var DELAY = 5 * 1000; // 轮询时间间隔5s
var Item = function Item(props) {
  var componentContext = useContext(ConfigContext);
  var uiLocale = componentContext.uiLocale;
  var _useContext = useContext(GlobalTaskContext),
    _useContext$delay = _useContext.delay,
    delay = _useContext$delay === void 0 ? DELAY : _useContext$delay,
    progressFn = _useContext.progressFn;
  var _useState = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    status = _useState2[0],
    setStatus = _useState2[1];
  var timer = useRef(null);
  var _useRequest = useRequest(progressFn, {
      manual: true
    }),
    runAsync = _useRequest.runAsync; // 获取进度

  // 导出失败
  var errorDownload = useCallback(function () {
    notification.error({
      duration: 6,
      message: props.fileName + uiLocale.FileExport.failed,
      description: uiLocale.FileExport.repeat
    });
  }, [props.fileName]);

  // 成功导出
  var successDownload = useCallback(function () {
    notification.success({
      duration: 6,
      message: props.fileName + uiLocale.FileExport.success,
      description: uiLocale.FileExport.popoDownload
    });
  }, [props.fileName]);
  var clearTimer = function clearTimer() {
    props.callback && props.callback(props.taskId);
    clearInterval(timer.current);
    timer.current = null;
  };

  // 查询任务的进度
  var queryTaskProcess = useCallback(function () {
    runAsync({
      processKey: props.taskId
    }).then(function (_ref) {
      var code = _ref.code,
        data = _ref.data;
      if (+code !== 200) {
        errorDownload();
        clearTimer();
        return;
      }
      if (+code === 200 && +data.status === 1) {
        setStatus(1);
        successDownload();
        clearTimer();
      }
    }).catch(function () {
      errorDownload();
      clearTimer();
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [runAsync, props, timer]);
  useEffect(function () {
    if (!timer.current) {
      timer.current = setInterval(function () {
        queryTaskProcess();
      }, delay);
    }
  }, [delay, queryTaskProcess, timer]);
  return /*#__PURE__*/React.createElement(ItemUI, _extends({}, props, {
    status: status
  }));
};
export { Item, ItemUI };