"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 onChange_exports = {};
__export(onChange_exports, {
  onChange: () => onChange
});
module.exports = __toCommonJS(onChange_exports);
var import_slate = require("slate");
var import_controls = require("@makeswift/controls");
var import_unwrapInline = require("../BlockPlugin/unwrapInline");
var import_wrapInline = require("../BlockPlugin/wrapInline");
var import_filterForSubtreeRoots = require("../BlockPlugin/utils/filterForSubtreeRoots");
var import_types = require("./types");
var import_getValue = require("./getValue");
const onChange = (editor, value) => {
  if (value == null)
    return (0, import_unwrapInline.unwrapInline)(editor, import_controls.Slate.InlineType.Link);
  const roots = (0, import_filterForSubtreeRoots.filterForSubtreeRoots)((0, import_getValue.getLinksAndTextInSelection)(editor));
  const root = roots.at(0);
  if (roots.length === 1 && root != null && (0, import_types.isLinkEntry)(root)) {
    import_slate.Transforms.setNodes(
      editor,
      {
        link: value
      },
      { at: root[1] }
    );
  } else {
    (0, import_unwrapInline.unwrapInline)(editor, import_controls.Slate.InlineType.Link);
    (0, import_wrapInline.wrapInline)(editor, { type: import_controls.Slate.InlineType.Link, link: value, children: [] });
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  onChange
});
//# sourceMappingURL=onChange.js.map