import { append, clearNode, $ as $$1 } from "../../dom.mjs";
import { UILabelProvider } from "../../../common/keybindingLabels.mjs";
import { equals } from "../../../common/objects.mjs";
import "./keybindingLabel.css.mjs";
import { localize } from "../../../../nls.mjs";
const $ = $$1;
class KeybindingLabel {
  constructor(container, os, options) {
    this.os = os;
    this.keyElements = /* @__PURE__ */ new Set();
    this.options = options || /* @__PURE__ */ Object.create(null);
    this.labelBackground = this.options.keybindingLabelBackground;
    this.labelForeground = this.options.keybindingLabelForeground;
    this.labelBorder = this.options.keybindingLabelBorder;
    this.labelBottomBorder = this.options.keybindingLabelBottomBorder;
    this.labelShadow = this.options.keybindingLabelShadow;
    this.domNode = append(container, $(".monaco-keybinding"));
    this.didEverRender = false;
    container.appendChild(this.domNode);
  }
  get element() {
    return this.domNode;
  }
  set(keybinding, matches) {
    if (this.didEverRender && this.keybinding === keybinding && KeybindingLabel.areSame(this.matches, matches)) {
      return;
    }
    this.keybinding = keybinding;
    this.matches = matches;
    this.render();
  }
  render() {
    this.clear();
    if (this.keybinding) {
      let [firstPart, chordPart] = this.keybinding.getParts();
      if (firstPart) {
        this.renderPart(this.domNode, firstPart, this.matches ? this.matches.firstPart : null);
      }
      if (chordPart) {
        append(this.domNode, $("span.monaco-keybinding-key-chord-separator", void 0, " "));
        this.renderPart(this.domNode, chordPart, this.matches ? this.matches.chordPart : null);
      }
      this.domNode.title = this.keybinding.getAriaLabel() || "";
    } else if (this.options && this.options.renderUnboundKeybindings) {
      this.renderUnbound(this.domNode);
    }
    this.applyStyles();
    this.didEverRender = true;
  }
  clear() {
    clearNode(this.domNode);
    this.keyElements.clear();
  }
  renderPart(parent, part, match) {
    const modifierLabels = UILabelProvider.modifierLabels[this.os];
    if (part.ctrlKey) {
      this.renderKey(parent, modifierLabels.ctrlKey, Boolean(match === null || match === void 0 ? void 0 : match.ctrlKey), modifierLabels.separator);
    }
    if (part.shiftKey) {
      this.renderKey(parent, modifierLabels.shiftKey, Boolean(match === null || match === void 0 ? void 0 : match.shiftKey), modifierLabels.separator);
    }
    if (part.altKey) {
      this.renderKey(parent, modifierLabels.altKey, Boolean(match === null || match === void 0 ? void 0 : match.altKey), modifierLabels.separator);
    }
    if (part.metaKey) {
      this.renderKey(parent, modifierLabels.metaKey, Boolean(match === null || match === void 0 ? void 0 : match.metaKey), modifierLabels.separator);
    }
    const keyLabel = part.keyLabel;
    if (keyLabel) {
      this.renderKey(parent, keyLabel, Boolean(match === null || match === void 0 ? void 0 : match.keyCode), "");
    }
  }
  renderKey(parent, label, highlight, separator) {
    append(parent, this.createKeyElement(label, highlight ? ".highlight" : ""));
    if (separator) {
      append(parent, $("span.monaco-keybinding-key-separator", void 0, separator));
    }
  }
  renderUnbound(parent) {
    append(parent, this.createKeyElement(localize("unbound", "Unbound")));
  }
  createKeyElement(label, extraClass = "") {
    const keyElement = $("span.monaco-keybinding-key" + extraClass, void 0, label);
    this.keyElements.add(keyElement);
    return keyElement;
  }
  style(styles) {
    this.labelBackground = styles.keybindingLabelBackground;
    this.labelForeground = styles.keybindingLabelForeground;
    this.labelBorder = styles.keybindingLabelBorder;
    this.labelBottomBorder = styles.keybindingLabelBottomBorder;
    this.labelShadow = styles.keybindingLabelShadow;
    this.applyStyles();
  }
  applyStyles() {
    var _a;
    if (this.element) {
      for (const keyElement of this.keyElements) {
        if (this.labelBackground) {
          keyElement.style.backgroundColor = (_a = this.labelBackground) === null || _a === void 0 ? void 0 : _a.toString();
        }
        if (this.labelBorder) {
          keyElement.style.borderColor = this.labelBorder.toString();
        }
        if (this.labelBottomBorder) {
          keyElement.style.borderBottomColor = this.labelBottomBorder.toString();
        }
        if (this.labelShadow) {
          keyElement.style.boxShadow = `inset 0 -1px 0 ${this.labelShadow}`;
        }
      }
      if (this.labelForeground) {
        this.element.style.color = this.labelForeground.toString();
      }
    }
  }
  static areSame(a, b) {
    if (a === b || !a && !b) {
      return true;
    }
    return !!a && !!b && equals(a.firstPart, b.firstPart) && equals(a.chordPart, b.chordPart);
  }
}
export {
  KeybindingLabel
};
