"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 tiptap_exports = {};
__export(tiptap_exports, {
  lineSpacing: () => lineSpacing
});
module.exports = __toCommonJS(tiptap_exports);
var import_ricos_types = require("./../ricos-types/index.js");
var import_line_spacing_utils = require("./../_shared/line-spacing-utils.js");
var import_plugin_commons = require("./../plugin-commons/index.js");
const lineSpacing = (0, import_plugin_commons.createRicosFunctionalExtension)({
  type: "extension",
  groups: [],
  name: "lineSpacing",
  reconfigure(config, _extensions) {
    const types = [
      import_ricos_types.Node_Type.PARAGRAPH,
      import_ricos_types.Node_Type.HEADING,
      import_ricos_types.Node_Type.CODE_BLOCK,
      import_ricos_types.Node_Type.BLOCKQUOTE,
      import_ricos_types.Node_Type.CAPTION
    ];
    return {
      ...config,
      addGlobalAttributes() {
        return [
          {
            types,
            attributes: {
              textStyle: {
                renderHTML: (attributes) => {
                  const styles = [];
                  if (!isNaN(attributes?.textStyle?.lineHeight)) {
                    styles.push(
                      `line-height:${(0, import_line_spacing_utils.normalizeLineHeight)(attributes.textStyle.lineHeight)}`
                    );
                  }
                  return {
                    style: styles.join(";")
                  };
                }
              }
            }
          },
          {
            types,
            attributes: {
              style: {
                renderHTML: (attributes) => {
                  const styles = [];
                  if (attributes?.style?.paddingTop) {
                    styles.push(`padding-top:${attributes.style.paddingTop}`);
                  }
                  if (attributes?.style?.paddingBottom) {
                    styles.push(`padding-bottom:${attributes.style.paddingBottom}`);
                  }
                  return {
                    style: styles.join(";")
                  };
                }
              }
            }
          }
        ];
      },
      addCommands() {
        return {
          setLineSpacings: (lineSpacings) => ({ chain }) => {
            const {
              "line-height": stringLineHeight,
              "padding-bottom": stringPaddingBottom,
              "padding-top": stringPaddingTop
            } = lineSpacings;
            const lineHeight = parseFloat(stringLineHeight);
            const paddingBottom = parseFloat(stringPaddingBottom);
            const paddingTop = parseFloat(stringPaddingTop);
            return chain().focus().setLineSpacing(lineHeight).setLineSpacingBefore(paddingTop).setLineSpacingAfter(paddingBottom).run();
          },
          setLineSpacing: (lineSpacing2) => ({ commands }) => {
            if (lineSpacing2 > 100 || lineSpacing2 < 0) {
              return false;
            }
            return types.map(
              (type) => commands.updateAttributesWithDeepMerge(type, {
                textStyle: { lineHeight: lineSpacing2 }
              })
            ).every(Boolean);
          },
          setLineSpacingBefore: (lineSpacing2) => ({ commands }) => {
            if (lineSpacing2 > 250 || lineSpacing2 < 0) {
              return false;
            }
            return types.map(
              (type) => commands.updateAttributesWithDeepMerge(type, {
                style: {
                  paddingTop: `${lineSpacing2}px`
                }
              })
            ).every(Boolean);
          },
          setLineSpacingAfter: (lineSpacing2) => ({ commands }) => {
            if (lineSpacing2 > 250 || lineSpacing2 < 0) {
              return false;
            }
            return types.map(
              (type) => commands.updateAttributesWithDeepMerge(type, {
                style: {
                  paddingBottom: `${lineSpacing2}px`
                }
              })
            ).every(Boolean);
          }
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name
    };
  }
});
//# sourceMappingURL=tiptap.js.map
