var _excluded = ["fileName", "progressValue", "errorMessage", "onLabelClick", "onClose", "qa"];
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 { Token } from '../Token/Token';
import { Tooltip } from '../Tooltip/Tooltip';
import { useI18nContext } from '../_hooks/I18n';
import { parseFilename } from '../_utils/filename';
import { pulseAnimationDuration, StyledExtension, StyledFilename, StyledFilenameShortcut, StyledFileToken } from './FileToken.styles';
/**
 * @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 _parseFilename = parseFilename(fileName),
    filename = _parseFilename.filename,
    extension = _parseFilename.extension,
    isFilename = _parseFilename.isFilename;
  var progressBarLabelId = useId();
  var name = isFilename ? "".concat(filename, ".") : fileName;
  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 token = /*#__PURE__*/React.createElement(Token, null, /*#__PURE__*/React.createElement(Token.Label, {
    onClick: onLabelClick
  }, /*#__PURE__*/React.createElement(StyledFilenameShortcut, null, /*#__PURE__*/React.createElement(StyledFilename, null, name), /*#__PURE__*/React.createElement(StyledExtension, null, extension))), /*#__PURE__*/React.createElement(Token.Remove, {
    onClick: onClose,
    "data-qa": 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
    }), token, /*#__PURE__*/React.createElement(Loader, {
      value: progressValue,
      animated: true
    }, token)));
  }
  return /*#__PURE__*/React.createElement(StyledFileToken, _extends({}, props, {
    $error: isError,
    $finalized: isFinalized,
    $progressComplete: isProgressComplete,
    ref: ref
  }), token, /*#__PURE__*/React.createElement(Loader, {
    value: progressValue,
    animated: true,
    "aria-hidden": true
  }, token), /*#__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