"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = EditedText;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _isEqual = _interopRequireDefault(require("lodash-es/isEqual"));
var _slateHistory = require("slate-history");
var _chat = require("../chat");
var _textEditorContext = _interopRequireDefault(require("./text-editor-context"));
var _textEditorContent = _interopRequireWildcard(require("./text-editor-content"));
var _withLinks = _interopRequireDefault(require("./hooks/with-links"));
var _textEditorLoader = _interopRequireDefault(require("./text-editor-loader"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function EditedTextComponent({
  value,
  placeholder,
  isScrollable = true,
  elementRender = _textEditorContent.Element,
  leafRender = _textEditorContent.Leaf,
  testId,
  ariaLabel
}) {
  const {
    slate,
    slateReact
  } = (0, _react.useContext)(_textEditorContext.default);
  const {
    createEditor
  } = slate;
  const {
    withReact,
    Slate
  } = slateReact;
  const editor = (0, _react.useMemo)(() => (0, _withLinks.default)((0, _slateHistory.withHistory)(withReact(createEditor()))), []);
  (0, _react.useEffect)(() => {
    if (value && !(0, _isEqual.default)(editor.children, value)) {
      // Update the cached value inside the editor
      (0, _chat.resetNodes)(editor, value);
    }
  }, [value, editor]);
  const ariaAttrs = (0, _react.useMemo)(() => {
    return {
      'aria-label': ariaLabel
    };
  }, [ariaLabel]);
  return (
    /*#__PURE__*/
    // Slate throws an error when no onChange is given, providing a noop func to avoid issues
    (0, _jsxRuntime.jsx)(Slate, {
      editor: editor,
      initialValue: value,
      onChange: () => undefined,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_textEditorContent.default, {
        placeholder: placeholder,
        isScrollable: isScrollable,
        elementRender: elementRender,
        leafRender: leafRender,
        readOnly: true,
        testId: testId,
        ariaAttrs: ariaAttrs
      })
    })
  );
}
function EditedText({
  value,
  placeholder,
  className = '',
  isScrollable = true,
  elementRender = _textEditorContent.Element,
  leafRender = _textEditorContent.Leaf,
  ariaLabel,
  testId
}) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)('edited-text', className),
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_textEditorLoader.default, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(EditedTextComponent, {
        isScrollable: isScrollable,
        elementRender: elementRender,
        leafRender: leafRender,
        value: value,
        placeholder: placeholder,
        testId: testId,
        ariaLabel: ariaLabel
      })
    })
  });
}