import { RicosPluginToolbarNames } from "./ricos-types/next/ricos-plugin-toolbar-names.js";
import { PluginNameToToolbarIdMap } from "./ricos-types/plugin-name-to-toolbar-id-map.js";
import { ToolbarType } from "./ricos-types/toolbar-enums.js";
const TOOLBAR_ID_TO_V2_NAME = (() => {
  const map = {};
  for (const [name, toolbarId] of Object.entries(PluginNameToToolbarIdMap)) {
    const v2Value = RicosPluginToolbarNames[name];
    if (v2Value && !map[toolbarId]) {
      map[toolbarId] = v2Value;
    }
  }
  return map;
})();
const toV2Name = (runtimeId) => TOOLBAR_ID_TO_V2_NAME[runtimeId] ?? runtimeId;
const DECORATION_RUNTIME_IDS = /* @__PURE__ */ new Set([
  PluginNameToToolbarIdMap.LINK,
  PluginNameToToolbarIdMap.ANCHOR
]);
const ALL_DISABLED = {
  desktop: false,
  mobile: { ios: false, android: false }
};
const resolveButtonsFactory = (buttons, desktopDefault, mobileDefault) => {
  if (!buttons) {
    return void 0;
  }
  if (Array.isArray(buttons)) {
    const list = buttons;
    return () => ({ desktop: list, mobile: { ios: list, android: list } });
  }
  const cb = buttons;
  return () => {
    const desktop = cb({ defaultButtons: desktopDefault });
    const mobile = cb({ defaultButtons: mobileDefault });
    return { desktop, mobile: { ios: mobile, android: mobile } };
  };
};
const toTextButtonIds = (list) => {
  const src = list ?? [];
  return src.filter((b) => typeof b === "string");
};
const buildGetToolbarSettings = (v2) => (ctx) => {
  const perToolbar = /* @__PURE__ */ new Map();
  const at = (name) => {
    const existing = perToolbar.get(name);
    if (existing) {
      return existing;
    }
    const fresh = { name };
    perToolbar.set(name, fresh);
    return fresh;
  };
  const text = v2.text;
  const textButtonsDefault = ctx.textButtons ?? { desktop: [], mobile: [] };
  const desktopDefaults = toTextButtonIds(textButtonsDefault.desktop);
  const mobileDefaults = toTextButtonIds(textButtonsDefault.mobile);
  const textVariants = [
    ToolbarType.FORMATTING,
    ToolbarType.STATIC,
    ToolbarType.INLINE,
    ToolbarType.MOBILE
  ];
  const getButtons = resolveButtonsFactory(
    text?.buttons,
    desktopDefaults,
    mobileDefaults
  );
  if (getButtons) {
    for (const name of textVariants) {
      at(name).getButtons = getButtons;
    }
  }
  if (text?.customButtons) {
    const platformMap = {
      desktop: text.customButtons,
      mobile: { ios: text.customButtons, android: text.customButtons }
    };
    for (const name of textVariants) {
      at(name).getTextPluginButtons = () => platformMap;
    }
  }
  if (text?.disabled === true) {
    for (const name of textVariants) {
      at(name).shouldCreate = () => ALL_DISABLED;
    }
  }
  const insert = v2.insert;
  const insertDefaults = ctx.pluginButtonNames ?? [];
  if (insert?.plusButton?.disabled === true) {
    at(ToolbarType.SIDE).shouldCreate = () => ALL_DISABLED;
  }
  if (insert?.plusButton?.onTrigger) {
    const onTrigger = insert.plusButton.onTrigger;
    at(ToolbarType.SIDE).onClick = (defaultTrigger) => onTrigger(defaultTrigger);
  }
  if (insert?.footer?.disabled === true) {
    at(ToolbarType.FOOTER).shouldCreate = () => ALL_DISABLED;
  }
  if (insert?.footer?.pluginsToDisplayInToolbar) {
    const footerConfig = {
      pluginsToDisplayInToolbar: insert.footer.pluginsToDisplayInToolbar
    };
    at(ToolbarType.FOOTER).footerToolbarConfig = footerConfig;
  }
  const insertGetButtons = resolveButtonsFactory(
    insert?.buttons,
    insertDefaults,
    insertDefaults
  );
  if (insertGetButtons) {
    at(ToolbarType.INSERT_PLUGIN).getButtons = insertGetButtons;
    if (!at(ToolbarType.FOOTER).getButtons) {
      at(ToolbarType.FOOTER).getButtons = insertGetButtons;
    }
  }
  const plugin = v2.plugin;
  if (plugin?.disabled === true) {
    at(ToolbarType.PLUGIN).shouldCreate = () => ALL_DISABLED;
    at(ToolbarType.LINK).shouldCreate = () => ALL_DISABLED;
  }
  if (plugin?.buttons) {
    const buttonsCb = plugin.buttons;
    at(ToolbarType.PLUGIN).filterButtons = (items, nodeType) => {
      const isDecoration = DECORATION_RUNTIME_IDS.has(nodeType);
      const v2Name = toV2Name(nodeType);
      const defaultIds = items.map((item) => item.id);
      const v2Ctx = isDecoration ? {
        type: "decoration",
        defaultButtons: defaultIds,
        decoration: v2Name
      } : { type: "node", defaultButtons: defaultIds, nodeType: v2Name };
      const returned = buttonsCb(v2Ctx);
      const byId = new Map(items.map((item) => [item.id, item]));
      return returned.map((id) => byId.get(id)).filter((item) => item !== void 0);
    };
  }
  const qa = v2.quickActions;
  if (qa) {
    if (qa.disabled === true) {
      at(ToolbarType.QUICK_ACTIONS).shouldCreate = () => ALL_DISABLED;
      at(ToolbarType.QUICK_ACTIONS).activateOnFloatingPlusButton = false;
    }
    if (qa.placeholder !== void 0) {
      at(ToolbarType.QUICK_ACTIONS).placeholder = qa.placeholder;
    }
  }
  return Array.from(perToolbar.values());
};
const toolbarsV2ToLegacy = (v2) => {
  if (!v2) {
    return void 0;
  }
  const result = {
    getToolbarSettings: buildGetToolbarSettings(v2)
  };
  if (v2.text?.mode === "static") {
    result.useStaticTextToolbar = true;
    result.textToolbarContainer = v2.text.container;
    result.staticToolbarAlignment = v2.text.alignment;
  }
  return result;
};
export {
  toolbarsV2ToLegacy
};
//# sourceMappingURL=next-toolbars-adapter.js.map
