"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 ReadOnlyTextV2_exports = {};
__export(ReadOnlyTextV2_exports, {
  LeafComponent: () => LeafComponent,
  default: () => ReadOnlyTextV2_default
});
module.exports = __toCommonJS(ReadOnlyTextV2_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_rich_text_v2 = require("../../../../controls/rich-text-v2");
var import_use_style = require("../../use-style");
var import_utils = require("../../../../slate/utils");
var import_control = require("../control");
const ReadOnlyTextV2 = (0, import_react.forwardRef)(function ReadOnlyText({ text, definition }, ref) {
  const descendantsAsString = (0, import_utils.toText)(
    text?.descendants ?? [],
    definition?.config.mode ?? import_rich_text_v2.RichText.Mode.Block
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "div",
    {
      ref,
      style: {
        /**
         * These are the default styles that Slate uses for its editable div.
         * https://github.com/ianstormtaylor/slate/blob/4bd15ed3950e3a0871f5d0ecb391bb637c05e59d/packages/slate-react/src/components/editable.tsx
         * Search for `disableDefaultStyles`
         */
        position: "relative",
        whiteSpace: "pre-wrap",
        wordWrap: "break-word"
      },
      children: descendantsAsString === "" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Placeholder, {}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        Descendants,
        {
          plugins: definition?.config.plugins ?? [],
          descendants: text?.descendants ?? []
        }
      )
    }
  );
});
var ReadOnlyTextV2_default = ReadOnlyTextV2;
function Placeholder({ text = "Write some text..." }) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "span",
    {
      className: (0, import_use_style.useStyle)({
        display: "inline-block",
        width: 0,
        maxWidth: "100%",
        whiteSpace: "nowrap",
        opacity: 0.333,
        verticalAlign: "text-top"
      }),
      children: text
    }
  );
}
function LeafComponent({ plugins, ...props }) {
  function initialRenderLeaf({ leaf }) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: leaf.className, children: leaf.text === "" ? "\uFEFF" : leaf.text });
  }
  const renderLeaf = plugins.reduce(
    (renderFn, plugin) => (props2) => {
      const { control, renderLeaf: renderLeaf2 } = plugin;
      if (control?.definition == null || renderLeaf2 == null)
        return renderFn(props2);
      if (control.getLeafValue == null)
        return renderLeaf2(renderFn, void 0)(props2);
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_control.ControlValue, { definition: control.definition, data: control.getLeafValue(props2.leaf), children: (value) => renderLeaf2(renderFn, value)(props2) });
    },
    initialRenderLeaf
  );
  return renderLeaf({ attributes: {}, leaf: props.leaf, children: null, text: props.leaf });
}
function ElementComponent({ plugins, ...props }) {
  function initialRenderElement(props2) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Descendants, { descendants: props2.element.children, plugins });
  }
  const renderElement = plugins.reduce(
    (renderFn, plugin) => (props2) => {
      const { control, renderElement: renderElement2 } = plugin;
      if (control?.definition == null || renderElement2 == null)
        return renderFn(props2);
      if (control.getElementValue == null)
        return renderElement2(renderFn, void 0)(props2);
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_control.ControlValue, { definition: control.definition, data: control.getElementValue(props2.element), children: (value) => renderElement2(renderFn, value)(props2) });
    },
    initialRenderElement
  );
  return renderElement({ attributes: {}, children: null, element: props.descendant });
}
function isText(node) {
  if (typeof node === "object" && "text" in node)
    return true;
  return false;
}
function Descendants({
  descendants,
  plugins
}) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: descendants.map((descendant, index) => {
    if (isText(descendant)) {
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LeafComponent, { plugins, leaf: descendant }, index);
    }
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ElementComponent, { descendant, plugins }, index);
  }) });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LeafComponent
});
//# sourceMappingURL=ReadOnlyTextV2.js.map