"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Placeholder = exports.Leaf = exports.Element = void 0;
exports.default = TextEditorContent;
var _react = _interopRequireWildcard(require("react"));
var _slate = require("slate");
var _classnames = _interopRequireDefault(require("classnames"));
var _textEditorContext = _interopRequireDefault(require("./text-editor-context"));
var _textEditorConstants = require("./text-editor-constants");
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); }
const Element = ({
  attributes,
  children,
  element
}) => {
  switch (element.type) {
    case _textEditorConstants.BLOCK_TYPES.HEADLINE_LARGE:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        ...attributes,
        className: "large-text-header",
        "test-id": "large-text",
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.HEADLINE_MEDIUM:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        ...attributes,
        className: "medium-text-header",
        "test-id": "medium-text",
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.HEADLINE_SMALL:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        ...attributes,
        className: "small-text-header",
        "test-id": "small-text",
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.BULLETED_LIST:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
        ...attributes,
        className: "list",
        "test-id": "bulleted-list",
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.NUMBERED_LIST:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("ol", {
        ...attributes,
        className: "list",
        "test-id": "numbered-list",
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.LIST_ITEM:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
        ...attributes,
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.LINK:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
        ...attributes,
        target: "_blank",
        rel: "noreferrer",
        className: "anchor r-link",
        href: element.url,
        "test-id": "anchor-link",
        children: children
      });
    case _textEditorConstants.BLOCK_TYPES.CODE:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        className: "code-block",
        "test-id": "code-block",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("code", {
          children: children
        })
      });
    default:
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        ...attributes,
        className: "regular-text",
        "test-id": "regular-text-content",
        children: children
      });
  }
};
exports.Element = Element;
const Leaf = ({
  attributes,
  children,
  leaf
}) => {
  let node = children;
  if (leaf[_textEditorConstants.MARK_TYPES.BOLD]) {
    node = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "bold",
      "test-id": "bold-text",
      children: node
    });
  }
  if (leaf[_textEditorConstants.MARK_TYPES.ITALIC]) {
    node = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "italic",
      "test-id": "italic-text",
      children: node
    });
  }
  if (leaf[_textEditorConstants.MARK_TYPES.UNDERLINE]) {
    node = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "underlined",
      "test-id": "underlined-text",
      children: node
    });
  }
  if (leaf[_textEditorConstants.MARK_TYPES.SUPERSCRIPT]) {
    node = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "superscript",
      "test-id": "superscript-text",
      children: node
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
    ...attributes,
    children: node
  });
};
exports.Leaf = Leaf;
const Placeholder = ({
  attributes,
  children
}) => {
  // Slate.js overwrites the default opacity to 0.33 within the style prop, omit it here
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const {
    opacity,
    ...restStyle
  } = attributes.style;
  const overwriteAttributes = {
    ...attributes,
    style: restStyle
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
    ...overwriteAttributes,
    className: "placeholder-text",
    "test-id": "placeholder-text",
    children: children
  });
};
exports.Placeholder = Placeholder;
function TextEditorContent({
  ariaAttrs,
  testId = 'text-editor-content',
  placeholder = '',
  readOnly = false,
  autoFocus = true,
  isScrollable = true,
  elementRender = Element,
  leafRender = Leaf,
  onBlur = () => {},
  onFocus = () => {},
  maxLength
}) {
  const {
    slateReact
  } = (0, _react.useContext)(_textEditorContext.default);
  const {
    Editable,
    useSlate
  } = slateReact;
  const editor = useSlate();
  const ElementNode = elementRender;
  const LeafNode = leafRender;

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  const renderElement = (0, _react.useCallback)(props => /*#__PURE__*/(0, _jsxRuntime.jsx)(ElementNode, {
    ...props
  }), []);
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  const renderLeaf = (0, _react.useCallback)(props => /*#__PURE__*/(0, _jsxRuntime.jsx)(LeafNode, {
    ...props
  }), []);
  const renderPlaceholder = (0, _react.useCallback)(
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  props => /*#__PURE__*/(0, _jsxRuntime.jsx)(Placeholder, {
    ...props
  }), []);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)('text-editor-content', {
      'is-scrollable': isScrollable
    }),
    "test-id": testId,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Editable, {
      className: "slate-content-editable",
      autoFocus: autoFocus,
      readOnly: readOnly,
      placeholder: placeholder,
      renderPlaceholder: renderPlaceholder,
      renderElement: renderElement,
      renderLeaf: renderLeaf,
      onBlur: onBlur,
      onFocus: onFocus,
      onDOMBeforeInput: event => {
        // NOTE: copy-paste events handled in withMaxLength() plugin because Slate under some
        // circumstances stops triggering onDOMBeforeInput event for copy-paste ¯\_(ツ)_/¯

        // no need to do any calculations if maxLength is not set or if it's a delete event
        if (maxLength && !event.inputType.startsWith('delete')) {
          const currentTextLength = _slate.Editor.string(editor, []).length;
          let newText = '';
          if (!!event.data) {
            newText = event.data;
          }
          if (currentTextLength + newText.length > maxLength) {
            event.preventDefault();
          }
        }
      },
      ...ariaAttrs
    })
  });
}