"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 extension_exports = {};
__export(extension_exports, {
  textAlign: () => textAlign
});
module.exports = __toCommonJS(extension_exports);
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_plugin_commons = require("./../../plugin-commons/index.js");
var import_get_enum_by_style = require("./get-enum-by-style.js");
var import_get_style_by_enum = require("./get-style-by-enum.js");
const textAlign = (0, import_plugin_commons.createRicosFunctionalExtension)({
  type: "extension",
  groups: [],
  name: "textAlign",
  reconfigure(config, _extensions, ricosProps) {
    const types = [
      import_v1.Node_Type.LIST_ITEM,
      import_v1.Node_Type.PARAGRAPH,
      import_v1.Node_Type.HEADING,
      import_v1.Node_Type.CODE_BLOCK,
      import_v1.Node_Type.BLOCKQUOTE,
      import_v1.Node_Type.CAPTION
    ];
    return {
      ...config,
      addOptions() {
        return {
          alignments: [
            import_v1.TextStyle_TextAlignment.AUTO,
            import_v1.TextStyle_TextAlignment.CENTER,
            import_v1.TextStyle_TextAlignment.LEFT,
            import_v1.TextStyle_TextAlignment.RIGHT,
            import_v1.TextStyle_TextAlignment.JUSTIFY
          ],
          textAlignment: (0, import_get_enum_by_style.getEnumByStyle)(ricosProps.textAlignment, import_v1.TextStyle_TextAlignment.AUTO)
        };
      },
      addGlobalAttributes() {
        return [
          {
            types,
            attributes: {
              textStyle: {
                default: this.options.textAlignment,
                parseHTML: (element) => {
                  const textAlignment = (0, import_get_enum_by_style.getEnumByStyle)(
                    element.style.textAlign,
                    this.options.textAlignment
                  );
                  return {
                    textAlignment
                  };
                },
                renderHTML: (attributes) => {
                  const textAlignment = attributes?.textStyle?.textAlignment ? attributes.textStyle.textAlignment : this.options.textAlignment;
                  return (0, import_get_style_by_enum.getStyleByEnum)(textAlignment);
                }
              }
            }
          }
        ];
      },
      addCommands() {
        return {
          setTextAlign: (alignment) => ({ commands }) => {
            const textAlignment = (0, import_get_enum_by_style.getEnumByStyle)(alignment, import_v1.TextStyle_TextAlignment.UNRECOGNIZED);
            if (!this.options.alignments.includes(textAlignment)) {
              console.error(
                `invalid text alignment ${alignment} provided to setTextAlign command`
              );
              return false;
            }
            return types.map(
              (type) => commands.updateAttributesWithDeepMerge(type, {
                textStyle: { textAlignment }
              })
            ).every(Boolean);
          },
          unsetTextAlign: () => ({ commands }) => types.map(
            (type) => commands.updateAttributesWithDeepMerge(type, {
              textStyle: { textAlignment: this.options.textAlignment }
            })
          ).every(Boolean)
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      priority: 3,
      addProseMirrorPlugins() {
        return [
          new import_state.Plugin({
            key: new import_state.PluginKey("textAlign"),
            appendTransaction: (_transactions, oldState, newState) => {
              if (newState.doc === oldState.doc) {
                return;
              }
              const tr = newState.tr;
              const { from, to } = newState.selection;
              newState.doc.nodesBetween(from, to, (node, pos) => {
                if ([import_v1.Node_Type.LIST_ITEM].includes(node.type.name)) {
                  const textStyle = node.firstChild?.attrs.textStyle || import_v1.TextStyle_TextAlignment.AUTO;
                  if (textStyle !== node.attrs.textStyle) {
                    tr.setNodeMarkup(pos, void 0, {
                      ...node.attrs,
                      textStyle
                    });
                  }
                }
              });
              return tr;
            }
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=extension.js.map
