const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
const require_getMarkdownMetadata = require('./packages/@intlayer/core/dist/esm/transpiler/markdown/getMarkdownMetadata.cjs');
const require_getLocalizedUrl = require('./packages/@intlayer/core/dist/esm/localization/getLocalizedUrl.cjs');
const require_locales = require('./packages/@intlayer/types/dist/esm/locales.cjs');
let node_path = require("node:path");

//#region ../../../docs/dist/esm/common.mjs
const defaultLocale = require_locales.locales_exports.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 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 = require_getMarkdownMetadata.getMarkdownMetadata(file);
	const slugs = (metadata.slugs ?? []).map(String);
	const keywords = (metadata.keywords ?? []).map(String);
	const relativeUrl = (0, node_path.join)("/", ...slugs);
	const slicedDocKey = docKey.slice(1);
	return {
		...metadata,
		docKey,
		slugs,
		keywords,
		githubUrl: `${GITHUB_URL_PREFIX}${slicedDocKey}`.replace("/en/", `/${locale}/`),
		relativeUrl: require_getLocalizedUrl.getLocalizedUrl(relativeUrl, locale),
		url: require_getLocalizedUrl.getLocalizedUrl((0, node_path.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 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
exports.defaultLocale = defaultLocale;
exports.getFile = getFile;
exports.getFileBySlug = getFileBySlug;
exports.getFileMetadataRecord = getFileMetadataRecord;
exports.getKeys = getKeys;
//# sourceMappingURL=common.cjs.map