import classnames from "classnames";
import React, { useContext } from "react";
import {
  RicosAlignmentContext,
  RicosDirectionHandledContext,
  RicosNodeContext,
  RicosNodeContextProvider,
  RicosViewerContext
} from "./../../../../../ricos-context/index.js";
import { TextStyle_TextAlignment } from "./../../../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { normalizeLineHeight } from "./../../../../../_shared/line-spacing-utils.js";
import alignmentStyle from "../../../../statics/alignment-scss.js";
import indentStyle from "../../../../statics/indentation-scss.js";
import { Anchorable } from "../../utils/anchorable.js";
import { getPaywallSeoClass, isPaywallSeo } from "../../utils/paywall-seo.js";
import { indentLevelToClassName } from "../consts.js";
import styles from "./paragraph-scss.js";
const Paragraph = ({
  node,
  nodeIndex,
  RicosNodesRenderer,
  shouldParagraphApplyTextStyle
}) => {
  const { seoMode } = useContext(RicosViewerContext);
  const { textAlignment: parentAlignment } = useContext(RicosAlignmentContext);
  const { parentNode } = useContext(RicosNodeContext);
  const directionHandled = useContext(RicosDirectionHandledContext);
  const { indentation = 0 } = node.paragraphData || {};
  const { textAlignment, lineHeight } = node.paragraphData?.textStyle || {};
  const finalParagraphAlignment = textAlignment === TextStyle_TextAlignment.AUTO || !textAlignment ? parentAlignment : textAlignment;
  const paragraphClassName = classnames(
    shouldParagraphApplyTextStyle && styles.text,
    alignmentStyle[finalParagraphAlignment || TextStyle_TextAlignment.AUTO],
    indentStyle[indentLevelToClassName[indentation]],
    !parentNode && isPaywallSeo(seoMode) && getPaywallSeoClass(seoMode?.paywall || {}, nodeIndex)
  );
  const childClassName = classnames(
    styles.child,
    textAlignment === TextStyle_TextAlignment.JUSTIFY && alignmentStyle[textAlignment]
  );
  const isEmpty = !node.nodes?.length;
  const doesEndWithSoftLine = !isEmpty && node.nodes?.[node.nodes?.length - 1]?.textData?.text?.slice(-1) === "\n";
  const Tag = isEmpty || !shouldParagraphApplyTextStyle ? "div" : "p";
  const nodeStyle = {
    ...node.style,
    ...lineHeight && { lineHeight: normalizeLineHeight(lineHeight) }
  };
  const Content = isEmpty ? /* @__PURE__ */ React.createElement("br", { role: "presentation" }) : /* @__PURE__ */ React.createElement(RicosNodeContextProvider, { parentNode: node, parentIndex: nodeIndex }, /* @__PURE__ */ React.createElement(RicosNodesRenderer, { key: `children-${node.id}`, nodes: node.nodes ?? [] }));
  return /* @__PURE__ */ React.createElement(Anchorable, { nodeId: node.id }, /* @__PURE__ */ React.createElement(Tag, { className: paragraphClassName, style: nodeStyle, dir: !directionHandled ? "auto" : "" }, /* @__PURE__ */ React.createElement("span", { className: childClassName }, Content), doesEndWithSoftLine && /* @__PURE__ */ React.createElement("br", { role: "presentation" })));
};
const ParagraphRenderer = ({ node, ...props }) => {
  return /* @__PURE__ */ React.createElement(Paragraph, { key: node.id, node, ...props });
};
var paragraph_default = ParagraphRenderer;
export {
  paragraph_default as default
};
//# sourceMappingURL=paragraph.js.map
