"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../base/browser/dom.js");
const event = require("../../../base/browser/event.js");
const keyboardEvent = require("../../../base/browser/keyboardEvent.js");
const touch = require("../../../base/browser/touch.js");
const event$1 = require("../../../base/common/event.js");
const lifecycle = require("../../../base/common/lifecycle.js");
const opener = require("../common/opener.js");
const colorRegistry = require("../../theme/common/colorRegistry.js");
const themeService = require("../../theme/common/themeService.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
exports.Link = class Link extends lifecycle.Disposable {
  constructor(container, _link, options = {}, openerService) {
    var _a;
    super();
    this._link = _link;
    this._enabled = true;
    this.el = dom.append(container, dom.$("a.monaco-link", {
      tabIndex: (_a = _link.tabIndex) !== null && _a !== void 0 ? _a : 0,
      href: _link.href,
      title: _link.title
    }, _link.label));
    this.el.setAttribute("role", "button");
    const onClickEmitter = this._register(new event.DomEmitter(this.el, "click"));
    const onKeyPress = this._register(new event.DomEmitter(this.el, "keypress"));
    const onEnterPress = event$1.Event.chain(onKeyPress.event).map((e) => new keyboardEvent.StandardKeyboardEvent(e)).filter(
      (e) => e.keyCode === 3
      /* Enter */
    ).event;
    const onTap = this._register(new event.DomEmitter(this.el, touch.EventType.Tap)).event;
    this._register(touch.Gesture.addTarget(this.el));
    const onOpen = event$1.Event.any(onClickEmitter.event, onEnterPress, onTap);
    this._register(onOpen((e) => {
      if (!this.enabled) {
        return;
      }
      dom.EventHelper.stop(e, true);
      if (options === null || options === void 0 ? void 0 : options.opener) {
        options.opener(this._link.href);
      } else {
        openerService.open(this._link.href, { allowCommands: true });
      }
    }));
    this.enabled = true;
  }
  get enabled() {
    return this._enabled;
  }
  set enabled(enabled) {
    if (enabled) {
      this.el.setAttribute("aria-disabled", "false");
      this.el.tabIndex = 0;
      this.el.style.pointerEvents = "auto";
      this.el.style.opacity = "1";
      this.el.style.cursor = "pointer";
      this._enabled = false;
    } else {
      this.el.setAttribute("aria-disabled", "true");
      this.el.tabIndex = -1;
      this.el.style.pointerEvents = "none";
      this.el.style.opacity = "0.4";
      this.el.style.cursor = "default";
      this._enabled = true;
    }
    this._enabled = enabled;
  }
};
exports.Link = __decorate([
  __param(3, opener.IOpenerService)
], exports.Link);
themeService.registerThemingParticipant((theme, collector) => {
  const textLinkForegroundColor = theme.getColor(colorRegistry.textLinkForeground);
  if (textLinkForegroundColor) {
    collector.addRule(`.monaco-link { color: ${textLinkForegroundColor}; }`);
  }
  const textLinkActiveForegroundColor = theme.getColor(colorRegistry.textLinkActiveForeground);
  if (textLinkActiveForegroundColor) {
    collector.addRule(`.monaco-link:hover { color: ${textLinkActiveForegroundColor}; }`);
  }
});
