import { append, $ } from "../../../../../base/browser/dom.mjs";
import { CountBadge } from "../../../../../base/browser/ui/countBadge/countBadge.mjs";
import { HighlightedLabel } from "../../../../../base/browser/ui/highlightedlabel/highlightedLabel.mjs";
import { IconLabel } from "../../../../../base/browser/ui/iconLabel/iconLabel.mjs";
import { createMatches, FuzzyScore } from "../../../../../base/common/filters.mjs";
import { getBaseLabel } from "../../../../../base/common/labels.mjs";
import { Disposable } from "../../../../../base/common/lifecycle.mjs";
import { basename, dirname } from "../../../../../base/common/resources.mjs";
import { ITextModelService } from "../../../../common/services/resolverService.mjs";
import { localize } from "../../../../../nls.mjs";
import { IInstantiationService } from "../../../../../platform/instantiation/common/instantiation.mjs";
import { IKeybindingService } from "../../../../../platform/keybinding/common/keybinding.mjs";
import { ILabelService } from "../../../../../platform/label/common/label.mjs";
import { attachBadgeStyler } from "../../../../../platform/theme/common/styler.mjs";
import { IThemeService } from "../../../../../platform/theme/common/themeService.mjs";
import { ReferencesModel, FileReferences, OneReference } from "../referencesModel.mjs";
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);
  };
};
let DataSource = class DataSource2 {
  constructor(_resolverService) {
    this._resolverService = _resolverService;
  }
  hasChildren(element) {
    if (element instanceof ReferencesModel) {
      return true;
    }
    if (element instanceof FileReferences) {
      return true;
    }
    return false;
  }
  getChildren(element) {
    if (element instanceof ReferencesModel) {
      return element.groups;
    }
    if (element instanceof FileReferences) {
      return element.resolve(this._resolverService).then((val) => {
        return val.children;
      });
    }
    throw new Error("bad tree");
  }
};
DataSource = __decorate([
  __param(0, ITextModelService)
], DataSource);
class Delegate {
  getHeight() {
    return 23;
  }
  getTemplateId(element) {
    if (element instanceof FileReferences) {
      return FileReferencesRenderer.id;
    } else {
      return OneReferenceRenderer.id;
    }
  }
}
let StringRepresentationProvider = class StringRepresentationProvider2 {
  constructor(_keybindingService) {
    this._keybindingService = _keybindingService;
  }
  getKeyboardNavigationLabel(element) {
    var _a;
    if (element instanceof OneReference) {
      const parts = (_a = element.parent.getPreview(element)) === null || _a === void 0 ? void 0 : _a.preview(element.range);
      if (parts) {
        return parts.value;
      }
    }
    return basename(element.uri);
  }
};
StringRepresentationProvider = __decorate([
  __param(0, IKeybindingService)
], StringRepresentationProvider);
class IdentityProvider {
  getId(element) {
    return element instanceof OneReference ? element.id : element.uri;
  }
}
let FileReferencesTemplate = class FileReferencesTemplate2 extends Disposable {
  constructor(container, _uriLabel, themeService) {
    super();
    this._uriLabel = _uriLabel;
    const parent = document.createElement("div");
    parent.classList.add("reference-file");
    this.file = this._register(new IconLabel(parent, { supportHighlights: true }));
    this.badge = new CountBadge(append(parent, $(".count")));
    this._register(attachBadgeStyler(this.badge, themeService));
    container.appendChild(parent);
  }
  set(element, matches) {
    let parent = dirname(element.uri);
    this.file.setLabel(getBaseLabel(element.uri), this._uriLabel.getUriLabel(parent, { relative: true }), { title: this._uriLabel.getUriLabel(element.uri), matches });
    const len = element.children.length;
    this.badge.setCount(len);
    if (len > 1) {
      this.badge.setTitleFormat(localize("referencesCount", "{0} references", len));
    } else {
      this.badge.setTitleFormat(localize("referenceCount", "{0} reference", len));
    }
  }
};
FileReferencesTemplate = __decorate([
  __param(1, ILabelService),
  __param(2, IThemeService)
], FileReferencesTemplate);
let FileReferencesRenderer = class FileReferencesRenderer2 {
  constructor(_instantiationService) {
    this._instantiationService = _instantiationService;
    this.templateId = FileReferencesRenderer2.id;
  }
  renderTemplate(container) {
    return this._instantiationService.createInstance(FileReferencesTemplate, container);
  }
  renderElement(node, index, template) {
    template.set(node.element, createMatches(node.filterData));
  }
  disposeTemplate(templateData) {
    templateData.dispose();
  }
};
FileReferencesRenderer.id = "FileReferencesRenderer";
FileReferencesRenderer = __decorate([
  __param(0, IInstantiationService)
], FileReferencesRenderer);
class OneReferenceTemplate {
  constructor(container) {
    this.label = new HighlightedLabel(container);
  }
  set(element, score) {
    var _a;
    const preview = (_a = element.parent.getPreview(element)) === null || _a === void 0 ? void 0 : _a.preview(element.range);
    if (!preview || !preview.value) {
      this.label.set(`${basename(element.uri)}:${element.range.startLineNumber + 1}:${element.range.startColumn + 1}`);
    } else {
      const { value, highlight } = preview;
      if (score && !FuzzyScore.isDefault(score)) {
        this.label.element.classList.toggle("referenceMatch", false);
        this.label.set(value, createMatches(score));
      } else {
        this.label.element.classList.toggle("referenceMatch", true);
        this.label.set(value, [highlight]);
      }
    }
  }
}
class OneReferenceRenderer {
  constructor() {
    this.templateId = OneReferenceRenderer.id;
  }
  renderTemplate(container) {
    return new OneReferenceTemplate(container);
  }
  renderElement(node, index, templateData) {
    templateData.set(node.element, node.filterData);
  }
  disposeTemplate() {
  }
}
OneReferenceRenderer.id = "OneReferenceRenderer";
class AccessibilityProvider {
  getWidgetAriaLabel() {
    return localize("treeAriaLabel", "References");
  }
  getAriaLabel(element) {
    return element.ariaMessage;
  }
}
export {
  AccessibilityProvider,
  DataSource,
  Delegate,
  FileReferencesRenderer,
  IdentityProvider,
  OneReferenceRenderer,
  StringRepresentationProvider
};
