'use strict';

var baseComponent = require('../base-component.js');

class LinkButton extends baseComponent.BaseComponent {
  // #region fields
  cmd;
  // #endregion
  // #region constructor
  constructor(element, options) {
    super(element, options);
    var dataCommand = this.$element.attr("data-command");
    if (dataCommand) {
      this.cmd = this.options.commands[dataCommand];
    }
    if (this.cmd) {
      this.$element.on("click", (event) => {
        if (this.cmd.getEnabled()) {
          this.cmd.exec($(event.currentTarget).attr("data-command-parameter"));
        } else {
          event.preventDefault();
        }
      });
      $(this.cmd).on("enabledChanged", (event) => {
        (this.cmd.getEnabled() ? $.fn.removeClass : $.fn.addClass).call(this.$element, "disabled");
      }).on("checkedChanged", (event) => {
        (this.cmd.getChecked() ? $.fn.addClass : $.fn.removeClass).call(this.$element, "checked");
      });
    }
  }
  // #endregion
}

exports.LinkButton = LinkButton;
