import { normalizeParsedImageBlocks } from './editorTabContent'

type EditorBlocks = unknown[]
type ParseMarkdownBlocks = (markdown: string) => EditorBlocks | Promise<EditorBlocks>

export type MarkdownParseResult = {
  blocks: EditorBlocks
  usedSourceFallback: boolean
}

function buildSourceLineBlock(line: string): Record<string, unknown> {
  return {
    type: 'paragraph',
    content: line ? [{ type: 'text', text: line, styles: {} }] : [],
    children: [],
  }
}

function buildMarkdownSourceBlocks(markdown: string): EditorBlocks {
  return markdown.split('\n').map(buildSourceLineBlock)
}

function parsedBlocksOrSourceFallback(blocks: EditorBlocks, sourceMarkdown: string): MarkdownParseResult {
  const normalizedBlocks = normalizeParsedImageBlocks(blocks)
  if (normalizedBlocks.length > 0 || sourceMarkdown.trim().length === 0) {
    return {
      blocks: normalizedBlocks,
      usedSourceFallback: false,
    }
  }
  return {
    blocks: buildMarkdownSourceBlocks(sourceMarkdown),
    usedSourceFallback: true,
  }
}

export async function parseMarkdownBlocksWithFallback(options: {
  parseMarkdownBlocks: ParseMarkdownBlocks
  preprocessed: string
  sourceMarkdown: string
  context: string
}): Promise<MarkdownParseResult> {
  const { parseMarkdownBlocks, preprocessed, sourceMarkdown, context } = options

  try {
    return parsedBlocksOrSourceFallback(await parseMarkdownBlocks(preprocessed), sourceMarkdown)
  } catch (error) {
    console.warn(`[editor] Rendering ${context} as plain Markdown because BlockNote could not parse it:`, error)
    return {
      blocks: buildMarkdownSourceBlocks(sourceMarkdown),
      usedSourceFallback: true,
    }
  }
}
