import { TIPTAP_TABLE_CELL_TYPE } from "./../../ricos-content/index.js";
import { resolveDocumentColorToCss } from "./../../_shared/theme-color-token.js";
import { createRicosNodeExtension } from "./../../plugin-commons/index.js";
import { cellStatesPlugin } from "./plugins/index.js";
import styles from "./plugins/table-scss.js";
const tableCell = createRicosNodeExtension({
  groups: [],
  type: "html-node",
  reconfigure: (config, _extensions, _props, settings, services) => ({
    ...config,
    addOptions: () => ({
      services,
      ...settings
    })
  }),
  name: TIPTAP_TABLE_CELL_TYPE,
  createExtensionConfig() {
    return {
      name: this.name,
      tableRole: "cell",
      isolating: true,
      content: "(PARAGRAPH|tableCellContent)+",
      addAttributes: () => ({
        cellStyle: {
          default: null,
          parseHTML: (element) => {
            const cellStyle = element.getAttribute("cellstyle");
            return cellStyle ? JSON.parse(cellStyle) : null;
          },
          renderHTML: (attributes) => {
            return attributes.cellStyle && {
              cellstyle: JSON.stringify(attributes.cellStyle)
            };
          }
        },
        borderColors: {
          default: null,
          parseHTML: (element) => {
            const borderColors = element.getAttribute("bordercolors");
            return borderColors ? JSON.parse(borderColors) : null;
          },
          renderHTML: (attributes) => {
            return attributes.borderColors && {
              bordercolors: JSON.stringify(attributes.borderColors)
            };
          }
        },
        borderWidths: {
          default: null,
          parseHTML: (element) => {
            const borderWidths = element.getAttribute("borderwidths");
            return borderWidths ? JSON.parse(borderWidths) : null;
          },
          renderHTML: (attributes) => {
            return attributes.borderWidths && {
              borderwidths: JSON.stringify(attributes.borderWidths)
            };
          }
        },
        colspan: {
          default: 1,
          parseHTML: (element) => {
            const colspan = element.getAttribute("colspan");
            return colspan ? parseInt(colspan, 10) : 1;
          }
        },
        rowspan: {
          default: 1,
          parseHTML: (element) => {
            const rowspan = element.getAttribute("rowspan");
            return rowspan ? parseInt(rowspan, 10) : 1;
          }
        },
        colwidth: {
          default: null,
          parseHTML: (element) => {
            const colwidth = element.getAttribute("colwidth");
            const value = colwidth ? [parseInt(colwidth, 10)] : null;
            return value;
          }
        },
        verticalAlignment: {
          default: "top",
          parseHTML: () => null,
          // Handled by cellStyle parseHTML above
          renderHTML: (attributes) => {
            return attributes.cellStyle?.verticalAlignment && {
              style: `vertical-align: ${attributes.cellStyle.verticalAlignment.toLowerCase()}`
            };
          }
        },
        backgroundColor: {
          default: null,
          parseHTML: () => null,
          // Handled by cellStyle parseHTML above
          renderHTML: (attributes) => {
            return attributes.cellStyle?.backgroundColor && {
              style: `background-color: ${resolveDocumentColorToCss(
                attributes.cellStyle.backgroundColor
              )}`
            };
          }
        }
      }),
      parseHTML() {
        return [{ tag: "td" }];
      },
      renderHTML({ node, HTMLAttributes }) {
        return [
          "td",
          { ...HTMLAttributes, "data-node-id": node.attrs.id },
          ["div", { class: styles.borderTop }],
          ["div", { class: styles.borderBottom }],
          ["div", { class: styles.borderLeft }],
          ["div", { class: styles.borderRight }],
          ["div", { class: styles.cell }, 0]
        ];
      },
      addProseMirrorPlugins() {
        return [
          cellStatesPlugin(
            this.editor,
            this.options.services,
            this.options.services.context.isMobile
          )
        ];
      }
    };
  }
});
export {
  tableCell
};
//# sourceMappingURL=table-cell-extension.js.map
