var _excluded = ["fileName", "progressValue", "errorMessage", "onLabelClick", "onClose", "qa"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import { useId } from '@react-aria/utils';
import { VisuallyHidden } from '@react-aria/visually-hidden';
import React from 'react';
import { Loader } from '../Loader/Loader';
import { loaderTransitionDuration } from '../Loader/Loader.styles';
import { Tooltip } from '../Tooltip/Tooltip';
import { useI18nContext } from '../_hooks/I18n';
import { pulseAnimationDuration, StyledFileToken } from './FileToken.styles';
import { FileTokenInner } from './FileTokenInner';

/**
 * @deprecatedSince 11
 * @deprecated Intended for internal library development.
 */
export var FileToken = /*#__PURE__*/React.forwardRef(function FileToken(_ref, ref) {
  var fileName = _ref.fileName,
    _ref$progressValue = _ref.progressValue,
    progressValue = _ref$progressValue === void 0 ? 0 : _ref$progressValue,
    _ref$errorMessage = _ref.errorMessage,
    errorMessage = _ref$errorMessage === void 0 ? '' : _ref$errorMessage,
    onLabelClick = _ref.onLabelClick,
    onClose = _ref.onClose,
    qa = _ref.qa,
    props = _objectWithoutProperties(_ref, _excluded);
  var i18n = useI18nContext();
  var isError = Boolean(errorMessage);
  var isProgressValueComplete = !isError && progressValue >= 100;
  var _React$useState = React.useState(isProgressValueComplete),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isProgressComplete = _React$useState2[0],
    setProgressComplete = _React$useState2[1];
  var _React$useState3 = React.useState(false),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    isFinalized = _React$useState4[0],
    setFinalized = _React$useState4[1];
  var didMountRef = React.useRef(false);
  var progressBarLabelId = useId();
  React.useEffect(function () {
    var timerId = isProgressValueComplete ? window.setTimeout(function () {
      setProgressComplete(true);
    }, loaderTransitionDuration) : null;
    if (!isProgressValueComplete) {
      setProgressComplete(false);
    }
    return function () {
      timerId && clearTimeout(timerId);
    };
  }, [isProgressValueComplete]);
  React.useEffect(function () {
    var isFinalized = didMountRef.current && isProgressComplete;
    var timerId = isFinalized ? window.setTimeout(function () {
      setFinalized(false);
    }, pulseAnimationDuration) : null;
    setFinalized(didMountRef.current && isProgressComplete);
    if (!didMountRef.current) {
      didMountRef.current = true;
    }
    return function () {
      timerId && clearTimeout(timerId);
    };
  }, [isProgressComplete]);
  var renderToken = function renderToken() {
    var includeInTabOrder = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    return /*#__PURE__*/React.createElement(FileTokenInner, {
      fullFilename: fileName,
      includeInTabOrder: includeInTabOrder,
      onClick: onLabelClick,
      onClose: onClose,
      qaCloseBtn: qa === null || qa === void 0 ? void 0 : qa.closeButton
    });
  };

  // prevent empty tooltip blinks when navigating via the keyboard by wrapping in tooltip conditionally
  if (isError) {
    return /*#__PURE__*/React.createElement(Tooltip, {
      ref: ref,
      trigger: "hover",
      overlay: errorMessage
    }, /*#__PURE__*/React.createElement(StyledFileToken, _extends({}, props, {
      $error: isError,
      $finalized: isFinalized,
      $progressComplete: isProgressComplete
    }), renderToken(true), /*#__PURE__*/React.createElement(Loader, {
      value: progressValue,
      animated: true
    }, renderToken())));
  }
  return /*#__PURE__*/React.createElement(StyledFileToken, _extends({}, props, {
    $error: isError,
    $finalized: isFinalized,
    $progressComplete: isProgressComplete,
    ref: ref
  }), renderToken(true), /*#__PURE__*/React.createElement(Loader, {
    value: progressValue,
    animated: true,
    "aria-hidden": true
  }, renderToken()), /*#__PURE__*/React.createElement(VisuallyHidden, null, /*#__PURE__*/React.createElement("span", {
    id: progressBarLabelId
  }, i18n.t('core.fileToken.uploading')), /*#__PURE__*/React.createElement("div", {
    "aria-labelledby": progressBarLabelId,
    role: "progressbar",
    "aria-valuenow": progressValue,
    "aria-valuemin": 0,
    "aria-valuemax": 100,
    "aria-valuetext": "".concat(progressValue, "%")
  })));
});
FileToken.displayName = 'FileToken';
//# sourceMappingURL=FileToken.js.map