"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
// How to use:
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions#Preferring_specialized_live_region_roles
// Demo: https://dequeuniversity.com/library/aria/liveregion-playground

// Important: Strongly recommend test your solution in all supported screen readers
// if you use non default value of properties: ariaAtomic, ariaLive, ariaRelevant, role

// Note: Text won't be announced if the text message doesn't change after the render.
// For using a forced announcement in this case, set the 'key' attribute - key={Date.now()}

// Note: Flag 'contentRendered' resolves bug with duplicates messages (NVDA + Firefox)
// https://github.com/nvaccess/nvda/labels/bug%2Fdouble-speaking

var timer;
var Announcer = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _ref$ariaAtomic = _ref.ariaAtomic,
    ariaAtomic = _ref$ariaAtomic === void 0 ? 'true' : _ref$ariaAtomic,
    _ref$ariaLive = _ref.ariaLive,
    ariaLive = _ref$ariaLive === void 0 ? 'polite' : _ref$ariaLive,
    _ref$ariaRelevant = _ref.ariaRelevant,
    ariaRelevant = _ref$ariaRelevant === void 0 ? 'all' : _ref$ariaRelevant,
    _ref$role = _ref.role,
    role = _ref$role === void 0 ? 'status' : _ref$role,
    _ref$text = _ref.text,
    text = _ref$text === void 0 ? '' : _ref$text,
    _ref$delay = _ref.delay,
    delay = _ref$delay === void 0 ? 0 : _ref$delay;
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    contentRendered = _useState2[0],
    setContentRendered = _useState2[1];
  (0, _react.useEffect)(function () {
    clearTimeout(timer);
    setContentRendered(false);
    timer = setTimeout(function () {
      setContentRendered(true);
    }, delay);
    return function () {
      return clearTimeout(timer);
    };
  }, [text, delay]);
  return /*#__PURE__*/_react.default.createElement("div", {
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
    className: "assistive",
    ref: ref,
    role: role,
    "aria-atomic": ariaAtomic,
    "aria-relevant": ariaRelevant,
    "aria-live": ariaLive
  }, contentRendered && /*#__PURE__*/_react.default.createElement("span", null, text));
});
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(Announcer);