"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var table_converters_exports = {};
__export(table_converters_exports, {
  tableCellConverter: () => tableCellConverter,
  tableConverter: () => tableConverter,
  tableRowConverter: () => tableRowConverter
});
module.exports = __toCommonJS(table_converters_exports);
var import_ricos_content = require("./../ricos-content/index.js");
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_defaults = require("./defaults.js");
const tableConverter = {
  toPm: {
    types: [import_v1.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: import_v1.Node_Type.TABLE,
        attrs: {
          id: node.id,
          dimensions,
          tableData: {
            ...restTableData,
            cellPadding: (0, import_defaults.normalizePadding)(cellPadding, [...import_defaults.TABLE_DEFAULTS.cellPadding])
          }
        },
        content: rows.map((rowNode, rowIndex) => {
          let colIdx = 0;
          return {
            type: import_v1.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 ? import_ricos_content.TIPTAP_TABLE_HEADER_CELL_TYPE : import_v1.Node_Type.TABLE_CELL,
                attrs: {
                  ...colNode.tableCellData,
                  id: colNode.id,
                  colwidth
                },
                content: visit(colNode)
              };
            })
          };
        })
      };
    }
  },
  fromPm: {
    types: [import_v1.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: import_v1.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 === import_defaults.TABLE_DEFAULTS.cellPadding[index]
          ) ? void 0 : cellPadding
        },
        nodes: visit(node)
      };
      return tableNode;
    }
  }
};
const tableRowConverter = {
  toPm: {
    types: [import_v1.Node_Type.TABLE_ROW],
    // @ts-ignore next node type
    convert: (node, visit) => ({
      type: import_v1.Node_Type.TABLE_ROW,
      attrs: {
        id: node.id
      },
      content: visit(node)
    })
  },
  fromPm: {
    types: [import_v1.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: import_v1.Node_Type.TABLE_ROW,
        id,
        ...cellNodes.length > 0 && { nodes: cellNodes }
      };
    }
  }
};
const tableCellConverter = {
  toPm: {
    types: [import_v1.Node_Type.TABLE_CELL],
    // @ts-ignore next node type
    convert: (node, visit) => {
      return {
        type: import_v1.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: [import_ricos_content.TIPTAP_TABLE_HEADER_CELL_TYPE, import_v1.Node_Type.TABLE_CELL],
    // @ts-ignore next node type
    convert: (node, visit) => {
      const { attrs = {} } = node;
      const { id, colwidth: _, colspan, rowspan, ...data } = attrs;
      return {
        type: import_v1.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));
  }
};
//# sourceMappingURL=table-converters.js.map
