"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var editor_preview_state_exports = {};
__export(editor_preview_state_exports, {
  createSketchPreviewState: () => createSketchPreviewState
});
module.exports = __toCommonJS(editor_preview_state_exports);
var import_ricos_types = require("./../../ricos-types/index.js");
const getBackgroundMarkType = (state) => state.schema.marks[import_ricos_types.Mark_Type.COLOR_BACKGROUND];
const collectBackgroundRestoreRanges = (doc, markType, from, to) => {
  const ranges = [];
  doc.nodesBetween(from, to, (node, pos) => {
    if (!node.isText) {
      return true;
    }
    const mark = node.marks.find((candidate) => candidate.type === markType);
    if (mark) {
      ranges.push({
        from: Math.max(from, pos),
        to: Math.min(to, pos + node.nodeSize),
        mark
      });
    }
    return true;
  });
  return ranges;
};
const createSketchPreviewState = () => {
  let animationRange = null;
  let override = null;
  let backgroundRestoreRanges = [];
  const restoreBackgroundMarks = (tr) => {
    const ranges = backgroundRestoreRanges;
    backgroundRestoreRanges = [];
    const docSize = tr.doc.content.size;
    for (const { from, to, mark } of ranges) {
      const safeFrom = Math.max(0, Math.min(from, docSize));
      const safeTo = Math.max(0, Math.min(to, docSize));
      if (safeFrom < safeTo) {
        tr.addMark(safeFrom, safeTo, mark);
      }
    }
  };
  const hideBackgroundMarks = (state, tr, nextOverride) => {
    restoreBackgroundMarks(tr);
    const markType = getBackgroundMarkType(state);
    if (!markType || nextOverride.style === "CLASSIC") {
      return;
    }
    const nextRestoreRanges = collectBackgroundRestoreRanges(
      tr.doc,
      markType,
      nextOverride.from,
      nextOverride.to
    );
    if (nextRestoreRanges.length === 0) {
      return;
    }
    backgroundRestoreRanges = nextRestoreRanges;
    tr.removeMark(nextOverride.from, nextOverride.to, markType);
  };
  return {
    getOverride: () => override,
    setOverride: (nextOverride) => {
      override = nextOverride;
    },
    getAnimationRange: () => animationRange,
    setAnimationRange: (range) => {
      animationRange = range;
    },
    consumeAnimationRange: () => {
      const range = animationRange;
      animationRange = null;
      return range;
    },
    hasBackgroundRestoreRanges: () => backgroundRestoreRanges.length > 0,
    hideBackgroundMarks,
    restoreBackgroundMarks
  };
};
//# sourceMappingURL=editor-preview-state.js.map
