"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var y_adapter_exports = {};
__export(y_adapter_exports, {
  readYXmlFragment: () => readYXmlFragment,
  writeYXmlFragment: () => writeYXmlFragment
});
module.exports = __toCommonJS(y_adapter_exports);
var Y = __toESM(require("yjs"));
var import_converters = require("./../converters/index.js");
var import_editor_common = require("./../editor-common/index.js");
var import_y_converters = require("./y-converters.js");
const transactMaybe = (yFragment, callback, origin) => {
  if (yFragment.doc) {
    yFragment.doc.transact(() => {
      callback();
    }, origin);
  } else {
    callback();
  }
};
const writeYXmlFragment = (yFragment, content, origin) => {
  const pmJson = (0, import_converters.getAllPmConverters)().toPm(content ?? (0, import_editor_common.getEmptyContent)());
  transactMaybe(
    yFragment,
    () => {
      yFragment.delete(0, yFragment.length);
      yFragment.insert(0, [(0, import_y_converters.convertPmNodeToYElement)(pmJson)]);
    },
    origin
  );
};
const readYXmlFragment = (yFragment) => {
  const yChildren = yFragment.toArray();
  if (yChildren.length !== 1) {
    throw new Error("Y.XmlFragment should contain exactly one element");
  }
  const yDocElement = yChildren[0];
  if (!(yDocElement instanceof Y.XmlElement)) {
    throw new Error("Document root should be an Y.XmlElement");
  }
  if (yDocElement.nodeName !== "doc") {
    throw new Error('Document root should be a "doc" element');
  }
  return (0, import_converters.getAllPmConverters)().fromPm(
    (0, import_y_converters.convertYElementToPmNode)(yDocElement)
  );
};
//# sourceMappingURL=y-adapter.js.map
