/**
 * Converter registry and shared types for rDocs import/export.
 *
 * All source-specific converters implement NoteConverter.
 * ConvertedNote is the intermediate format between external sources and NoteItem.
 */

import type { NoteItem, SourceRef } from '../schemas';

// Re-export types from shared converters
export type { ConvertedNote, ImportResult, ExportResult, NoteConverter, ImportInput, ExportOptions } from '../../../shared/converters/types';

// ── Shared utilities ──

/** Hash content for conflict detection (shared across all converters). */
export function hashContent(content: string): string {
	let hash = 0;
	for (let i = 0; i < content.length; i++) {
		const char = content.charCodeAt(i);
		hash = ((hash << 5) - hash) + char;
		hash |= 0;
	}
	return Math.abs(hash).toString(36);
}

// ── Converter registry ──

import type { NoteConverter } from '../../../shared/converters/types';

const converters = new Map<string, NoteConverter>();

export function registerConverter(converter: NoteConverter): void {
	converters.set(converter.id, converter);
}

export function getConverter(id: string): NoteConverter | undefined {
	ensureConvertersLoaded();
	return converters.get(id);
}

export function getAllConverters(): NoteConverter[] {
	ensureConvertersLoaded();
	return Array.from(converters.values());
}

// ── Lazy-load converters ──
let _loaded = false;
export function ensureConvertersLoaded(): void {
	if (_loaded) return;
	_loaded = true;
	require('./obsidian');
	require('./logseq');
	require('./notion');
	require('./google-docs');
	require('./evernote');
	require('./roam');
}
