"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../browser/dom.js");
const keyboardEvent = require("../../../browser/keyboardEvent.js");
const actionbar = require("../../../browser/ui/actionbar/actionbar.js");
const iconLabel = require("../../../browser/ui/iconLabel/iconLabel.js");
const keybindingLabel = require("../../../browser/ui/keybindingLabel/keybindingLabel.js");
const actions = require("../../../common/actions.js");
const arrays = require("../../../common/arrays.js");
const codicons = require("../../../common/codicons.js");
const comparers = require("../../../common/comparers.js");
const decorators = require("../../../common/decorators.js");
const event = require("../../../common/event.js");
const iconLabels = require("../../../common/iconLabels.js");
const lifecycle = require("../../../common/lifecycle.js");
const platform = require("../../../common/platform.js");
const types = require("../../../common/types.js");
const quickInputUtils = require("./quickInputUtils.js");
require("./media/quickInput.css.js");
const nls = require("../../../../nls.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators2, 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(decorators2, target, key, desc);
  else
    for (var i = decorators2.length - 1; i >= 0; i--)
      if (d = decorators2[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 __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function(resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function(resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
const $ = dom.$;
class ListElement {
  constructor(init) {
    this.hidden = false;
    this._onChecked = new event.Emitter();
    this.onChecked = this._onChecked.event;
    Object.assign(this, init);
  }
  get checked() {
    return !!this._checked;
  }
  set checked(value) {
    if (value !== this._checked) {
      this._checked = value;
      this._onChecked.fire(value);
    }
  }
  dispose() {
    this._onChecked.dispose();
  }
}
class ListElementRenderer {
  get templateId() {
    return ListElementRenderer.ID;
  }
  renderTemplate(container) {
    const data = /* @__PURE__ */ Object.create(null);
    data.toDisposeElement = [];
    data.toDisposeTemplate = [];
    data.entry = dom.append(container, $(".quick-input-list-entry"));
    const label = dom.append(data.entry, $("label.quick-input-list-label"));
    data.toDisposeTemplate.push(dom.addStandardDisposableListener(label, dom.EventType.CLICK, (e) => {
      if (!data.checkbox.offsetParent) {
        e.preventDefault();
      }
    }));
    data.checkbox = dom.append(label, $("input.quick-input-list-checkbox"));
    data.checkbox.type = "checkbox";
    data.toDisposeTemplate.push(dom.addStandardDisposableListener(data.checkbox, dom.EventType.CHANGE, (e) => {
      data.element.checked = data.checkbox.checked;
    }));
    const rows = dom.append(label, $(".quick-input-list-rows"));
    const row1 = dom.append(rows, $(".quick-input-list-row"));
    const row2 = dom.append(rows, $(".quick-input-list-row"));
    data.label = new iconLabel.IconLabel(row1, { supportHighlights: true, supportDescriptionHighlights: true, supportIcons: true });
    const keybindingContainer = dom.append(row1, $(".quick-input-list-entry-keybinding"));
    data.keybinding = new keybindingLabel.KeybindingLabel(keybindingContainer, platform.OS);
    const detailContainer = dom.append(row2, $(".quick-input-list-label-meta"));
    data.detail = new iconLabel.IconLabel(detailContainer, { supportHighlights: true, supportIcons: true });
    data.separator = dom.append(data.entry, $(".quick-input-list-separator"));
    data.actionBar = new actionbar.ActionBar(data.entry);
    data.actionBar.domNode.classList.add("quick-input-list-entry-action-bar");
    data.toDisposeTemplate.push(data.actionBar);
    return data;
  }
  renderElement(element, index, data) {
    data.toDisposeElement = lifecycle.dispose(data.toDisposeElement);
    data.element = element;
    data.checkbox.checked = element.checked;
    data.toDisposeElement.push(element.onChecked((checked) => data.checkbox.checked = checked));
    const { labelHighlights, descriptionHighlights, detailHighlights } = element;
    const options = /* @__PURE__ */ Object.create(null);
    options.matches = labelHighlights || [];
    options.descriptionTitle = element.saneDescription;
    options.descriptionMatches = descriptionHighlights || [];
    options.extraClasses = element.item.iconClasses;
    options.italic = element.item.italic;
    options.strikethrough = element.item.strikethrough;
    data.label.setLabel(element.saneLabel, element.saneDescription, options);
    data.keybinding.set(element.item.keybinding);
    if (element.saneDetail) {
      data.detail.setLabel(element.saneDetail, void 0, {
        matches: detailHighlights,
        title: element.saneDetail
      });
    }
    if (element.separator && element.separator.label) {
      data.separator.textContent = element.separator.label;
      data.separator.style.display = "";
    } else {
      data.separator.style.display = "none";
    }
    data.entry.classList.toggle("quick-input-list-separator-border", !!element.separator);
    data.actionBar.clear();
    const buttons = element.item.buttons;
    if (buttons && buttons.length) {
      data.actionBar.push(buttons.map((button, index2) => {
        let cssClasses = button.iconClass || (button.iconPath ? quickInputUtils.getIconClass(button.iconPath) : void 0);
        if (button.alwaysVisible) {
          cssClasses = cssClasses ? `${cssClasses} always-visible` : "always-visible";
        }
        const action = new actions.Action(`id-${index2}`, "", cssClasses, true, () => __awaiter(this, void 0, void 0, function* () {
          element.fireButtonTriggered({
            button,
            item: element.item
          });
        }));
        action.tooltip = button.tooltip || "";
        return action;
      }), { icon: true, label: false });
      data.entry.classList.add("has-actions");
    } else {
      data.entry.classList.remove("has-actions");
    }
  }
  disposeElement(element, index, data) {
    data.toDisposeElement = lifecycle.dispose(data.toDisposeElement);
  }
  disposeTemplate(data) {
    data.toDisposeElement = lifecycle.dispose(data.toDisposeElement);
    data.toDisposeTemplate = lifecycle.dispose(data.toDisposeTemplate);
  }
}
ListElementRenderer.ID = "listelement";
class ListElementDelegate {
  getHeight(element) {
    return element.saneDetail ? 44 : 22;
  }
  getTemplateId(element) {
    return ListElementRenderer.ID;
  }
}
exports.QuickInputListFocus = void 0;
(function(QuickInputListFocus) {
  QuickInputListFocus[QuickInputListFocus["First"] = 1] = "First";
  QuickInputListFocus[QuickInputListFocus["Second"] = 2] = "Second";
  QuickInputListFocus[QuickInputListFocus["Last"] = 3] = "Last";
  QuickInputListFocus[QuickInputListFocus["Next"] = 4] = "Next";
  QuickInputListFocus[QuickInputListFocus["Previous"] = 5] = "Previous";
  QuickInputListFocus[QuickInputListFocus["NextPage"] = 6] = "NextPage";
  QuickInputListFocus[QuickInputListFocus["PreviousPage"] = 7] = "PreviousPage";
})(exports.QuickInputListFocus || (exports.QuickInputListFocus = {}));
class QuickInputList {
  constructor(parent, id, options) {
    this.parent = parent;
    this.inputElements = [];
    this.elements = [];
    this.elementsToIndexes = /* @__PURE__ */ new Map();
    this.matchOnDescription = false;
    this.matchOnDetail = false;
    this.matchOnLabel = true;
    this.matchOnMeta = true;
    this.sortByLabel = true;
    this._onChangedAllVisibleChecked = new event.Emitter();
    this.onChangedAllVisibleChecked = this._onChangedAllVisibleChecked.event;
    this._onChangedCheckedCount = new event.Emitter();
    this.onChangedCheckedCount = this._onChangedCheckedCount.event;
    this._onChangedVisibleCount = new event.Emitter();
    this.onChangedVisibleCount = this._onChangedVisibleCount.event;
    this._onChangedCheckedElements = new event.Emitter();
    this.onChangedCheckedElements = this._onChangedCheckedElements.event;
    this._onButtonTriggered = new event.Emitter();
    this.onButtonTriggered = this._onButtonTriggered.event;
    this._onKeyDown = new event.Emitter();
    this.onKeyDown = this._onKeyDown.event;
    this._onLeave = new event.Emitter();
    this.onLeave = this._onLeave.event;
    this._fireCheckedEvents = true;
    this.elementDisposables = [];
    this.disposables = [];
    this.id = id;
    this.container = dom.append(this.parent, $(".quick-input-list"));
    const delegate = new ListElementDelegate();
    const accessibilityProvider = new QuickInputAccessibilityProvider();
    this.list = options.createList("QuickInput", this.container, delegate, [new ListElementRenderer()], {
      identityProvider: { getId: (element) => element.saneLabel },
      setRowLineHeight: false,
      multipleSelectionSupport: false,
      horizontalScrolling: false,
      accessibilityProvider
    });
    this.list.getHTMLElement().id = id;
    this.disposables.push(this.list);
    this.disposables.push(this.list.onKeyDown((e) => {
      const event2 = new keyboardEvent.StandardKeyboardEvent(e);
      switch (event2.keyCode) {
        case 10:
          this.toggleCheckbox();
          break;
        case 31:
          if (platform.isMacintosh ? e.metaKey : e.ctrlKey) {
            this.list.setFocus(arrays.range(this.list.length));
          }
          break;
        case 16: {
          const focus1 = this.list.getFocus();
          if (focus1.length === 1 && focus1[0] === 0) {
            this._onLeave.fire();
          }
          break;
        }
        case 18: {
          const focus2 = this.list.getFocus();
          if (focus2.length === 1 && focus2[0] === this.list.length - 1) {
            this._onLeave.fire();
          }
          break;
        }
      }
      this._onKeyDown.fire(event2);
    }));
    this.disposables.push(this.list.onMouseDown((e) => {
      if (e.browserEvent.button !== 2) {
        e.browserEvent.preventDefault();
      }
    }));
    this.disposables.push(dom.addDisposableListener(this.container, dom.EventType.CLICK, (e) => {
      if (e.x || e.y) {
        this._onLeave.fire();
      }
    }));
    this.disposables.push(this.list.onMouseMiddleClick((e) => {
      this._onLeave.fire();
    }));
    this.disposables.push(this.list.onContextMenu((e) => {
      if (typeof e.index === "number") {
        e.browserEvent.preventDefault();
        this.list.setSelection([e.index]);
      }
    }));
    this.disposables.push(this._onChangedAllVisibleChecked, this._onChangedCheckedCount, this._onChangedVisibleCount, this._onChangedCheckedElements, this._onButtonTriggered, this._onLeave, this._onKeyDown);
  }
  get onDidChangeFocus() {
    return event.Event.map(this.list.onDidChangeFocus, (e) => e.elements.map((e2) => e2.item));
  }
  get onDidChangeSelection() {
    return event.Event.map(this.list.onDidChangeSelection, (e) => ({ items: e.elements.map((e2) => e2.item), event: e.browserEvent }));
  }
  get scrollTop() {
    return this.list.scrollTop;
  }
  set scrollTop(scrollTop) {
    this.list.scrollTop = scrollTop;
  }
  getAllVisibleChecked() {
    return this.allVisibleChecked(this.elements, false);
  }
  allVisibleChecked(elements, whenNoneVisible = true) {
    for (let i = 0, n = elements.length; i < n; i++) {
      const element = elements[i];
      if (!element.hidden) {
        if (!element.checked) {
          return false;
        } else {
          whenNoneVisible = true;
        }
      }
    }
    return whenNoneVisible;
  }
  getCheckedCount() {
    let count = 0;
    const elements = this.elements;
    for (let i = 0, n = elements.length; i < n; i++) {
      if (elements[i].checked) {
        count++;
      }
    }
    return count;
  }
  getVisibleCount() {
    let count = 0;
    const elements = this.elements;
    for (let i = 0, n = elements.length; i < n; i++) {
      if (!elements[i].hidden) {
        count++;
      }
    }
    return count;
  }
  setAllVisibleChecked(checked) {
    try {
      this._fireCheckedEvents = false;
      this.elements.forEach((element) => {
        if (!element.hidden) {
          element.checked = checked;
        }
      });
    } finally {
      this._fireCheckedEvents = true;
      this.fireCheckedEvents();
    }
  }
  setElements(inputElements) {
    this.elementDisposables = lifecycle.dispose(this.elementDisposables);
    const fireButtonTriggered = (event2) => this.fireButtonTriggered(event2);
    this.inputElements = inputElements;
    this.elements = inputElements.reduce((result, item, index) => {
      var _a, _b, _c;
      if (item.type !== "separator") {
        const previous = index && inputElements[index - 1];
        const saneLabel = item.label && item.label.replace(/\r?\n/g, " ");
        const saneMeta = item.meta && item.meta.replace(/\r?\n/g, " ");
        const saneDescription = item.description && item.description.replace(/\r?\n/g, " ");
        const saneDetail = item.detail && item.detail.replace(/\r?\n/g, " ");
        const saneAriaLabel = item.ariaLabel || [saneLabel, saneDescription, saneDetail].map((s) => codicons.getCodiconAriaLabel(s)).filter((s) => !!s).join(", ");
        const hasCheckbox = this.parent.classList.contains("show-checkboxes");
        result.push(new ListElement({
          hasCheckbox,
          index,
          item,
          saneLabel,
          saneMeta,
          saneAriaLabel,
          saneDescription,
          saneDetail,
          labelHighlights: (_a = item.highlights) === null || _a === void 0 ? void 0 : _a.label,
          descriptionHighlights: (_b = item.highlights) === null || _b === void 0 ? void 0 : _b.description,
          detailHighlights: (_c = item.highlights) === null || _c === void 0 ? void 0 : _c.detail,
          checked: false,
          separator: previous && previous.type === "separator" ? previous : void 0,
          fireButtonTriggered
        }));
      }
      return result;
    }, []);
    this.elementDisposables.push(...this.elements);
    this.elementDisposables.push(...this.elements.map((element) => element.onChecked(() => this.fireCheckedEvents())));
    this.elementsToIndexes = this.elements.reduce((map, element, index) => {
      map.set(element.item, index);
      return map;
    }, /* @__PURE__ */ new Map());
    this.list.splice(0, this.list.length);
    this.list.splice(0, this.list.length, this.elements);
    this._onChangedVisibleCount.fire(this.elements.length);
  }
  getFocusedElements() {
    return this.list.getFocusedElements().map((e) => e.item);
  }
  setFocusedElements(items) {
    this.list.setFocus(items.filter((item) => this.elementsToIndexes.has(item)).map((item) => this.elementsToIndexes.get(item)));
    if (items.length > 0) {
      const focused = this.list.getFocus()[0];
      if (typeof focused === "number") {
        this.list.reveal(focused);
      }
    }
  }
  getActiveDescendant() {
    return this.list.getHTMLElement().getAttribute("aria-activedescendant");
  }
  setSelectedElements(items) {
    this.list.setSelection(items.filter((item) => this.elementsToIndexes.has(item)).map((item) => this.elementsToIndexes.get(item)));
  }
  getCheckedElements() {
    return this.elements.filter((e) => e.checked).map((e) => e.item);
  }
  setCheckedElements(items) {
    try {
      this._fireCheckedEvents = false;
      const checked = /* @__PURE__ */ new Set();
      for (const item of items) {
        checked.add(item);
      }
      for (const element of this.elements) {
        element.checked = checked.has(element.item);
      }
    } finally {
      this._fireCheckedEvents = true;
      this.fireCheckedEvents();
    }
  }
  set enabled(value) {
    this.list.getHTMLElement().style.pointerEvents = value ? "" : "none";
  }
  focus(what) {
    if (!this.list.length) {
      return;
    }
    if (what === exports.QuickInputListFocus.Next && this.list.getFocus()[0] === this.list.length - 1) {
      what = exports.QuickInputListFocus.First;
    }
    if (what === exports.QuickInputListFocus.Previous && this.list.getFocus()[0] === 0) {
      what = exports.QuickInputListFocus.Last;
    }
    if (what === exports.QuickInputListFocus.Second && this.list.length < 2) {
      what = exports.QuickInputListFocus.First;
    }
    switch (what) {
      case exports.QuickInputListFocus.First:
        this.list.focusFirst();
        break;
      case exports.QuickInputListFocus.Second:
        this.list.focusNth(1);
        break;
      case exports.QuickInputListFocus.Last:
        this.list.focusLast();
        break;
      case exports.QuickInputListFocus.Next:
        this.list.focusNext();
        break;
      case exports.QuickInputListFocus.Previous:
        this.list.focusPrevious();
        break;
      case exports.QuickInputListFocus.NextPage:
        this.list.focusNextPage();
        break;
      case exports.QuickInputListFocus.PreviousPage:
        this.list.focusPreviousPage();
        break;
    }
    const focused = this.list.getFocus()[0];
    if (typeof focused === "number") {
      this.list.reveal(focused);
    }
  }
  clearFocus() {
    this.list.setFocus([]);
  }
  domFocus() {
    this.list.domFocus();
  }
  layout(maxHeight) {
    this.list.getHTMLElement().style.maxHeight = maxHeight ? `calc(${Math.floor(maxHeight / 44) * 44}px)` : "";
    this.list.layout();
  }
  filter(query) {
    if (!(this.sortByLabel || this.matchOnLabel || this.matchOnDescription || this.matchOnDetail)) {
      this.list.layout();
      return false;
    }
    query = query.trim();
    if (!query || !(this.matchOnLabel || this.matchOnDescription || this.matchOnDetail)) {
      this.elements.forEach((element) => {
        element.labelHighlights = void 0;
        element.descriptionHighlights = void 0;
        element.detailHighlights = void 0;
        element.hidden = false;
        const previous = element.index && this.inputElements[element.index - 1];
        element.separator = previous && previous.type === "separator" ? previous : void 0;
      });
    } else {
      let currentSeparator;
      this.elements.forEach((element) => {
        const labelHighlights = this.matchOnLabel ? types.withNullAsUndefined(iconLabels.matchesFuzzyIconAware(query, iconLabels.parseLabelWithIcons(element.saneLabel))) : void 0;
        const descriptionHighlights = this.matchOnDescription ? types.withNullAsUndefined(iconLabels.matchesFuzzyIconAware(query, iconLabels.parseLabelWithIcons(element.saneDescription || ""))) : void 0;
        const detailHighlights = this.matchOnDetail ? types.withNullAsUndefined(iconLabels.matchesFuzzyIconAware(query, iconLabels.parseLabelWithIcons(element.saneDetail || ""))) : void 0;
        const metaHighlights = this.matchOnMeta ? types.withNullAsUndefined(iconLabels.matchesFuzzyIconAware(query, iconLabels.parseLabelWithIcons(element.saneMeta || ""))) : void 0;
        if (labelHighlights || descriptionHighlights || detailHighlights || metaHighlights) {
          element.labelHighlights = labelHighlights;
          element.descriptionHighlights = descriptionHighlights;
          element.detailHighlights = detailHighlights;
          element.hidden = false;
        } else {
          element.labelHighlights = void 0;
          element.descriptionHighlights = void 0;
          element.detailHighlights = void 0;
          element.hidden = !element.item.alwaysShow;
        }
        element.separator = void 0;
        if (!this.sortByLabel) {
          const previous = element.index && this.inputElements[element.index - 1];
          currentSeparator = previous && previous.type === "separator" ? previous : currentSeparator;
          if (currentSeparator && !element.hidden) {
            element.separator = currentSeparator;
            currentSeparator = void 0;
          }
        }
      });
    }
    const shownElements = this.elements.filter((element) => !element.hidden);
    if (this.sortByLabel && query) {
      const normalizedSearchValue = query.toLowerCase();
      shownElements.sort((a, b) => {
        return compareEntries(a, b, normalizedSearchValue);
      });
    }
    this.elementsToIndexes = shownElements.reduce((map, element, index) => {
      map.set(element.item, index);
      return map;
    }, /* @__PURE__ */ new Map());
    this.list.splice(0, this.list.length, shownElements);
    this.list.setFocus([]);
    this.list.layout();
    this._onChangedAllVisibleChecked.fire(this.getAllVisibleChecked());
    this._onChangedVisibleCount.fire(shownElements.length);
    return true;
  }
  toggleCheckbox() {
    try {
      this._fireCheckedEvents = false;
      const elements = this.list.getFocusedElements();
      const allChecked = this.allVisibleChecked(elements);
      for (const element of elements) {
        element.checked = !allChecked;
      }
    } finally {
      this._fireCheckedEvents = true;
      this.fireCheckedEvents();
    }
  }
  display(display) {
    this.container.style.display = display ? "" : "none";
  }
  isDisplayed() {
    return this.container.style.display !== "none";
  }
  dispose() {
    this.elementDisposables = lifecycle.dispose(this.elementDisposables);
    this.disposables = lifecycle.dispose(this.disposables);
  }
  fireCheckedEvents() {
    if (this._fireCheckedEvents) {
      this._onChangedAllVisibleChecked.fire(this.getAllVisibleChecked());
      this._onChangedCheckedCount.fire(this.getCheckedCount());
      this._onChangedCheckedElements.fire(this.getCheckedElements());
    }
  }
  fireButtonTriggered(event2) {
    this._onButtonTriggered.fire(event2);
  }
  style(styles) {
    this.list.style(styles);
  }
}
__decorate([
  decorators.memoize
], QuickInputList.prototype, "onDidChangeFocus", null);
__decorate([
  decorators.memoize
], QuickInputList.prototype, "onDidChangeSelection", null);
function compareEntries(elementA, elementB, lookFor) {
  const labelHighlightsA = elementA.labelHighlights || [];
  const labelHighlightsB = elementB.labelHighlights || [];
  if (labelHighlightsA.length && !labelHighlightsB.length) {
    return -1;
  }
  if (!labelHighlightsA.length && labelHighlightsB.length) {
    return 1;
  }
  if (labelHighlightsA.length === 0 && labelHighlightsB.length === 0) {
    return 0;
  }
  return comparers.compareAnything(elementA.saneLabel, elementB.saneLabel, lookFor);
}
class QuickInputAccessibilityProvider {
  getWidgetAriaLabel() {
    return nls.localize("quickInput", "Quick Input");
  }
  getAriaLabel(element) {
    return element.saneAriaLabel;
  }
  getWidgetRole() {
    return "listbox";
  }
  getRole(element) {
    return element.hasCheckbox ? "checkbox" : "option";
  }
  isChecked(element) {
    if (!element.hasCheckbox) {
      return void 0;
    }
    return {
      value: element.checked,
      onDidChange: element.onChecked
    };
  }
}
exports.QuickInputList = QuickInputList;
