import escapeHtml from "escape-html";
import { Editor, Text } from "slate";
import { Slate } from "@makeswift/controls";
import { InlineType } from "../../../slate/types";
import { createEditorWithPlugins, pathToString } from "./translations-core";
function getDescendantTranslatableData(descendant, path) {
  if (Text.isText(descendant)) {
    return {};
  }
  if (Slate.isList(descendant) || Slate.isListItem(descendant)) {
    return descendant.children.reduce(
      (acc, d, j) => ({ ...acc, ...getDescendantTranslatableData(d, [...path, j]) }),
      {}
    );
  }
  const text = getInlineOrTextTranslatableData(descendant);
  if (text == null)
    return {};
  return { [pathToString(path)]: text };
}
function getInlineOrTextTranslatableData(descendant, path = []) {
  if (Text.isText(descendant)) {
    let string = escapeHtml(descendant.text);
    if (string === "")
      return null;
    if (descendant.typography === void 0)
      return string;
    return `<span key="${pathToString(path)}">${string}</span>`;
  }
  const children = descendant.children.map((child, i) => getInlineOrTextTranslatableData(child, [...path, i])).join("");
  if (children === "")
    return null;
  switch (descendant.type) {
    case InlineType.Link:
      return `<a key="${pathToString(path)}">${children}</a>`;
    case InlineType.SuperScript:
      return `<sup key="${pathToString(path)}">${children}</sup>`;
    case InlineType.SubScript:
      return `<sub key="${pathToString(path)}">${children}</sub>`;
    case InlineType.Code:
      return `<code key="${pathToString(path)}">${children}</code>`;
    default:
      return children;
  }
}
function getTranslatableData(nodes, plugins) {
  const editor = createEditorWithPlugins(plugins);
  editor.children = nodes;
  editor.typographyNormalizationDirection = "up";
  Editor.normalize(editor, { force: true });
  return editor.children.reduce(
    (acc, descendant, i) => ({
      ...acc,
      ...getDescendantTranslatableData(descendant, [i])
    }),
    {}
  );
}
export {
  getTranslatableData
};
//# sourceMappingURL=get-translations.js.map