"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 layout_converter_exports = {};
__export(layout_converter_exports, {
  layoutConverter: () => layoutConverter
});
module.exports = __toCommonJS(layout_converter_exports);
var import_types = require("./../../pm-converters/types.js");
var import_next_schema = require("./../../ricos-schema/next-schema/index.js");
var import_get_normalized_col_spans = require("./get-normalized-col-spans.js");
var import_layout_fill = require("./layout-fill.js");
var import_shared_with_viewer = require("./shared-with-viewer.js");
const layoutConverter = (0, import_types.definePmNodeConverter)({
  toPm: {
    types: [import_next_schema.Node_Type.LAYOUT],
    convert: (node, visit) => {
      const normalizedColSpans = (0, import_get_normalized_col_spans.getNormalizedColSpans)(
        node.nodes.map((cell) => cell.layoutCellData?.colSpan ?? 0)
      );
      const background = (0, import_layout_fill.resolveLayoutFill)(
        node.layoutData?.background,
        node.layoutData?.backgroundColor
      );
      const backdrop = (0, import_layout_fill.resolveLayoutFill)(node.layoutData?.backdrop, node.layoutData?.backdropColor);
      return {
        type: import_next_schema.Node_Type.LAYOUT,
        attrs: {
          id: node.id,
          containerData: node.layoutData?.containerData,
          background,
          backdrop,
          backgroundColor: (0, import_layout_fill.getLayoutFillColor)(background) ?? import_shared_with_viewer.LAYOUT_DEFAULTS.backgroundColor,
          backdropColor: (0, import_layout_fill.getLayoutFillColor)(backdrop) ?? import_shared_with_viewer.LAYOUT_DEFAULTS.backdropColor,
          backgroundImage: node.layoutData?.backgroundImage ?? import_shared_with_viewer.LAYOUT_DEFAULTS.backgroundImage,
          borderColor: node.layoutData?.borderColor ?? import_shared_with_viewer.LAYOUT_DEFAULTS.borderColor,
          borderWidth: node.layoutData?.borderWidth ?? import_shared_with_viewer.LAYOUT_DEFAULTS.borderWidth,
          borderRadius: node.layoutData?.borderRadius ?? import_shared_with_viewer.LAYOUT_DEFAULTS.borderRadius,
          backdropImage: node.layoutData?.backdropImage ?? import_shared_with_viewer.LAYOUT_DEFAULTS.backdropImage,
          backdropPaddingTop: node.layoutData?.backdropPaddingTop ?? import_shared_with_viewer.LAYOUT_DEFAULTS.backdropPaddingTop,
          backdropPaddingBottom: node.layoutData?.backdropPaddingBottom ?? import_shared_with_viewer.LAYOUT_DEFAULTS.backdropPaddingBottom,
          gap: node.layoutData?.gap ?? import_shared_with_viewer.LAYOUT_DEFAULTS.gap,
          responsivenessBehaviour: node.layoutData?.responsivenessBehaviour ?? import_shared_with_viewer.LAYOUT_DEFAULTS.responsivenessBehaviour,
          responsivenessBreakpoint: node.layoutData?.responsivenessBreakpoint ?? import_shared_with_viewer.LAYOUT_DEFAULTS.responsivenessBreakpoint,
          cellPadding: (0, import_shared_with_viewer.normalizePadding)(node.layoutData?.cellPadding, [
            ...import_shared_with_viewer.LAYOUT_DEFAULTS.cellPadding
          ]),
          cellVerticalAlignment: node.layoutData?.cellVerticalAlignment ?? import_shared_with_viewer.LAYOUT_DEFAULTS.cellVerticalAlignment,
          designTarget: node.layoutData?.designTarget ?? import_shared_with_viewer.LAYOUT_DEFAULTS.designTarget,
          banner: node.layoutData?.banner ?? import_shared_with_viewer.LAYOUT_DEFAULTS.banner
        },
        content: node.nodes.map((cell, index) => {
          return {
            type: import_next_schema.Node_Type.LAYOUT_CELL,
            attrs: {
              id: cell.id,
              colSpan: normalizedColSpans[index]
            },
            content: visit?.(cell) ?? []
          };
        })
      };
    }
  },
  fromPm: {
    types: [import_next_schema.Node_Type.LAYOUT],
    convert: (node, visit) => {
      const { attrs = {} } = node;
      const {
        id,
        containerData,
        background,
        backdrop,
        backgroundColor,
        backgroundImage,
        borderColor,
        borderWidth,
        borderRadius,
        backdropColor,
        backdropImage,
        backdropPaddingTop,
        backdropPaddingBottom,
        gap,
        cellPadding,
        cellVerticalAlignment,
        responsivenessBehaviour,
        responsivenessBreakpoint,
        designTarget,
        banner
      } = attrs;
      const areAllColSpansSame = node.content?.every(
        (cell) => cell.attrs?.colSpan === node.content?.[0].attrs?.colSpan
      );
      const nodeToVisit = areAllColSpansSame ? {
        ...node,
        content: node.content?.map((cell) => ({
          ...cell,
          ..."attrs" in cell ? { attrs: { ...cell.attrs, colSpan: void 0 } } : {}
        }))
      } : node;
      const resolvedBackground = (0, import_layout_fill.resolveLayoutFill)(background, backgroundColor);
      const resolvedBackdrop = (0, import_layout_fill.resolveLayoutFill)(backdrop, backdropColor);
      return {
        type: import_next_schema.Node_Type.LAYOUT,
        id,
        layoutData: {
          containerData: Object.keys(containerData ?? {}).length ? containerData : void 0,
          background: resolvedBackground,
          backdrop: resolvedBackdrop,
          backgroundImage: backgroundImage === import_shared_with_viewer.LAYOUT_DEFAULTS.backgroundImage ? void 0 : backgroundImage,
          borderColor: borderColor === import_shared_with_viewer.LAYOUT_DEFAULTS.borderColor ? void 0 : borderColor,
          borderWidth: borderWidth === import_shared_with_viewer.LAYOUT_DEFAULTS.borderWidth ? void 0 : borderWidth,
          borderRadius: borderRadius === import_shared_with_viewer.LAYOUT_DEFAULTS.borderRadius ? void 0 : borderRadius,
          backdropImage: backdropImage === import_shared_with_viewer.LAYOUT_DEFAULTS.backdropImage ? void 0 : backdropImage,
          backdropPaddingTop: backdropPaddingTop === import_shared_with_viewer.LAYOUT_DEFAULTS.backdropPaddingTop ? void 0 : backdropPaddingTop,
          backdropPaddingBottom: backdropPaddingBottom === import_shared_with_viewer.LAYOUT_DEFAULTS.backdropPaddingBottom ? void 0 : backdropPaddingBottom,
          gap: gap === import_shared_with_viewer.LAYOUT_DEFAULTS.gap ? void 0 : gap,
          responsivenessBehaviour: responsivenessBehaviour === import_shared_with_viewer.LAYOUT_DEFAULTS.responsivenessBehaviour ? void 0 : responsivenessBehaviour,
          responsivenessBreakpoint: responsivenessBreakpoint === import_shared_with_viewer.LAYOUT_DEFAULTS.responsivenessBreakpoint ? void 0 : responsivenessBreakpoint,
          cellPadding: cellPadding?.length === 4 && cellPadding?.every(
            (p, index) => p === import_shared_with_viewer.LAYOUT_DEFAULTS.cellPadding[index]
          ) ? void 0 : cellPadding,
          cellVerticalAlignment: cellVerticalAlignment === import_shared_with_viewer.LAYOUT_DEFAULTS.cellVerticalAlignment ? void 0 : cellVerticalAlignment,
          designTarget: designTarget === import_shared_with_viewer.LAYOUT_DEFAULTS.designTarget ? void 0 : designTarget,
          banner: banner ?? void 0
        },
        nodes: visit?.(nodeToVisit) ?? []
      };
    }
  }
});
//# sourceMappingURL=layout-converter.js.map
