import React, { useContext, useMemo } from "react";
import {
  RicosAlignmentContextProvider,
  RicosContext,
  RicosNodeContext,
  RicosNodeContextProvider
} from "./../../ricos-context/index.js";
import { SmartBlockCellView } from "../components/smart-block-cell-view.js";
import { SMART_BLOCK_DEFAULTS } from "../consts.js";
import { getCircleCellStyle } from "../utils/cell-position.js";
import { ricosCellToViewModel } from "../utils/cell-view-model.js";
import { resolveThemeSmartBlock } from "../utils/resolve-theme-smart-block.js";
const CIRCLE_SIDE_TO_ALIGNMENT = {
  left: "right",
  right: "left",
  bottom: "center"
};
const SmartBlockCellViewer = ({
  node,
  nodeIndex: cellIndex,
  RicosNodesRenderer
}) => {
  const cellNode = node;
  const { theme } = useContext(RicosContext);
  const themeSmartBlock = useMemo(() => resolveThemeSmartBlock(theme), [theme]);
  const nodeContext = useContext(RicosNodeContext) ?? {};
  const parentNode = nodeContext.parentNode;
  const smartBlockData = parentNode?.smartBlockData;
  const type = smartBlockData?.type ?? SMART_BLOCK_DEFAULTS.type;
  const columnSize = smartBlockData && "columnSize" in smartBlockData ? smartBlockData?.columnSize ?? SMART_BLOCK_DEFAULTS.columnSize : SMART_BLOCK_DEFAULTS.columnSize;
  const orientation = smartBlockData && "orientation" in smartBlockData ? smartBlockData.orientation : "HORIZONTAL";
  const isShapesMode = !!parentNode?.nodes?.some(
    (n) => n.type === "SMART_BLOCK_CELL" && n.smartBlockCellData !== null && n.smartBlockCellData !== void 0 && "shape" in n.smartBlockCellData && n.smartBlockCellData.shape !== null && n.smartBlockCellData.shape !== void 0
  );
  const totalCells = parentNode?.nodes?.length ?? 1;
  const viewModel = useMemo(
    () => ricosCellToViewModel(cellNode, {
      type,
      columnSize,
      orientation,
      totalCells,
      cellIndex,
      isShapesMode
    }),
    [cellNode, type, columnSize, orientation, totalCells, cellIndex, isShapesMode]
  );
  const circleAlignment = type === "CIRCLE" ? CIRCLE_SIDE_TO_ALIGNMENT[getCircleCellStyle(cellIndex, totalCells).side] : void 0;
  return /* @__PURE__ */ React.createElement(
    SmartBlockCellView,
    {
      viewModel,
      themeSmartBlock,
      contentSlot: (contentProps) => {
        const renderer = /* @__PURE__ */ React.createElement(
          RicosNodesRenderer,
          {
            key: `children-${node.id}`,
            nodes: node.nodes ?? [],
            shouldParagraphApplyTextStyle: true
          }
        );
        return /* @__PURE__ */ React.createElement("div", { ...contentProps }, /* @__PURE__ */ React.createElement(RicosNodeContextProvider, { parentNode: node, parentIndex: cellIndex }, circleAlignment ? /* @__PURE__ */ React.createElement(RicosAlignmentContextProvider, { textAlignment: circleAlignment }, renderer) : renderer));
      }
    }
  );
};
const SmartBlockCellRenderer = ({ node, ...props }) => {
  return /* @__PURE__ */ React.createElement(SmartBlockCellViewer, { key: node.id, node, ...props });
};
var smart_block_cell_default = SmartBlockCellRenderer;
export {
  smart_block_cell_default as default
};
//# sourceMappingURL=smart-block-cell.js.map
