'use strict';

require('@progress/telerik-common-report-viewer');

var defaultOptions = {};
function Accessibility(options) {
  var controller;
  var areas;
  var lastArea;
  var keyMap = {
    CONFIRM_KEY: 13,
    // C
    CONTENT_AREA_KEY: 67,
    // D
    DOCUMENT_MAP_AREA_KEY: 68,
    // M
    MENU_AREA_KEY: 77,
    // P
    PARAMETERS_AREA_KEY: 80
  };
  options = $.extend({}, defaultOptions, options);
  controller = options.controller;
  if (!controller) {
    throw "No controller (telerikReporting.ReportViewerController) has been specified.";
  }
  init();
  function init() {
    _attachEvents();
  }
  function _attachEvents() {
    controller.onAsync("reportLoadComplete", async () => {
      setAccessibilityUI();
      var content = findContentArea();
      if (content.length > 0) {
        content.focus();
      }
    }).on("pageReady", (pageInfo) => {
      initPage(pageInfo);
    }).on("error", (event, message) => {
      focusOnErrorMessage();
      window.setTimeout(setAccessibilityUI, 500);
    });
  }
  function setAccessibilityUI() {
    if (!areas) {
      initAreas();
      $(document.body).off("keydown", processKeyDown);
      $(document.body).on("keydown", processKeyDown);
    }
  }
  function focusOnErrorMessage() {
    var $errMsg = $("div.trv-pages-area div.trv-error-message");
    if ($errMsg.length === 0) {
      return;
    }
    $errMsg.attr("tabIndex", 0);
    $errMsg.focus();
  }
  function initPage(pageInfo) {
    if (!pageInfo) {
      return;
    }
    setAccessibilityUI();
    var area = areas[keyMap.CONTENT_AREA_KEY];
    setContentAreaKeyDown(area);
  }
  function initAreas() {
    areas = {};
    areas[keyMap.DOCUMENT_MAP_AREA_KEY] = findDocumentMapArea();
    areas[keyMap.MENU_AREA_KEY] = findMenuArea();
    areas[keyMap.CONTENT_AREA_KEY] = findContentArea();
    var parametersArea = findParametersArea();
    if (parametersArea) {
      areas[keyMap.PARAMETERS_AREA_KEY] = parametersArea;
      setParameterEditorsKeyDown(parametersArea);
    }
  }
  function findContentArea() {
    return $("div[data-role=telerik_ReportViewer_PagesArea]");
  }
  function findDocumentMapArea() {
    return $("div[data-role=telerik_ReportViewer_DocumentMapArea] div[data-role=treeview]");
  }
  function findMenuArea() {
    return $("div[data-role=telerik_ReportViewer_Toolbar]");
  }
  function findParametersArea() {
    return $("div[data-role=telerik_ReportViewer_ParametersArea] .trv-parameters-area-content .trv-parameters-wrapper");
  }
  function processKeyDown(event) {
    if (!areas) {
      return;
    }
    if (!(event.altKey && event.ctrlKey)) {
      return;
    }
    var currentArea = areas[event.which];
    if (!currentArea) {
      return;
    }
    if (!IsAreaContainerVisible(currentArea.parent())) {
      return;
    }
    var className = "k-focus";
    if (lastArea) {
      lastArea.removeClass(className);
    }
    currentArea.addClass(className);
    currentArea.focus();
    lastArea = currentArea;
    event.preventDefault();
  }
  function setParameterEditorsKeyDown(parametersAreaContent) {
    if (parametersAreaContent.length === 0) {
      return;
    }
    var $paramsArea = parametersAreaContent.parent("div[data-role=telerik_ReportViewer_ParametersArea]");
    if (!IsAreaContainerVisible($paramsArea)) {
      return;
    }
    Array.from(parametersAreaContent.children()).forEach((child) => {
      $(child).on("keydown", (event) => {
        if (event.which == keyMap.CONFIRM_KEY) {
          var paramsButton = $paramsArea.find("button.trv-parameters-area-preview-button");
          paramsButton.focus();
          event.preventDefault();
        }
      });
    });
  }
  function IsAreaContainerVisible(container) {
    return container && !(container.hasClass("k-collapsed") || container.hasClass("k-hidden"));
  }
  function setContentAreaKeyDown(contentArea) {
    if (!contentArea) {
      return;
    }
    var actions = contentArea.find("div [data-reporting-action]");
    if (!actions.length > 0) {
      return;
    }
    Array.from(actions).forEach((action) => {
      var $action = $(action);
      $action.on("keydown", (event) => {
        if (event.which == keyMap.CONFIRM_KEY) {
          $action.trigger("click");
        }
      });
    });
  }
  function setKeyMap(keyMapValues) {
    keyMap = keyMapValues;
    areas = void 0;
  }
  function getKeyMap() {
    return keyMap;
  }
  return {
    getKeyMap,
    setKeyMap
  };
}

exports.Accessibility = Accessibility;
