'use strict';

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

function _interopNamespaceDefault(e) {
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n.default = e;
    return Object.freeze(n);
}

var RCV__namespace = /*#__PURE__*/_interopNamespaceDefault(RCV);

function UIController(options) {
  var stateFlags = {
    ExportInProgress: 1 << 0,
    PrintInProgress: 1 << 1,
    RenderInProgress: 1 << 2
  };
  function getState(flags) {
    return (state & flags) != 0;
  }
  function setState(flags, value) {
    if (value) {
      state |= flags;
    } else {
      state &= ~flags;
    }
  }
  var controller = options.controller;
  var notificationService = options.notificationService;
  var historyManager = options.history;
  var state = 0;
  var refreshUI;
  var commands = options.commands;
  if (!controller) {
    throw "No controller (telerikReporting.ReportViewerController) has been specified.";
  }
  if (!notificationService) {
    throw "No notificationService (telerikReporting.NotificationService) has been specified.";
  }
  function getDocumentMapState() {
    var args = {};
    notificationService.getDocumentMapState(args);
    return args;
  }
  function getParametersAreaState() {
    var args = {};
    notificationService.getParametersAreaState(args);
    return args;
  }
  function getSendEmailDialogState() {
    var args = {};
    notificationService.getSendEmailDialogState(args);
    return args;
  }
  function updateUI() {
    if (!refreshUI) {
      refreshUI = true;
      window.setTimeout(() => {
        try {
          updateUICore();
        } finally {
          refreshUI = false;
        }
      }, 10);
    }
  }
  function updateUICore() {
    var rs = controller.getReportSource();
    var pageCount = controller.getPageCount();
    var currentPageNumber = controller.getCurrentPageNumber();
    var hasReport = rs && rs.report;
    var hasPages = hasReport && pageCount > 0;
    var nextPage = hasPages && currentPageNumber < pageCount;
    var prevPage = hasPages && currentPageNumber > 1;
    var hasPage = hasPages && currentPageNumber;
    var documentMapState = getDocumentMapState();
    var parametersAreaState = getParametersAreaState();
    var sendEmailDialogState = getSendEmailDialogState();
    var searchInitiated = controller.getSearchInitiated();
    var aiPromptInitiated = controller.getAiPromptInitiated();
    var renderInProgress = getState(stateFlags.RenderInProgress);
    var printInProgress = getState(stateFlags.PrintInProgress);
    var exportInProgress = getState(stateFlags.ExportInProgress);
    commands.goToFirstPage.setEnabled(prevPage);
    commands.goToPrevPage.setEnabled(prevPage);
    commands.stopRendering.setEnabled(hasReport && renderInProgress);
    commands.goToLastPage.setEnabled(nextPage);
    commands.goToNextPage.setEnabled(nextPage);
    commands.goToPage.setEnabled(hasPages);
    commands.print.setEnabled(hasPages && !renderInProgress && !printInProgress);
    commands.export.setEnabled(hasPages && !renderInProgress && !exportInProgress);
    commands.refresh.setEnabled(hasReport);
    commands.historyBack.setEnabled(historyManager && historyManager.canMoveBack());
    commands.historyForward.setEnabled(historyManager && historyManager.canMoveForward());
    commands.toggleDocumentMap.setEnabled(hasReport && documentMapState.enabled);
    commands.toggleDocumentMap.setChecked(documentMapState.enabled && documentMapState.visible);
    commands.toggleParametersArea.setEnabled(hasReport && parametersAreaState.enabled);
    commands.toggleParametersArea.setChecked(parametersAreaState.enabled && parametersAreaState.visible);
    commands.togglePrintPreview.setEnabled(hasPages).setChecked(controller.getViewMode() == RCV__namespace.ViewMode.PrintPreview);
    commands.pageMode.setEnabled(hasPages).setChecked(controller.getPageMode() == RCV__namespace.PageMode.ContinuousScroll);
    commands.zoom.setEnabled(hasPage);
    commands.zoomIn.setEnabled(hasPage);
    commands.zoomOut.setEnabled(hasPage);
    commands.toggleZoomMode.setEnabled(hasPage).setChecked(controller.getScaleMode() == RCV__namespace.ScaleMode.FitPage || controller.getScaleMode() == RCV__namespace.ScaleMode.FitPageWidth);
    commands.toggleSearchDialog.setEnabled(hasPages).setChecked(searchInitiated);
    commands.toggleSendEmailDialog.setEnabled(hasPages).setChecked(sendEmailDialogState.visible);
    commands.toggleAiPromptDialog.setEnabled(hasPages && !renderInProgress).setChecked(aiPromptInitiated);
    notificationService.updateUI(null);
    notificationService.pageNumberChange(currentPageNumber);
    notificationService.pageCountChange(pageCount);
  }
  notificationService.setParametersAreaVisible(updateUI);
  notificationService.setDocumentMapVisible(updateUI);
  notificationService.updateUIInternal(updateUI);
  notificationService.setSearchDialogVisible(updateUI);
  notificationService.setSendEmailDialogVisible(updateUI);
  controller.on("scaleModeChanged", (scaleMode) => {
    commands.toggleZoomMode.setChecked(scaleMode == RCV__namespace.ScaleMode.FitPage || scaleMode === RCV__namespace.ScaleMode.FitPageWidth);
  }).on("currentPageChanged", updateUI).onAsync("beforeLoadReport", async () => {
    setState(stateFlags.RenderInProgress, true);
    updateUI();
  }).on("reportLoadProgress", updateUI).onAsync("reportLoadComplete", async () => {
    setState(stateFlags.RenderInProgress, false);
    updateUI();
  }).on("reportSourceChanged", updateUI).on("viewModeChanged", updateUI).on("pageModeChanged", updateUI).on("setUIState", (args) => {
    setState(stateFlags[args.operationName], args.inProgress);
    updateUI();
  }).on("error", () => {
    setState(stateFlags.ExportInProgress, false);
    setState(stateFlags.PrintInProgress, false);
    setState(stateFlags.RenderInProgress, false);
    updateUI();
  }).on("renderingStopped", () => {
    setState(stateFlags.RenderInProgress, false);
    updateUI();
  });
  updateUI();
}

exports.UIController = UIController;
