import { TIPTAP_TABLE_HEADER_CELL_TYPE } from "./../ricos-content/index.js";
import { Node_Type } from "./../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { TABLE_DEFAULTS, normalizePadding } from "./defaults.js";
const tableConverter = {
  toPm: {
    types: [Node_Type.TABLE],
    // @ts-ignore next node type
    convert: (node, visit) => {
      const { dimensions, cellPadding, ...restTableData } = node.tableData || {};
      const { colsWidthRatio, colsMinWidth } = node.tableData?.dimensions || {};
      const widthInPixel = ratioWidthToPixel(colsWidthRatio, colsMinWidth);
      const rows = node.nodes || [];
      const gridOccupied = rows.map(() => []);
      return {
        type: Node_Type.TABLE,
        attrs: {
          id: node.id,
          dimensions,
          tableData: {
            ...restTableData,
            cellPadding: normalizePadding(cellPadding, [...TABLE_DEFAULTS.cellPadding])
          }
        },
        content: rows.map((rowNode, rowIndex) => {
          let colIdx = 0;
          return {
            type: Node_Type.TABLE_ROW,
            attrs: { id: rowNode.id, height: node.tableData?.dimensions?.rowsHeight?.[rowIndex] },
            content: (rowNode.nodes || []).map((colNode) => {
              const colspan = colNode.tableCellData?.colspan || 1;
              const rowspan = colNode.tableCellData?.rowspan || 1;
              while (gridOccupied[rowIndex]?.[colIdx]) {
                colIdx++;
              }
              const visualCol = colIdx;
              for (let r = 0; r < rowspan; r++) {
                for (let c = 0; c < colspan; c++) {
                  if (gridOccupied[rowIndex + r]) {
                    gridOccupied[rowIndex + r][visualCol + c] = true;
                  }
                }
              }
              let colwidth = null;
              if (widthInPixel) {
                colwidth = [];
                for (let i = 0; i < colspan; i++) {
                  const width = widthInPixel[visualCol + i];
                  if (width !== void 0) {
                    colwidth.push(width);
                  }
                }
                if (colwidth.length === 0) {
                  colwidth = null;
                }
              }
              colIdx += colspan;
              return {
                type: rowIndex === 0 && node.tableData?.rowHeader || visualCol === 0 && node.tableData?.columnHeader ? TIPTAP_TABLE_HEADER_CELL_TYPE : Node_Type.TABLE_CELL,
                attrs: {
                  ...colNode.tableCellData,
                  id: colNode.id,
                  colwidth
                },
                content: visit(colNode)
              };
            })
          };
        })
      };
    }
  },
  fromPm: {
    types: [Node_Type.TABLE],
    // @ts-ignore next node type
    convert: (node, visit) => {
      const { attrs = {} } = node;
      const { id, dimensions, tableData: attrsTableData } = attrs;
      const tableDataFromAttrs = attrsTableData || attrs;
      const { dimensions: _, cellPadding, ...restTableData } = tableDataFromAttrs;
      const tableNode = {
        type: Node_Type.TABLE,
        id,
        // @ts-ignore next node type - data may be from legacy schema
        tableData: {
          ...restTableData,
          dimensions: {
            ...dimensions,
            rowsHeight: node.content?.map((node2) => node2.attrs?.height)
          },
          // Only include cellPadding if it differs from defaults
          cellPadding: cellPadding?.length === 4 && cellPadding?.every(
            (p, index) => p === TABLE_DEFAULTS.cellPadding[index]
          ) ? void 0 : cellPadding
        },
        nodes: visit(node)
      };
      return tableNode;
    }
  }
};
const tableRowConverter = {
  toPm: {
    types: [Node_Type.TABLE_ROW],
    // @ts-ignore next node type
    convert: (node, visit) => ({
      type: Node_Type.TABLE_ROW,
      attrs: {
        id: node.id
      },
      content: visit(node)
    })
  },
  fromPm: {
    types: [Node_Type.TABLE_ROW],
    // @ts-ignore next node type
    convert: (node, visit) => {
      const { attrs = {} } = node;
      const { id } = attrs;
      const nodes = visit(node);
      const cellNodes = Array.isArray(nodes) && nodes.length > 0 ? nodes : [];
      return {
        type: Node_Type.TABLE_ROW,
        id,
        ...cellNodes.length > 0 && { nodes: cellNodes }
      };
    }
  }
};
const tableCellConverter = {
  toPm: {
    types: [Node_Type.TABLE_CELL],
    // @ts-ignore next node type
    convert: (node, visit) => {
      return {
        type: Node_Type.TABLE_CELL,
        attrs: {
          id: node.id,
          ...node.tableCellData,
          colspan: node.tableCellData?.colspan || 1,
          rowspan: node.tableCellData?.rowspan || 1
        },
        content: visit(node)
      };
    }
  },
  fromPm: {
    types: [TIPTAP_TABLE_HEADER_CELL_TYPE, Node_Type.TABLE_CELL],
    // @ts-ignore next node type
    convert: (node, visit) => {
      const { attrs = {} } = node;
      const { id, colwidth: _, colspan, rowspan, ...data } = attrs;
      return {
        type: Node_Type.TABLE_CELL,
        id,
        tableCellData: {
          ...data,
          ...colspan > 1 && { colspan },
          ...rowspan > 1 && { rowspan }
        },
        nodes: visit(node)
      };
    }
  }
};
const ratioWidthToPixel = (colsWidthRatio, colsMinWidth) => {
  if (colsWidthRatio && colsMinWidth) {
    return colsWidthRatio.map((width, i) => Math.max(width, colsMinWidth[i] || 0));
  }
};
export {
  tableCellConverter,
  tableConverter,
  tableRowConverter
};
//# sourceMappingURL=table-converters.js.map
