"use strict";

exports.__esModule = true;
exports.playMessage = playMessage;
var clearMessageTimeoutId;

function playMessage(messageText, politenessSetting) {
  if (politenessSetting === void 0) {
    politenessSetting = 'assertive';
  }

  var ariaLiveRegionId = 'aria-live-region';

  function createAriaLiveRegion(id) {
    var ariaLiveRegionContainer = document.createElement('div');
    var ariaLiveRegion = document.createElement('div');
    ariaLiveRegionContainer.className = 'bootstrap-rc';
    ariaLiveRegion.id = id;
    ariaLiveRegion.className = 'sr-only';
    ariaLiveRegion.setAttribute('aria-live', 'assertive');
    ariaLiveRegionContainer.appendChild(ariaLiveRegion);
    document.body.appendChild(ariaLiveRegionContainer);
    return ariaLiveRegion;
  }

  if (!messageText) {
    return;
  }

  if (clearMessageTimeoutId) {
    clearTimeout(clearMessageTimeoutId);
  }

  var ariaLiveRegion = document.getElementById(ariaLiveRegionId);

  if (!ariaLiveRegion) {
    ariaLiveRegion = createAriaLiveRegion(ariaLiveRegionId);
  }

  var currentPolitenessSetting = ariaLiveRegion.getAttribute('aria-live');

  if (politenessSetting !== currentPolitenessSetting) {
    ariaLiveRegion.setAttribute('aria-live', politenessSetting);
  }

  ariaLiveRegion.innerHTML = messageText;
  clearMessageTimeoutId = setTimeout(function () {
    ariaLiveRegion.innerHTML = '';
  }, 500);
}