export type EditorHostKind = "leptos_tiptap_island";

export interface EditorHostConfig {
  kind: EditorHostKind;
}

export const DEFAULT_EDITOR_HOST_KIND: EditorHostKind = "leptos_tiptap_island";

export function normalizeEditorHostKind(
  value: unknown,
  fallback: EditorHostKind = DEFAULT_EDITOR_HOST_KIND,
): EditorHostKind {
  const normalized = typeof value === "string" ? value.trim().toLowerCase() : "";
  if (normalized === "blocknote") {
    return fallback;
  }
  if (
    normalized === "leptos_tiptap_runtime" ||
    normalized === "leptos_tiptap_inline" ||
    normalized === "leptos_tiptap"
  ) {
    return "leptos_tiptap_island";
  }
  // 说明：历史 debug host 已退出文档页主路径，旧 query/runtime 值统一折返正式 island host。
  return fallback;
}

export function resolveEditorHostKind(input: {
  override?: unknown;
  runtimeDefault?: unknown;
}): EditorHostKind {
  const runtimeDefault = normalizeEditorHostKind(
    input.runtimeDefault,
    DEFAULT_EDITOR_HOST_KIND,
  );
  const hasOverride =
    typeof input.override === "string" && input.override.trim().length > 0;
  if (hasOverride) {
    return normalizeEditorHostKind(input.override, runtimeDefault);
  }
  return runtimeDefault;
}

export function isLeptosTiptapHostKind(kind: EditorHostKind): boolean {
  return kind === "leptos_tiptap_island";
}

export function getEditorHostKindFromEnv(value?: unknown): EditorHostKind {
  return normalizeEditorHostKind(value);
}

export function getEditorHostConfig(value?: unknown): EditorHostConfig {
  return { kind: getEditorHostKindFromEnv(value) };
}
