"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 TypographyPlugin_exports = {};
__export(TypographyPlugin_exports, {
  TYPOGRAPHY_KEY: () => TYPOGRAPHY_KEY,
  TypographyActions: () => TypographyActions,
  TypographyPlugin: () => TypographyPlugin,
  withTypography: () => withTypography
});
module.exports = __toCommonJS(TypographyPlugin_exports);
var import_slate = require("slate");
var import_css = require("@emotion/css");
var import_controls = require("@makeswift/controls");
var import_clearActiveTypographyStyle = require("./clearActiveTypographyStyle");
var import_clearDeviceActiveTypography = require("./clearDeviceActiveTypography");
var import_detachActiveTypography = require("./detachActiveTypography");
var import_setActiveTypographyId = require("./setActiveTypographyId");
var import_setActiveTypographyStyle = require("./setActiveTypographyStyle");
var import_getValue = require("./getValue");
var import_selectors = require("../selectors");
var import_plugin = require("../../controls/rich-text-v2/plugin");
var import_normalizeTypographyUp = require("./normalizeTypographyUp");
var import_normalizeTypographyDown = require("./normalizeTypographyDown");
var import_normalizeSimilarText = require("./normalizeSimilarText");
const TypographyActions = {
  setActiveTypographyId: import_setActiveTypographyId.setActiveTypographyId,
  setActiveTypographyStyle: import_setActiveTypographyStyle.setActiveTypographyStyle,
  clearActiveTypographyStyle: import_clearActiveTypographyStyle.clearActiveTypographyStyle,
  clearDeviceActiveTypography: import_clearDeviceActiveTypography.clearDeviceActiveTypography,
  detachActiveTypography: import_detachActiveTypography.detachActiveTypography
};
const TYPOGRAPHY_KEY = "typography";
function withTypography(editor) {
  const { normalizeNode } = editor;
  editor.normalizeNode = (entry) => {
    const [normalizationNode, normalizationPath] = entry;
    if ("typography" in normalizationNode && normalizationNode?.typography?.id == null && normalizationNode?.typography?.style.length === 0) {
      import_slate.Transforms.unsetNodes(editor, TYPOGRAPHY_KEY, { at: normalizationPath });
      return;
    }
    if ("slice" in normalizationNode && normalizationNode?.slice != null) {
      import_slate.Transforms.unsetNodes(editor, "slice", { at: normalizationPath });
      return;
    }
    if ((0, import_normalizeSimilarText.normalizeSimilarText)(editor, entry))
      return;
    if (editor.typographyNormalizationDirection === "up" && (0, import_normalizeTypographyUp.normalizeTypographyUp)(editor, entry))
      return;
    if ((editor.typographyNormalizationDirection === "down" || editor.typographyNormalizationDirection == null) && (0, import_normalizeTypographyDown.normalizeTypographyDown)(editor, entry))
      return;
    normalizeNode(entry);
  };
  return editor;
}
function TypographyPlugin() {
  return (0, import_plugin.Plugin)({
    withPlugin: withTypography,
    control: {
      definition: (0, import_controls.unstable_Typography)(),
      onChange: (editor, value) => {
        import_slate.Editor.withoutNormalizing(editor, () => {
          const at = (0, import_selectors.getSelection)(editor);
          if (!at)
            return;
          const atRef = import_slate.Editor.rangeRef(editor, at);
          if (atRef.current) {
            import_slate.Transforms.setNodes(
              editor,
              {
                slice: true
              },
              {
                at: atRef.current,
                match: import_slate.Text.isText,
                split: true
              }
            );
          }
          if (atRef.current) {
            const nodesToUpdate = Array.from(
              import_slate.Editor.nodes(editor, {
                at: atRef.current,
                match: (node) => import_slate.Text.isText(node) && node.slice === true
              })
            );
            if (nodesToUpdate.length !== value?.length)
              return console.error(
                `TypographyControl.onChange received the wrong number of arguments.
            Called with ${value?.length} values mapping to ${nodesToUpdate.length} nodes.`
              );
            for (const [index, [, path]] of nodesToUpdate.entries()) {
              import_slate.Transforms.setNodes(
                editor,
                {
                  typography: value?.at(index)
                },
                { at: path, match: import_slate.Text.isText }
              );
            }
          }
          atRef.unref();
        });
      },
      getValue: import_getValue.getValue,
      getLeafValue: (text) => {
        return import_slate.Text.isText(text) ? text.typography : void 0;
      }
    },
    renderLeaf: (renderLeaf, className) => (props) => {
      return renderLeaf({
        ...props,
        leaf: {
          ...props.leaf,
          className: (0, import_css.cx)(props.leaf.className, className)
        }
      });
    }
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  TYPOGRAPHY_KEY,
  TypographyActions,
  TypographyPlugin,
  withTypography
});
//# sourceMappingURL=index.js.map