import { definePmNodeConverter } from "./../pm-converters/types.js";
import { TIPTAP_ACTION_BUTTON_TYPE, TIPTAP_LINK_BUTTON_TYPE } from "./../ricos-content/index.js";
import { Node_Type, PluginContainerData_Alignment } from "./../ricos-schema/next-schema/index.js";
import { fromTiptapLinkConverter } from "./../_shared/link/link-converters.js";
const toTiptapButtonConvert = (node) => {
  const styles = node.buttonData.styles;
  const bgColor = styles?.backgroundColor ?? styles?.colors?.background;
  const bgColorHover = styles?.backgroundColorHover;
  return {
    type: node.buttonData.type === "LINK" ? TIPTAP_LINK_BUTTON_TYPE : TIPTAP_ACTION_BUTTON_TYPE,
    attrs: {
      ...node.buttonData,
      styles: {
        ...styles,
        // Clear deprecated fields
        border: void 0,
        colors: void 0,
        backgroundColor: void 0,
        backgroundColorHover: void 0,
        // Migrate deprecated backgroundColor to new background union
        background: styles?.background ?? (bgColor ? { type: "COLOR", color: bgColor } : void 0),
        backgroundHover: styles?.backgroundHover ?? (bgColorHover ? { type: "COLOR", color: bgColorHover } : void 0),
        textColor: styles?.textColor ?? styles?.colors?.text,
        borderColor: styles?.borderColor ?? styles?.colors?.border,
        borderWidth: styles?.borderWidth ?? styles?.border?.width,
        borderRadius: styles?.borderRadius ?? styles?.border?.radius
      },
      id: node.id
    }
  };
};
const fromTiptapButtonConvert = (node) => {
  const { id, ...data } = node.attrs || {};
  const linkData = data.link;
  const containerData = data.containerData;
  return {
    type: Node_Type.BUTTON,
    id,
    nodes: [],
    buttonData: {
      ...data,
      link: fromTiptapLinkConverter(linkData),
      containerData: {
        ...containerData,
        alignment: containerData?.alignment || PluginContainerData_Alignment.CENTER,
        width: containerData?.width ? {
          ...containerData?.width,
          size: containerData?.width?.custom ? void 0 : containerData?.width?.size
        } : void 0
      },
      text: data.text || ""
    }
  };
};
const linkButtonConverter = definePmNodeConverter({
  toPm: {
    types: [Node_Type.BUTTON],
    convert: toTiptapButtonConvert
  },
  fromPm: {
    types: [TIPTAP_LINK_BUTTON_TYPE],
    convert: fromTiptapButtonConvert
  }
});
const actionButtonConverter = definePmNodeConverter({
  toPm: {
    types: [Node_Type.BUTTON],
    convert: toTiptapButtonConvert
  },
  fromPm: {
    types: [TIPTAP_ACTION_BUTTON_TYPE],
    convert: fromTiptapButtonConvert
  }
});
export {
  actionButtonConverter,
  linkButtonConverter
};
//# sourceMappingURL=button-converters.js.map
