export interface NavItem {
	title: string;
	href: string;
	items?: NavItem[];
}

export const navigation: NavItem[] = [
	{
		title: 'Getting Started',
		href: '/docs/getting-started',
		items: [
			{ title: 'Installation', href: '/docs/getting-started/installation' },
			{ title: 'Quickstart', href: '/docs/getting-started/quickstart' }
		]
	},
	{
		title: 'API Reference',
		href: '/docs/api',
		items: [
			{ title: 'Overview', href: '/docs/api/overview' },
			{ title: 'finance.summary', href: '/docs/api/finance-summary' },
			{ title: 'finance.statements', href: '/docs/api/finance-statements' },
			{ title: 'All Modules', href: '/docs/api/finance-others' },
			{ title: '계정 표준화와 시계열', href: '/docs/api/timeseries' },
			{ title: '섹터 분류', href: '/docs/api/sector' },
			{ title: '인사이트 등급', href: '/docs/api/insight' },
			{ title: '시장 순위', href: '/docs/api/rank' }
		]
	},
	{
		title: 'Tutorials',
		href: '/docs/tutorials',
		items: [
			{ title: '1. Quickstart', href: '/docs/tutorials/quickstart' },
			{ title: '2. 재무제표 조회', href: '/docs/tutorials/financial-statements' },
			{ title: '3. 시계열 분석', href: '/docs/tutorials/timeseries' },
			{ title: '4. 재무비율', href: '/docs/tutorials/ratios' },
			{ title: '5. 보고서 데이터', href: '/docs/tutorials/report-data' },
			{ title: '6. 공시 텍스트', href: '/docs/tutorials/disclosure' },
			{ title: '7. 고급 분석', href: '/docs/tutorials/advanced' },
			{ title: '8. 기업 간 비교', href: '/docs/tutorials/cross-company' }
		]
	},
	{
		title: 'Changelog',
		href: '/docs/changelog'
	}
];

function flattenNav(items: NavItem[]): NavItem[] {
	const result: NavItem[] = [];
	for (const item of items) {
		if (item.items && item.items.length > 0) {
			result.push(...flattenNav(item.items));
		} else {
			result.push(item);
		}
	}
	return result;
}

export function findPrevNext(
	path: string,
	items: NavItem[] = navigation
): { prev?: NavItem; next?: NavItem } {
	const flat = flattenNav(items);
	const idx = flat.findIndex((item) => path.endsWith(item.href));
	return {
		prev: idx > 0 ? flat[idx - 1] : undefined,
		next: idx < flat.length - 1 ? flat[idx + 1] : undefined
	};
}
