import { PluginKey } from "./../../tiptap/pm/state.js";
import { Decoration, DecorationSet } from "./../../tiptap/pm/view.js";
import { TEXT_LAYER_Z_INDEX } from "./defaults.js";
import {
  buildSyntheticPreviewTrackedGroups,
  isOverlayPreviewStyle,
  isTrackedAnnotationGroupContainedInPreview
} from "./range-tracking.js";
const SKETCH_PLUGIN_KEY = new PluginKey("sketchDecorations");
const WIDGET_DATA_HOOK = "sketch-widget";
const WIDGET_KEY_ATTR = "data-rn-key";
const widgetDomFactory = (key) => () => {
  const div = document.createElement("div");
  div.setAttribute("data-hook", WIDGET_DATA_HOOK);
  div.setAttribute(WIDGET_KEY_ATTR, key);
  div.style.cssText = "position: absolute; pointer-events: none; left: 0; top: 0; width: 0; height: 0;";
  return div;
};
const pushDecorationsForTrackedGroup = (decorations, trackedGroup) => {
  decorations.push(
    Decoration.widget(trackedGroup.lcaStart, widgetDomFactory(trackedGroup.id), {
      side: -1,
      key: trackedGroup.id,
      marks: []
    })
  );
};
const buildDecorationSet = (state, trackedGroups, previewOverride) => {
  const decorations = [];
  for (const trackedGroup of trackedGroups) {
    if (previewOverride && isTrackedAnnotationGroupContainedInPreview(trackedGroup, previewOverride)) {
      continue;
    }
    pushDecorationsForTrackedGroup(decorations, trackedGroup);
  }
  if (previewOverride) {
    if (isOverlayPreviewStyle(previewOverride.style)) {
      for (const synthetic of buildSyntheticPreviewTrackedGroups(state, previewOverride)) {
        pushDecorationsForTrackedGroup(decorations, synthetic);
      }
    }
    if (previewOverride.style !== "CLASSIC") {
      decorations.push(
        Decoration.inline(previewOverride.from, previewOverride.to, {
          style: `background-color: transparent !important; position: relative; z-index: ${TEXT_LAYER_Z_INDEX};`
        })
      );
    }
  }
  return DecorationSet.create(state.doc, decorations);
};
const getPluginState = (state) => SKETCH_PLUGIN_KEY.getState(state);
export {
  SKETCH_PLUGIN_KEY,
  WIDGET_DATA_HOOK,
  WIDGET_KEY_ATTR,
  buildDecorationSet,
  getPluginState
};
//# sourceMappingURL=editor-decorations.js.map
