import React, { forwardRef, useCallback, useImperativeHandle, useMemo } from "react";
import { RicosEditor as OriginalRicosEditor } from "./ricos-editor/index.js";
import { toolbarsV2ToLegacy } from "./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 = forwardRef((props, ref) => {
  const {
    onChange,
    onSelectionChange,
    initialContent,
    toolbars,
    isMobile: isMobileProp,
    ...rest
  } = props;
  const combinedOnChange = useCallback(
    (content, meta) => {
      if (meta.source === "edit") {
        onChange?.(content);
      } else {
        onSelectionChange?.(content);
      }
    },
    [onChange, onSelectionChange]
  );
  const toolbarSettings = useMemo(() => toolbarsV2ToLegacy(toolbars), [toolbars]);
  const [inner, setInner] = React.useState(null);
  const setInnerRef = useCallback((api) => setInner(api), []);
  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__ */ React.createElement(
    OriginalRicosEditor,
    {
      ...rest,
      isMobile: isMobileProp,
      content: initialContent,
      onChange: combinedOnChange,
      toolbarSettings,
      ref: setInnerRef
    }
  );
});
export {
  RicosEditor
};
//# sourceMappingURL=next-runtime.js.map
