"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 merge_translations_exports = {};
__export(merge_translations_exports, {
  mergeTranslatedNodes: () => mergeTranslatedNodes
});
module.exports = __toCommonJS(merge_translations_exports);
var import_slate = require("slate");
var import_slate_hyperscript = require("slate-hyperscript");
var import_parse5 = require("parse5");
var import_controls = require("@makeswift/controls");
var import_types = require("../../../slate/types");
var import_translations_core = require("./translations-core");
function deserializeTranslationHtmlString(el, translationKey) {
  if (el.nodeName === "#document-fragment") {
    const children = Array.from(el.childNodes).map((element) => deserializeTranslationHtmlString(element)).flat();
    if (children.length === 0) {
      children.push((0, import_slate_hyperscript.jsx)("text", {}, ""));
    }
    return children;
  }
  if (el.nodeName === "#text" && "value" in el) {
    return [(0, import_slate_hyperscript.jsx)("text", { translationKey: translationKey ?? void 0 }, el.value)];
  }
  if ("namespaceURI" in el) {
    const translationKey2 = el.attrs.find((a) => a.name === "key")?.value ?? void 0;
    const children = Array.from(el.childNodes).map((element) => deserializeTranslationHtmlString(element, translationKey2)).flat();
    if (children.length === 0) {
      children.push((0, import_slate_hyperscript.jsx)("text", {}, ""));
    }
    switch (el.nodeName) {
      case "code":
        return [(0, import_slate_hyperscript.jsx)("element", { type: import_types.InlineType.Code, translationKey: translationKey2 }, children)];
      case "sub":
        return [(0, import_slate_hyperscript.jsx)("element", { type: import_types.InlineType.SubScript, translationKey: translationKey2 }, children)];
      case "sup":
        return [(0, import_slate_hyperscript.jsx)("element", { type: import_types.InlineType.SuperScript, translationKey: translationKey2 }, children)];
      case "a":
        return [(0, import_slate_hyperscript.jsx)("element", { type: import_types.InlineType.Link, translationKey: translationKey2 }, children)];
      default:
        return children;
    }
  }
  return [];
}
function mergeTranslatedNodes(nodes, translatedData, plugins) {
  const sourceEditor = (0, import_translations_core.createEditorWithPlugins)(plugins);
  const targetEditor = (0, import_translations_core.createEditorWithPlugins)(plugins);
  sourceEditor.children = nodes;
  sourceEditor.typographyNormalizationDirection = "up";
  import_slate.Editor.normalize(sourceEditor, { force: true });
  Object.entries(translatedData).reverse().forEach(([blockStringPath, htmlString]) => {
    const blockPath = (0, import_translations_core.stringToPath)(blockStringPath);
    if (blockPath.length === 0)
      return;
    const html = (0, import_parse5.parseFragment)(htmlString);
    const inlineDescendants = deserializeTranslationHtmlString(html);
    targetEditor.children = [{ type: import_types.BlockType.Default, children: inlineDescendants }];
    targetEditor.typographyNormalizationDirection = "neutral";
    import_slate.Editor.normalize(targetEditor, { force: true });
    for (const [descendant, absolutePathToTargetNode] of import_slate.Node.descendants(targetEditor)) {
      if (!import_slate.Text.isText(descendant) && !import_controls.Slate.isInline(descendant) || descendant.translationKey == null || descendant.translationKey === "") {
        continue;
      }
      const relativePathToSourceNode = (0, import_translations_core.stringToPath)(descendant.translationKey);
      const absolutePathToSourceNode = [...blockPath, ...relativePathToSourceNode];
      const [sourceNode] = import_slate.Editor.node(sourceEditor, absolutePathToSourceNode);
      if (import_slate.Text.isText(sourceNode) && import_slate.Text.isText(descendant)) {
        const { translationKey, text, ...rest } = sourceNode;
        import_slate.Transforms.setNodes(targetEditor, rest, { at: absolutePathToTargetNode });
        import_slate.Transforms.unsetNodes(targetEditor, "translationKey", { at: absolutePathToTargetNode });
      } else if (import_controls.Slate.isInline(sourceNode) && import_controls.Slate.isInline(descendant)) {
        const { translationKey, children, ...rest } = sourceNode;
        import_slate.Transforms.setNodes(targetEditor, rest, { at: absolutePathToTargetNode });
        import_slate.Transforms.unsetNodes(targetEditor, "translationKey", { at: absolutePathToTargetNode });
      }
    }
    const translatedChildren = targetEditor.children.at(0)?.children;
    import_slate.Editor.withoutNormalizing(sourceEditor, () => {
      Array.from(import_slate.Node.children(sourceEditor, blockPath)).reverse().forEach(([_, path]) => {
        import_slate.Transforms.removeNodes(sourceEditor, { at: path });
      });
      import_slate.Transforms.insertNodes(sourceEditor, translatedChildren, { at: [...blockPath, 0] });
    });
  });
  sourceEditor.typographyNormalizationDirection = "down";
  import_slate.Editor.normalize(sourceEditor, { force: true });
  return sourceEditor.children;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  mergeTranslatedNodes
});
//# sourceMappingURL=merge-translations.js.map