import { Facet, RangeSetBuilder } from "@codemirror/state";
import {
  Decoration,
  EditorView,
  ViewPlugin,
  WidgetType
} from "@codemirror/view";
import { scanSelectionRefs } from "./selection-refs.js";
export const selectionLookupFacet = Facet.define({
  combine: (values) => values.length > 0 ? values[values.length - 1] : /* @__PURE__ */ new Map()
});
class SelectionChipWidget extends WidgetType {
  constructor(id, name) {
    super();
    this.id = id;
    this.name = name;
  }
  eq(other) {
    return other.id === this.id && other.name === this.name;
  }
  toDOM() {
    const chip = document.createElement("span");
    chip.className = "cm-selection-chip";
    chip.setAttribute("data-selection-id", this.id);
    const tag = document.createElement("span");
    tag.className = "cm-selection-chip-tag";
    tag.textContent = "\u9009\u4E2D\u9879";
    chip.appendChild(tag);
    const text = document.createElement("span");
    text.className = "cm-selection-chip-label";
    if (this.name !== null) {
      text.textContent = this.name;
      chip.title = `\u9009\u4E2D\u9879 \xB7 ${this.name}`;
    } else {
      chip.classList.add("cm-selection-chip-dangling");
      const short = this.id.length > 8 ? `${this.id.slice(0, 8)}\u2026` : this.id;
      text.textContent = short;
      chip.title = `\u672A\u77E5\u5B57\u6BB5 ${this.id}`;
    }
    chip.appendChild(text);
    return chip;
  }
  ignoreEvent() {
    return false;
  }
}
function buildChips(view) {
  const lookup = view.state.facet(selectionLookupFacet);
  const builder = new RangeSetBuilder();
  for (const ref of scanSelectionRefs(view.state.doc.toString())) {
    const name = lookup.get(ref.id) ?? null;
    builder.add(
      ref.from,
      ref.to,
      Decoration.replace({ widget: new SelectionChipWidget(ref.id, name) })
    );
  }
  return builder.finish();
}
const chipPlugin = ViewPlugin.fromClass(
  class {
    decorations;
    constructor(view) {
      this.decorations = buildChips(view);
    }
    update(update) {
      if (update.docChanged || update.viewportChanged || update.startState.facet(selectionLookupFacet) !== update.state.facet(selectionLookupFacet)) {
        this.decorations = buildChips(update.view);
      }
    }
  },
  {
    decorations: (plugin) => plugin.decorations,
    // Atomic so the caret steps over a chip and Backspace removes the whole
    // `selections["id"]` at once — matching the scope chip's delete-as-unit feel.
    provide: (plugin) => EditorView.atomicRanges.of((view) => view.plugin(plugin)?.decorations ?? Decoration.none)
  }
);
const chipTheme = EditorView.theme({
  ".cm-selection-chip": {
    display: "inline-flex",
    alignItems: "center",
    gap: "0.25rem",
    margin: "0 1px",
    padding: "0.125rem 0.25rem",
    borderRadius: "0.25rem",
    background: "#f0fdfa",
    border: "1px solid #99f6e4",
    color: "#0f766e",
    fontFamily: "ui-sans-serif, system-ui, sans-serif",
    verticalAlign: "baseline",
    cursor: "default",
    whiteSpace: "nowrap"
  },
  ".cm-selection-chip-dangling": {
    background: "#fef2f2",
    borderColor: "#fecaca",
    color: "#b91c1c"
  },
  ".cm-selection-chip-tag": {
    borderRadius: "0.125rem",
    background: "#ccfbf1",
    padding: "0.05rem 0.2rem",
    fontSize: "9px",
    lineHeight: "1",
    color: "#0d9488"
  },
  ".cm-selection-chip-dangling .cm-selection-chip-tag": {
    background: "#fee2e2",
    color: "#b91c1c"
  },
  ".cm-selection-chip-label": {
    fontSize: "0.75rem",
    lineHeight: "1.1"
  }
});
export function selectionChips() {
  return [chipPlugin, chipTheme];
}
