"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rich_text_v2_exports = {};
__export(rich_text_v2_exports, {
  RichText: () => RichText,
  RichTextV2Control: () => import_control.RichTextV2Control,
  RichTextV2Definition: () => RichTextV2Definition
});
module.exports = __toCommonJS(rich_text_v2_exports);
var import_zod = require("zod");
var import_corporate_ipsum = __toESM(require("corporate-ipsum"));
var import_controls = require("@makeswift/controls");
var import_LinkPlugin = require("../../slate/LinkPlugin");
var import_InlinePlugin = require("../../slate/InlinePlugin");
var import_TextAlignPlugin = require("../../slate/TextAlignPlugin");
var import_BlockPlugin = require("../../slate/BlockPlugin");
var import_TypographyPlugin = require("../../slate/TypographyPlugin");
var import_InlineModePlugin = require("../../slate/InlineModePlugin");
var import_utils = require("../../slate/utils");
var import_rich_text_v2 = require("../../runtimes/react/controls/rich-text-v2");
var import_plugin = require("./plugin");
var import_control = require("./control");
var import_get_translations = require("./translations/get-translations");
class Definition extends import_controls.RichTextDefinition {
  constructor({ mode, defaultValue }, plugins) {
    super({
      mode,
      defaultValue: defaultValue ?? (mode === Definition.Mode.Inline ? "Edit this text" : Definition.generateParagraph()),
      plugins: plugins ?? (mode === Definition.Mode.Inline ? [(0, import_InlineModePlugin.InlineModePlugin)()] : [(0, import_BlockPlugin.BlockPlugin)(), (0, import_TypographyPlugin.TypographyPlugin)(), (0, import_TextAlignPlugin.TextAlignPlugin)(), (0, import_InlinePlugin.InlinePlugin)(), (0, import_LinkPlugin.LinkPlugin)()])
    });
  }
  static generateParagraph() {
    return (0, import_corporate_ipsum.default)(3);
  }
  static deserialize(data, deserializeCallback) {
    if (data.type !== Definition.type) {
      throw new Error(`RichText: expected type ${Definition.type}, got ${data.type}`);
    }
    const { config } = Definition.fullSchema({
      pluginDef: import_controls.SerializationSchema.deserializedRecord
    }).definition.parse(data);
    const { plugins, ...userConfig } = config;
    return new RichTextV2Definition(
      userConfig,
      plugins.map(
        ({ control }) => control ? { control: { definition: deserializeCallback(control?.definition) } } : {}
      )
    );
  }
  static fullSchema({ pluginDef }) {
    const baseSchema = super.schema;
    const plugin = import_zod.z.object({
      control: import_zod.z.object({
        definition: pluginDef
      }).optional()
    });
    const config = baseSchema.userConfig.extend({
      defaultValue: import_zod.z.string(),
      plugins: import_zod.z.array(plugin)
    });
    return {
      ...baseSchema,
      config,
      definition: import_zod.z.object({
        type: baseSchema.type,
        config
      })
    };
  }
  get configSchema() {
    return Definition.fullSchema({ pluginDef: import_zod.z.any() }).config;
  }
  createInstance(sendMessage) {
    return new import_control.RichTextV2Control(sendMessage, this);
  }
  resolveValue(data, _resolver, _stylesheet, control) {
    const stableValue = (0, import_controls.StableValue)({
      name: Definition.type,
      read: () => (0, import_rich_text_v2.renderRichTextV2)(data, this, control ?? null)
    });
    return {
      ...stableValue,
      triggerResolve: async () => {
      }
    };
  }
  getTranslatableData(data) {
    if (data == null)
      return null;
    return (0, import_get_translations.getTranslatableData)(Definition.dataToNodes(data), this.config.plugins);
  }
  get pluginControls() {
    return this.config.plugins.map((plugin) => plugin.control).filter(import_controls.isNotNil);
  }
  pluginControlAt(index) {
    return this.pluginControls[index];
  }
  toText(data) {
    if (data == null)
      return "";
    return (0, import_utils.toText)(Definition.dataToNodes(data), this.config.mode ?? Definition.Mode.Block);
  }
}
class RichTextV2Definition extends Definition {
}
function RichText(config) {
  return new RichTextV2Definition(config ?? {});
}
RichText.Mode = Definition.Mode;
RichText.isV1Data = Definition.isV1Data;
RichText.dataToNodes = Definition.dataToNodes;
RichText.Plugin = import_plugin.Plugin;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RichText,
  RichTextV2Control,
  RichTextV2Definition
});
//# sourceMappingURL=rich-text-v2.js.map