import { $, addStandardDisposableListener, append, clearNode, show, hide } from "../../../../base/browser/dom.mjs";
import { ActionBar } from "../../../../base/browser/ui/actionbar/actionbar.mjs";
import { Action } from "../../../../base/common/actions.mjs";
import { Codicon } from "../../../../base/common/codicons.mjs";
import { Color } from "../../../../base/common/color.mjs";
import { Emitter } from "../../../../base/common/event.mjs";
import { mixin } from "../../../../base/common/objects.mjs";
import "./media/peekViewWidget.css.mjs";
import { registerEditorContribution } from "../../../browser/editorExtensions.mjs";
import { ICodeEditorService } from "../../../browser/services/codeEditorService.mjs";
import { EmbeddedCodeEditorWidget } from "../../../browser/widget/embeddedCodeEditorWidget.mjs";
import { ZoneWidget } from "../../zoneWidget/browser/zoneWidget.mjs";
import { localize } from "../../../../nls.mjs";
import { createActionViewItem } from "../../../../platform/actions/browser/menuEntryActionViewItem.mjs";
import { RawContextKey, IContextKeyService } from "../../../../platform/contextkey/common/contextkey.mjs";
import { registerSingleton } from "../../../../platform/instantiation/common/extensions.mjs";
import { createDecorator, IInstantiationService } from "../../../../platform/instantiation/common/instantiation.mjs";
import { registerColor, transparent, editorInfoForeground, contrastBorder, activeContrastBorder } from "../../../../platform/theme/common/colorRegistry.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);
  };
};
const IPeekViewService = createDecorator("IPeekViewService");
registerSingleton(IPeekViewService, class {
  constructor() {
    this._widgets = /* @__PURE__ */ new Map();
  }
  addExclusiveWidget(editor, widget) {
    const existing = this._widgets.get(editor);
    if (existing) {
      existing.listener.dispose();
      existing.widget.dispose();
    }
    const remove = () => {
      const data = this._widgets.get(editor);
      if (data && data.widget === widget) {
        data.listener.dispose();
        this._widgets.delete(editor);
      }
    };
    this._widgets.set(editor, { widget, listener: widget.onDidClose(remove) });
  }
});
var PeekContext;
(function(PeekContext2) {
  PeekContext2.inPeekEditor = new RawContextKey("inReferenceSearchEditor", true, localize("inReferenceSearchEditor", "Whether the current code editor is embedded inside peek"));
  PeekContext2.notInPeekEditor = PeekContext2.inPeekEditor.toNegated();
})(PeekContext || (PeekContext = {}));
let PeekContextController = class PeekContextController2 {
  constructor(editor, contextKeyService) {
    if (editor instanceof EmbeddedCodeEditorWidget) {
      PeekContext.inPeekEditor.bindTo(contextKeyService);
    }
  }
  dispose() {
  }
};
PeekContextController.ID = "editor.contrib.referenceController";
PeekContextController = __decorate([
  __param(1, IContextKeyService)
], PeekContextController);
registerEditorContribution(PeekContextController.ID, PeekContextController);
function getOuterEditor(accessor) {
  let editor = accessor.get(ICodeEditorService).getFocusedCodeEditor();
  if (editor instanceof EmbeddedCodeEditorWidget) {
    return editor.getParentEditor();
  }
  return editor;
}
const defaultOptions = {
  headerBackgroundColor: Color.white,
  primaryHeadingColor: Color.fromHex("#333333"),
  secondaryHeadingColor: Color.fromHex("#6c6c6cb3")
};
let PeekViewWidget = class PeekViewWidget2 extends ZoneWidget {
  constructor(editor, options, instantiationService) {
    super(editor, options);
    this.instantiationService = instantiationService;
    this._onDidClose = new Emitter();
    this.onDidClose = this._onDidClose.event;
    mixin(this.options, defaultOptions, false);
  }
  dispose() {
    if (!this.disposed) {
      this.disposed = true;
      super.dispose();
      this._onDidClose.fire(this);
    }
  }
  style(styles) {
    let options = this.options;
    if (styles.headerBackgroundColor) {
      options.headerBackgroundColor = styles.headerBackgroundColor;
    }
    if (styles.primaryHeadingColor) {
      options.primaryHeadingColor = styles.primaryHeadingColor;
    }
    if (styles.secondaryHeadingColor) {
      options.secondaryHeadingColor = styles.secondaryHeadingColor;
    }
    super.style(styles);
  }
  _applyStyles() {
    super._applyStyles();
    let options = this.options;
    if (this._headElement && options.headerBackgroundColor) {
      this._headElement.style.backgroundColor = options.headerBackgroundColor.toString();
    }
    if (this._primaryHeading && options.primaryHeadingColor) {
      this._primaryHeading.style.color = options.primaryHeadingColor.toString();
    }
    if (this._secondaryHeading && options.secondaryHeadingColor) {
      this._secondaryHeading.style.color = options.secondaryHeadingColor.toString();
    }
    if (this._bodyElement && options.frameColor) {
      this._bodyElement.style.borderColor = options.frameColor.toString();
    }
  }
  _fillContainer(container) {
    this.setCssClass("peekview-widget");
    this._headElement = $(".head");
    this._bodyElement = $(".body");
    this._fillHead(this._headElement);
    this._fillBody(this._bodyElement);
    container.appendChild(this._headElement);
    container.appendChild(this._bodyElement);
  }
  _fillHead(container, noCloseAction) {
    const titleElement = $(".peekview-title");
    if (this.options.supportOnTitleClick) {
      titleElement.classList.add("clickable");
      addStandardDisposableListener(titleElement, "click", (event) => this._onTitleClick(event));
    }
    append(this._headElement, titleElement);
    this._fillTitleIcon(titleElement);
    this._primaryHeading = $("span.filename");
    this._secondaryHeading = $("span.dirname");
    this._metaHeading = $("span.meta");
    append(titleElement, this._primaryHeading, this._secondaryHeading, this._metaHeading);
    const actionsContainer = $(".peekview-actions");
    append(this._headElement, actionsContainer);
    const actionBarOptions = this._getActionBarOptions();
    this._actionbarWidget = new ActionBar(actionsContainer, actionBarOptions);
    this._disposables.add(this._actionbarWidget);
    if (!noCloseAction) {
      this._actionbarWidget.push(new Action("peekview.close", localize("label.close", "Close"), Codicon.close.classNames, true, () => {
        this.dispose();
        return Promise.resolve();
      }), { label: false, icon: true });
    }
  }
  _fillTitleIcon(container) {
  }
  _getActionBarOptions() {
    return {
      actionViewItemProvider: createActionViewItem.bind(void 0, this.instantiationService),
      orientation: 0
      /* HORIZONTAL */
    };
  }
  _onTitleClick(event) {
  }
  setTitle(primaryHeading, secondaryHeading) {
    if (this._primaryHeading && this._secondaryHeading) {
      this._primaryHeading.innerText = primaryHeading;
      this._primaryHeading.setAttribute("title", primaryHeading);
      if (secondaryHeading) {
        this._secondaryHeading.innerText = secondaryHeading;
      } else {
        clearNode(this._secondaryHeading);
      }
    }
  }
  setMetaTitle(value) {
    if (this._metaHeading) {
      if (value) {
        this._metaHeading.innerText = value;
        show(this._metaHeading);
      } else {
        hide(this._metaHeading);
      }
    }
  }
  _doLayout(heightInPixel, widthInPixel) {
    if (!this._isShowing && heightInPixel < 0) {
      this.dispose();
      return;
    }
    const headHeight = Math.ceil(this.editor.getOption(
      59
      /* lineHeight */
    ) * 1.2);
    const bodyHeight = Math.round(heightInPixel - (headHeight + 2));
    this._doLayoutHead(headHeight, widthInPixel);
    this._doLayoutBody(bodyHeight, widthInPixel);
  }
  _doLayoutHead(heightInPixel, widthInPixel) {
    if (this._headElement) {
      this._headElement.style.height = `${heightInPixel}px`;
      this._headElement.style.lineHeight = this._headElement.style.height;
    }
  }
  _doLayoutBody(heightInPixel, widthInPixel) {
    if (this._bodyElement) {
      this._bodyElement.style.height = `${heightInPixel}px`;
    }
  }
};
PeekViewWidget = __decorate([
  __param(2, IInstantiationService)
], PeekViewWidget);
const peekViewTitleBackground = registerColor("peekViewTitle.background", { dark: transparent(editorInfoForeground, 0.1), light: transparent(editorInfoForeground, 0.1), hc: null }, localize("peekViewTitleBackground", "Background color of the peek view title area."));
const peekViewTitleForeground = registerColor("peekViewTitleLabel.foreground", { dark: Color.white, light: Color.black, hc: Color.white }, localize("peekViewTitleForeground", "Color of the peek view title."));
const peekViewTitleInfoForeground = registerColor("peekViewTitleDescription.foreground", { dark: "#ccccccb3", light: "#616161", hc: "#FFFFFF99" }, localize("peekViewTitleInfoForeground", "Color of the peek view title info."));
const peekViewBorder = registerColor("peekView.border", { dark: editorInfoForeground, light: editorInfoForeground, hc: contrastBorder }, localize("peekViewBorder", "Color of the peek view borders and arrow."));
const peekViewResultsBackground = registerColor("peekViewResult.background", { dark: "#252526", light: "#F3F3F3", hc: Color.black }, localize("peekViewResultsBackground", "Background color of the peek view result list."));
registerColor("peekViewResult.lineForeground", { dark: "#bbbbbb", light: "#646465", hc: Color.white }, localize("peekViewResultsMatchForeground", "Foreground color for line nodes in the peek view result list."));
registerColor("peekViewResult.fileForeground", { dark: Color.white, light: "#1E1E1E", hc: Color.white }, localize("peekViewResultsFileForeground", "Foreground color for file nodes in the peek view result list."));
registerColor("peekViewResult.selectionBackground", { dark: "#3399ff33", light: "#3399ff33", hc: null }, localize("peekViewResultsSelectionBackground", "Background color of the selected entry in the peek view result list."));
registerColor("peekViewResult.selectionForeground", { dark: Color.white, light: "#6C6C6C", hc: Color.white }, localize("peekViewResultsSelectionForeground", "Foreground color of the selected entry in the peek view result list."));
const peekViewEditorBackground = registerColor("peekViewEditor.background", { dark: "#001F33", light: "#F2F8FC", hc: Color.black }, localize("peekViewEditorBackground", "Background color of the peek view editor."));
registerColor("peekViewEditorGutter.background", { dark: peekViewEditorBackground, light: peekViewEditorBackground, hc: peekViewEditorBackground }, localize("peekViewEditorGutterBackground", "Background color of the gutter in the peek view editor."));
registerColor("peekViewResult.matchHighlightBackground", { dark: "#ea5c004d", light: "#ea5c004d", hc: null }, localize("peekViewResultsMatchHighlight", "Match highlight color in the peek view result list."));
registerColor("peekViewEditor.matchHighlightBackground", { dark: "#ff8f0099", light: "#f5d802de", hc: null }, localize("peekViewEditorMatchHighlight", "Match highlight color in the peek view editor."));
registerColor("peekViewEditor.matchHighlightBorder", { dark: null, light: null, hc: activeContrastBorder }, localize("peekViewEditorMatchHighlightBorder", "Match highlight border in the peek view editor."));
export {
  IPeekViewService,
  PeekContext,
  PeekViewWidget,
  getOuterEditor,
  peekViewBorder,
  peekViewEditorBackground,
  peekViewResultsBackground,
  peekViewTitleBackground,
  peekViewTitleForeground,
  peekViewTitleInfoForeground
};
