import { z } from "zod";
import ipsum from "corporate-ipsum";
import {
  RichTextDefinition as BaseRichTextDefinition,
  isNotNil,
  SerializationSchema,
  StableValue
} from "@makeswift/controls";
import { LinkPlugin } from "../../slate/LinkPlugin";
import { InlinePlugin } from "../../slate/InlinePlugin";
import { TextAlignPlugin } from "../../slate/TextAlignPlugin";
import { BlockPlugin } from "../../slate/BlockPlugin";
import { TypographyPlugin } from "../../slate/TypographyPlugin";
import { InlineModePlugin } from "../../slate/InlineModePlugin";
import { toText } from "../../slate/utils";
import { renderRichTextV2 } from "../../runtimes/react/controls/rich-text-v2";
import { Plugin } from "./plugin";
import { RichTextV2Control } from "./control";
import { getTranslatableData } from "./translations/get-translations";
class Definition extends BaseRichTextDefinition {
  constructor({ mode, defaultValue }, plugins) {
    super({
      mode,
      defaultValue: defaultValue ?? (mode === Definition.Mode.Inline ? "Edit this text" : Definition.generateParagraph()),
      plugins: plugins ?? (mode === Definition.Mode.Inline ? [InlineModePlugin()] : [BlockPlugin(), TypographyPlugin(), TextAlignPlugin(), InlinePlugin(), LinkPlugin()])
    });
  }
  static generateParagraph() {
    return ipsum(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: 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 = z.object({
      control: z.object({
        definition: pluginDef
      }).optional()
    });
    const config = baseSchema.userConfig.extend({
      defaultValue: z.string(),
      plugins: z.array(plugin)
    });
    return {
      ...baseSchema,
      config,
      definition: z.object({
        type: baseSchema.type,
        config
      })
    };
  }
  get configSchema() {
    return Definition.fullSchema({ pluginDef: z.any() }).config;
  }
  createInstance(sendMessage) {
    return new RichTextV2Control(sendMessage, this);
  }
  resolveValue(data, _resolver, _stylesheet, control) {
    const stableValue = StableValue({
      name: Definition.type,
      read: () => renderRichTextV2(data, this, control ?? null)
    });
    return {
      ...stableValue,
      triggerResolve: async () => {
      }
    };
  }
  getTranslatableData(data) {
    if (data == null)
      return null;
    return getTranslatableData(Definition.dataToNodes(data), this.config.plugins);
  }
  get pluginControls() {
    return this.config.plugins.map((plugin) => plugin.control).filter(isNotNil);
  }
  pluginControlAt(index) {
    return this.pluginControls[index];
  }
  toText(data) {
    if (data == null)
      return "";
    return 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 = Plugin;
export {
  RichText,
  RichTextV2Control,
  RichTextV2Definition
};
//# sourceMappingURL=rich-text-v2.js.map