'use strict';

var command = require('./command.js');
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);

var scaleTransitionMap = {};
scaleTransitionMap[RCV__namespace.ScaleMode.FitPage] = RCV__namespace.ScaleMode.FitPageWidth;
scaleTransitionMap[RCV__namespace.ScaleMode.FitPageWidth] = RCV__namespace.ScaleMode.Specific;
scaleTransitionMap[RCV__namespace.ScaleMode.Specific] = RCV__namespace.ScaleMode.FitPage;
var scaleValues = [0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 4, 8];
function CommandSet(options) {
  var controller = options.controller;
  if (!controller) {
    throw "No options.controller.";
  }
  var notificationService = options.notificationService;
  if (!notificationService) {
    throw "No options.notificationService.";
  }
  var historyManager = options.history;
  if (!historyManager) {
    throw "No options.history.";
  }
  function getDocumentMapVisible() {
    var args = {};
    notificationService.getDocumentMapState(args);
    return Boolean(args.visible);
  }
  function getParametersAreaVisible() {
    var args = {};
    notificationService.getParametersAreaState(args);
    return Boolean(args.visible);
  }
  function getSearchDialogVisible() {
    return Boolean(controller.getSearchInitiated());
  }
  function getSendEmailDialogVisible() {
    var args = {};
    notificationService.getSendEmailDialogState(args);
    return Boolean(args.visible);
  }
  function getAiPromptDialogVisible() {
    return Boolean(controller.getAiPromptInitiated());
  }
  return {
    "historyBack": new command.Command(() => {
      historyManager.back();
    }),
    "historyForward": new command.Command(() => {
      historyManager.forward();
    }),
    "stopRendering": new command.Command(() => {
      controller.stopRendering();
    }),
    "goToPrevPage": new command.Command(() => {
      controller.navigateToPage(controller.getCurrentPageNumber() - 1);
    }),
    "goToNextPage": new command.Command(() => {
      controller.navigateToPage(controller.getCurrentPageNumber() + 1);
    }),
    "goToFirstPage": new command.Command(() => {
      controller.navigateToPage(1);
    }),
    "goToLastPage": new command.Command(() => {
      controller.navigateToPage(controller.getPageCount());
    }),
    "goToPage": new command.Command((pageNumber) => {
      if (!isNaN(pageNumber)) {
        var pageCount = controller.getPageCount();
        if (pageNumber > pageCount) {
          pageNumber = pageCount;
        } else if (pageNumber < 1) {
          pageNumber = 1;
        }
        controller.navigateToPage(pageNumber);
        return pageNumber;
      }
    }),
    "refresh": new command.Command(() => {
      controller.refreshReport(true, "", false);
    }),
    "export": new command.Command((format) => {
      if (format) {
        controller.exportReport(format);
      }
    }),
    "print": new command.Command(() => {
      controller.printReport();
    }),
    "pageMode": new command.Command(() => {
      controller.setPageMode(
        controller.getPageMode() === RCV__namespace.PageMode.SinglePage ? RCV__namespace.PageMode.ContinuousScroll : RCV__namespace.PageMode.SinglePage
      );
      controller.refreshReportCore(false, controller.getReportDocumentId());
    }),
    "togglePrintPreview": new command.Command(() => {
      controller.setViewMode(
        controller.getViewMode() === RCV__namespace.ViewMode.PrintPreview ? RCV__namespace.ViewMode.Interactive : RCV__namespace.ViewMode.PrintPreview
      );
      controller.refreshReportCore(false, controller.getReportDocumentId());
    }),
    "toggleDocumentMap": new command.Command(() => {
      notificationService.setDocumentMapVisible({ visible: !getDocumentMapVisible() });
    }),
    "toggleParametersArea": new command.Command(() => {
      notificationService.setParametersAreaVisible({ visible: !getParametersAreaVisible() });
    }),
    "zoom": new command.Command((scale) => {
      controller.setScaleMode(RCV__namespace.ScaleMode.Specific);
      controller.setScale(scale);
    }),
    "zoomIn": new command.Command(() => {
      zoom(1);
    }),
    "zoomOut": new command.Command(() => {
      zoom(-1);
    }),
    "toggleZoomMode": new command.Command((e) => {
      var scaleMode = controller.getScaleMode();
      var newScaleMode = scaleTransitionMap[scaleMode];
      controller.setScaleMode(newScaleMode);
      if (newScaleMode === RCV__namespace.ScaleMode.Specific) {
        controller.setScale(1);
      }
    }),
    "toggleSearchDialog": new command.Command(() => {
      notificationService.setSearchDialogVisible({ visible: !getSearchDialogVisible() });
    }),
    "toggleSendEmailDialog": new command.Command(() => {
      notificationService.setSendEmailDialogVisible({ visible: !getSendEmailDialogVisible() });
    }),
    "toggleAiPromptDialog": new command.Command(() => {
      notificationService.setAiPromptDialogVisible({ visible: !getAiPromptDialogVisible() });
    })
  };
  function zoom(step) {
    var scale = getZoomScale(controller.getScale(), step);
    controller.setScaleMode(RCV__namespace.ScaleMode.Specific);
    controller.setScale(scale);
  }
  function getZoomScale(scale, steps) {
    var pos = -1;
    var length = scaleValues.length;
    for (var i = 0; i < length; i++) {
      var value = scaleValues[i];
      if (scale < value) {
        pos = i - 0.5;
        break;
      }
      if (scale === value) {
        pos = i;
        break;
      }
    }
    pos = pos + steps;
    if (steps >= 0) {
      pos = Math.round(pos - 0.49);
    } else {
      pos = Math.round(pos + 0.49);
    }
    if (pos < 0) {
      pos = 0;
    } else if (pos > length - 1) {
      pos = length - 1;
    }
    return scaleValues[pos];
  }
}

exports.CommandSet = CommandSet;
