import {
  Node_Type
} from "./../../ricos-schema/next-schema/index.js";
import {
  PmNodeType
} from "./../../_shared/pm-node-types.js";
import { SMART_BLOCK_DEFAULTS } from "../consts.js";
const nullToUndef = (value) => value === null || value === void 0 ? void 0 : value;
const hasCellRenderData = (type) => type === "CIRCLE" || type === "STAIRCASE" || type === "PYRAMID" || type === "VERTICAL_FUNNEL";
const pmCells = (pmNode) => {
  const cells = [];
  pmNode.forEach((child) => {
    if (child.type.name === PmNodeType.SMART_BLOCK_CELL) {
      const attrs = child.attrs;
      cells.push({
        cellId: nullToUndef(attrs.id),
        accentColor: nullToUndef(attrs.accentColor),
        shape: attrs.shape,
        shapeColor: nullToUndef(attrs.shapeColor)
      });
    }
  });
  return cells;
};
const jtdCells = (node) => (node.nodes ?? []).filter((n) => n.type === Node_Type.SMART_BLOCK_CELL).map((cell) => {
  const data = cell.smartBlockCellData;
  return {
    cellId: cell.id,
    accentColor: data && "accentColor" in data ? data.accentColor : void 0,
    shape: data && "shape" in data ? data.shape : void 0,
    shapeColor: data && "shapeColor" in data ? data.shapeColor : void 0
  };
});
const pmBlockToViewModel = (pmNode) => {
  const attrs = pmNode.attrs;
  const type = attrs.type ?? SMART_BLOCK_DEFAULTS.type;
  return {
    type,
    columnSize: attrs.columnSize ?? SMART_BLOCK_DEFAULTS.columnSize,
    orientation: nullToUndef(attrs.orientation),
    totalCells: pmNode.childCount,
    blockBorder: {
      borderColor: nullToUndef(attrs.borderColor),
      borderWidth: nullToUndef(attrs.borderWidth),
      borderRadius: nullToUndef(attrs.borderRadius)
    },
    cells: hasCellRenderData(type) ? pmCells(pmNode) : void 0
  };
};
const jtdBlockToViewModel = (node) => {
  const data = node.smartBlockData;
  const type = data?.type ?? SMART_BLOCK_DEFAULTS.type;
  return {
    type,
    columnSize: data && "columnSize" in data ? data.columnSize ?? SMART_BLOCK_DEFAULTS.columnSize : SMART_BLOCK_DEFAULTS.columnSize,
    orientation: data && "orientation" in data ? data.orientation : void 0,
    totalCells: node.nodes?.length ?? 0,
    blockBorder: {
      borderColor: data && "borderColor" in data ? data.borderColor : void 0,
      borderWidth: data && "borderWidth" in data ? data.borderWidth : void 0,
      borderRadius: data && "borderRadius" in data ? data.borderRadius : void 0
    },
    cells: hasCellRenderData(type) ? jtdCells(node) : void 0
  };
};
export {
  jtdBlockToViewModel,
  pmBlockToViewModel
};
//# sourceMappingURL=block-view-model.js.map
