"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 diff_tools_exports = {};
__export(diff_tools_exports, {
  getContentChange: () => getContentChange,
  getDiffRanges: () => getDiffRanges,
  isIgnoredTransaction: () => isIgnoredTransaction,
  reportGenericDiff: () => reportGenericDiff,
  reportLinkDiff: () => reportLinkDiff,
  reportTextStyleDiff: () => reportTextStyleDiff
});
module.exports = __toCommonJS(diff_tools_exports);
var import_Predicate = require("fp-ts-esm/Predicate");
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_content_change = require("./content-change.js");
var import_range = require("./range.js");
const getDiffRanges = (baseline, changes) => {
  const from = baseline.findDiffStart(changes);
  const to = baseline.findDiffEnd(changes);
  if (from === null || to === null) {
    return { newRange: import_range.Range.empty, oldRange: import_range.Range.empty };
  }
  if (from > to.a || from > to.b) {
    return { newRange: new import_range.Range(from, to.a), oldRange: new import_range.Range(from, to.b) };
  }
  return { newRange: new import_range.Range(from, to.b), oldRange: new import_range.Range(from, to.a) };
};
const isIgnoredTransaction = (transaction) => ["unique-id", "trailingNodeInsertion"].some((key) => transaction.getMeta(key));
const getContentChange = (baseline, changes, { oldRange, newRange }) => {
  const oldSlice = baseline.slice(oldRange.from, oldRange.to);
  const newSlice = changes.slice(newRange.from, newRange.to);
  return new import_content_change.ContentChange(oldSlice, newSlice);
};
const isNodeReplacement = (changes) => changes.length === 2 && changes[0].change === "delete" && changes[1].change === "insert" && changes[0].type === changes[1].type && changes[0].data?.id === changes[1].data?.id;
const reportGenericDiff = (diffs, onPluginAdded, onPluginDeleted) => !isNodeReplacement(diffs) && diffs.filter((0, import_Predicate.not)(hasIgnoredType)).filter((diff) => diff.type !== import_v1.Decoration_Type.LINK && diff.type !== import_v1.Decoration_Type.ANCHOR).forEach(({ type, change, data }) => {
  if (change === "insert") {
    onPluginAdded(type, data || {});
  } else if (change === "delete") {
    onPluginDeleted(type);
  }
});
const hasIgnoredType = (diff) => [
  import_v1.Node_Type.PARAGRAPH,
  "text",
  import_v1.Node_Type.LIST_ITEM,
  import_v1.Node_Type.COLLAPSIBLE_ITEM,
  import_v1.Node_Type.COLLAPSIBLE_ITEM_BODY,
  import_v1.Node_Type.COLLAPSIBLE_ITEM_TITLE,
  import_v1.Node_Type.TABLE_ROW,
  import_v1.Node_Type.TABLE_CELL
].includes(diff.type);
const reportTextStyleDiff = (diffs, onPluginAdded, onPluginDeleted) => {
  const textContainerChanges = diffs.filter(
    (diff) => diff.type === import_v1.Node_Type.PARAGRAPH || diff.type === import_v1.Node_Type.HEADING || diff.type === import_v1.Node_Type.BLOCKQUOTE || diff.type === import_v1.Node_Type.CODE_BLOCK
  );
  if (isNodeReplacement(textContainerChanges)) {
    if (textContainerChanges[0].data?.textStyle?.textAlignment !== textContainerChanges[1].data?.textStyle?.textAlignment) {
      if (textContainerChanges[1].data?.textStyle?.textAlignment !== import_v1.TextStyle_TextAlignment.AUTO) {
        onPluginAdded("textAlignment", {
          textAlignment: textContainerChanges[1].data?.textStyle?.textAlignment
        });
      } else {
        onPluginDeleted("textAlignment");
      }
    }
    if (textContainerChanges[0].data?.textStyle?.lineHeight !== textContainerChanges[1].data?.textStyle?.lineHeight) {
      if (textContainerChanges[1].data?.textStyle?.lineHeight) {
        onPluginAdded("lineHeight", {
          lineHeight: textContainerChanges[1]?.data?.textStyle?.lineHeight
        });
      } else {
        onPluginDeleted("lineHeight");
      }
    }
  }
};
const reportLinkDiff = (diffs, onLinkAdded, onPluginDeleted) => diffs.filter((diff) => diff.type === import_v1.Decoration_Type.LINK || diff.type === import_v1.Decoration_Type.ANCHOR).forEach(({ change, data, type }) => {
  if (change === "insert") {
    const link = data?.link || {};
    const linkData = {
      anchor: data?.anchor,
      link: link.url,
      nofollow: link.rel?.nofollow,
      noreferrer: link.rel?.noreferrer,
      newTab: link.target === import_v1.Link_Target.BLANK,
      pluginId: type,
      nodeId: ""
    };
    onLinkAdded(linkData);
  } else if (change === "delete") {
    onPluginDeleted(type);
  }
});
//# sourceMappingURL=diff-tools.js.map
