import { join } from "node:path";
import { getLocalizedUrl, getMarkdownMetadata } from "@intlayer/core";
import { Locales } from "@intlayer/types";

//#region src/common.ts
const defaultLocale = Locales.ENGLISH;
const GITHUB_URL_PREFIX = "https://github.com/aymericzip/intlayer/blob/main/docs";
const URL_PREFIX = "https://intlayer.org/";
const getKeys = (obj) => Object.keys(obj);
const getFiles = async (files, lang = defaultLocale) => {
	const filesEntries = await Promise.all(Object.entries(files).map(([key, value]) => [key, value[lang]]).map(async ([key, value]) => [key, await value]));
	return Object.fromEntries(filesEntries);
};
const getFile = async (files, docKey, locale = defaultLocale) => {
	if (!files[docKey]) throw new Error(`File ${docKey} not found`);
	const file = await files[docKey]?.[locale];
	if (!file) {
		const englishFile = await files[docKey][defaultLocale];
		if (!englishFile) throw new Error(`File ${docKey} not found`);
		return englishFile;
	}
	return file;
};
const formatMetadata = (docKey, file, locale = defaultLocale) => {
	const metadata = getMarkdownMetadata(file);
	const slugs = (metadata.slugs ?? []).map(String);
	const keywords = (metadata.keywords ?? []).map(String);
	const relativeUrl = join("/", ...slugs);
	const slicedDocKey = docKey.slice(1);
	return {
		...metadata,
		docKey,
		slugs,
		keywords,
		githubUrl: `${GITHUB_URL_PREFIX}${slicedDocKey}`.replace("/en/", `/${locale}/`),
		relativeUrl: getLocalizedUrl(relativeUrl, locale),
		url: getLocalizedUrl(join(URL_PREFIX, relativeUrl), locale)
	};
};
const getFileMetadata = async (files, docKey, locale = defaultLocale) => {
	return formatMetadata(docKey, await getFile(files, docKey, locale), locale);
};
const getFileMetadataRecord = async (files, locale = defaultLocale) => {
	const filesEntries = await Promise.all(Object.entries(files).map(async ([key]) => [key, await getFileMetadata(files, key, locale)]));
	return Object.fromEntries(filesEntries);
};
const getFileMetadataBySlug = async (files, slugs, locale = defaultLocale, strict = false) => {
	const slugsArray = Array.isArray(slugs) ? slugs : [slugs];
	const filesMetadata = await getFileMetadataRecord(files, defaultLocale);
	let fileMetadataArray = Object.values(filesMetadata).filter((fileMetadata) => slugsArray.every((slug) => fileMetadata.slugs?.includes(slug)));
	if (strict) fileMetadataArray = fileMetadataArray.filter((fileMetadata) => fileMetadata.slugs.length === slugsArray.length);
	if (locale !== defaultLocale) return await Promise.all(fileMetadataArray.map(async (fileMetadata) => await getFileMetadata(files, fileMetadata.docKey, locale)));
	return fileMetadataArray;
};
const getFileBySlug = async (files, slugs, locale = defaultLocale, strict = false) => {
	const slugsArray = Array.isArray(slugs) ? slugs : [slugs];
	const filesMetadata = await getFileMetadataRecord(files, defaultLocale);
	let fileMetadataArray = Object.values(filesMetadata).filter((fileMetadata) => slugsArray.every((slug) => fileMetadata.slugs?.includes(slug)));
	if (strict) fileMetadataArray = fileMetadataArray.filter((fileMetadata) => fileMetadata.slugs.length === slugsArray.length);
	return await Promise.all(fileMetadataArray.map(async (fileMetadata) => {
		return await getFile(files, fileMetadata.docKey, locale);
	}));
};

//#endregion
export { GITHUB_URL_PREFIX, URL_PREFIX, defaultLocale, formatMetadata, getFile, getFileBySlug, getFileMetadata, getFileMetadataBySlug, getFileMetadataRecord, getFiles, getKeys };
//# sourceMappingURL=common.mjs.map