"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 next_runtime_exports = {};
__export(next_runtime_exports, {
  RicosEditor: () => RicosEditor
});
module.exports = __toCommonJS(next_runtime_exports);
var import_react = __toESM(require("react"));
var import_ricos_editor = require("./ricos-editor/index.js");
var import_next_toolbars_adapter = require("./next-toolbars-adapter.js");
const legacyTypeFor = (type) => {
  switch (type) {
    case "text":
      return "FORMATTING";
    case "insert":
      return "INSERT_PLUGIN";
    case "plugin":
      return "PLUGIN";
    case "auto":
      return "WIXEL";
    case "quickActions":
      return "QUICK_ACTIONS";
  }
};
const toDropdownOptions = (src) => src.map((o) => ({
  id: o.id,
  label: o.label ?? o.text ?? o.id,
  ...o.icon ? { icon: o.icon } : {},
  ...o.dataHook ? { dataHook: o.dataHook } : {}
}));
const toExternalToolbarButtons = (legacyButtons, isMobile) => {
  const out = [];
  for (const [id, btn] of Object.entries(legacyButtons)) {
    if (!btn) {
      continue;
    }
    if (btn.type === "separator") {
      out.push({ kind: "separator", id });
      continue;
    }
    if (btn.type === "custom" && typeof btn.component === "function") {
      out.push({ kind: "custom", id, component: btn.component });
      continue;
    }
    const icon = btn.getIcon?.();
    if (!icon) {
      continue;
    }
    const label = btn.getLabel?.() ?? "";
    const tooltip = btn.tooltip ?? "";
    const isActive = btn.isActive?.() ?? false;
    const isDisabled = btn.isDisabled?.() ?? false;
    if (btn.type === "dropdown" && btn.componentProps) {
      const cp = btn.componentProps;
      if (Array.isArray(cp.dropdownOptions) && typeof cp.onSelect === "function") {
        out.push({
          kind: "dropdown",
          id,
          label,
          tooltip,
          icon,
          isActive,
          isDisabled,
          options: toDropdownOptions(cp.dropdownOptions),
          selectedId: cp.selectedId,
          // `ctx` is accepted for type consistency but not forwarded — the
          // underlying legacy callbacks for this shape (heading, alignment,
          // font-size) apply the selection directly without positional UI.
          onSelect: (optionId, _ctx) => cp.onSelect(optionId)
        });
        continue;
      }
      if (Array.isArray(cp.options) && typeof cp.onSelect === "function") {
        out.push({
          kind: "dropdown",
          id,
          label,
          tooltip,
          icon,
          isActive,
          isDisabled,
          options: toDropdownOptions(cp.options),
          selectedId: cp.selectedId,
          onSelect: (optionId, ctx) => {
            const apply = cp.onSelect({
              referenceElement: ctx?.referenceElement,
              isMobile
            });
            apply?.(optionId);
          }
        });
        continue;
      }
    }
    const legacyOnClick = btn.onClick;
    out.push({
      kind: "action",
      id,
      label,
      tooltip,
      icon,
      isActive,
      isDisabled,
      onClick: (ctx) => legacyOnClick?.(ctx ?? {}),
      ...Array.isArray(btn.tags) && btn.tags.length > 0 ? { tags: btn.tags } : {}
    });
  }
  return out;
};
const resolvedFromLegacyType = (legacyType) => {
  if (legacyType === "FORMATTING") {
    return "text";
  }
  if (legacyType === "PLUGIN") {
    return "plugin";
  }
  return void 0;
};
const useToolbarPropsV2 = (adapter, type, isMobile) => {
  const legacy = adapter.useToolbarProps(legacyTypeFor(type));
  const buttons = toExternalToolbarButtons(legacy.buttons, isMobile);
  if (type !== "auto") {
    return { buttons };
  }
  const resolved = resolvedFromLegacyType(legacy.toolbarType);
  return resolved ? { buttons, resolved } : { buttons };
};
const RicosEditor = (0, import_react.forwardRef)((props, ref) => {
  const {
    onChange,
    onSelectionChange,
    initialContent,
    toolbars,
    isMobile: isMobileProp,
    ...rest
  } = props;
  const combinedOnChange = (0, import_react.useCallback)(
    (content, meta) => {
      if (meta.source === "edit") {
        onChange?.(content);
      } else {
        onSelectionChange?.(content);
      }
    },
    [onChange, onSelectionChange]
  );
  const toolbarSettings = (0, import_react.useMemo)(() => (0, import_next_toolbars_adapter.toolbarsV2ToLegacy)(toolbars), [toolbars]);
  const [inner, setInner] = import_react.default.useState(null);
  const setInnerRef = (0, import_react.useCallback)((api) => setInner(api), []);
  (0, import_react.useImperativeHandle)(ref, () => {
    if (!inner) {
      return void 0;
    }
    const v2Hook = (type) => {
      if (type === "text" || type === "insert" || type === "plugin" || type === "auto" || type === "quickActions") {
        return useToolbarPropsV2(inner, type, !!isMobileProp);
      }
      return inner.useToolbarProps(type);
    };
    return new Proxy(inner, {
      get(target, prop, receiver) {
        if (prop === "useToolbarProps") {
          return v2Hook;
        }
        return Reflect.get(target, prop, receiver);
      }
    });
  }, [inner, isMobileProp]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_ricos_editor.RicosEditor,
    {
      ...rest,
      isMobile: isMobileProp,
      content: initialContent,
      onChange: combinedOnChange,
      toolbarSettings,
      ref: setInnerRef
    }
  );
});
//# sourceMappingURL=next-runtime.js.map
