'use strict';

var globalSettings = require('./globalSettings.js');

class Binder {
  static bind($element, ...args) {
    const commands = args[0].commands;
    const viewerOptions = args[1];
    Binder.attachCommands($element, commands, viewerOptions);
    var plugins = $element.find('[data-role^="telerik_ReportViewer_"]');
    Array.from(plugins).forEach((element) => {
      var $element2 = $(element);
      var fn = $.fn[$element2.attr("data-role")];
      if (typeof fn === "function") {
        fn.apply($element2, args);
      }
    });
  }
  static attachCommands($element, commands, viewerOptions) {
    var elementSelector = '[data-command^="telerik_ReportViewer_"]';
    var customElementSelector = "[data-target-report-viewer]" + elementSelector;
    $element.on("click", elementSelector, commandHandler);
    if (!globalSettings.GlobalSettings.CommandHandlerAttached) {
      $(document.body).on("click", customElementSelector, customCommandHandler);
      globalSettings.GlobalSettings.CommandHandlerAttached = true;
    }
    Object.entries(commands).forEach(([key, command]) => {
      attachCommand(key, command, viewerOptions, $element);
    });
    function commandHandler(event) {
      var prefixedDataCommand = $(event.currentTarget).attr("data-command");
      if (prefixedDataCommand) {
        var dataCommand = prefixedDataCommand.substring("telerik_ReportViewer_".length);
        var cmd = commands[dataCommand];
        if (cmd && cmd.getEnabled()) {
          cmd.exec($(event.currentTarget).attr("data-command-parameter"));
        }
        event.preventDefault();
      }
    }
    function customCommandHandler(event) {
      var $this = $(event.currentTarget);
      var prefixedDataCommand = $this.attr("data-command");
      var reportViewerTarget = $this.attr("data-target-report-viewer");
      if (prefixedDataCommand && reportViewerTarget) {
        var dataCommand = prefixedDataCommand.substring("telerik_ReportViewer_".length);
        var reportViewer = $(reportViewerTarget).data("telerik_ReportViewer");
        var cmd = reportViewer.commands[dataCommand];
        if (cmd.getEnabled()) {
          cmd.exec($(event.currentTarget).attr("data-command-parameter"));
        }
        event.preventDefault();
      }
    }
  }
}
function attachCommand(dataCommand, cmd, viewerOptions, $element) {
  if (cmd) {
    var elementSelector = '[data-command="telerik_ReportViewer_' + dataCommand + '"]';
    var customElementSelector = '[data-target-report-viewer="' + viewerOptions.selector + '"]' + elementSelector;
    var $defaultElement = $element.find(elementSelector);
    var $customElement = $(customElementSelector);
    $(cmd).on("enabledChanged", (event) => {
      (cmd.getEnabled() ? $.fn.removeClass : $.fn.addClass).call($defaultElement.parent("li"), "k-disabled");
      (cmd.getEnabled() ? $.fn.removeClass : $.fn.addClass).call($customElement, viewerOptions.disabledButtonClass);
    }).on("checkedChanged", (event) => {
      var defaultTarget = $defaultElement.parent("li");
      (cmd.getChecked() ? $.fn.addClass : $.fn.removeClass).call(defaultTarget, getSelectedClassName(defaultTarget));
      (cmd.getChecked() ? $.fn.addClass : $.fn.removeClass).call($customElement, viewerOptions.checkedButtonClass);
    });
  }
}
function getSelectedClassName($element) {
  return $element.hasClass("trv-menu-toggleable") ? "k-selected !k-bg-primary" : "k-selected";
}

exports.Binder = Binder;
