Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
let node_path = require("node:path");
let _intlayer_core_localization = require("@intlayer/core/localization");
let _intlayer_core_transpiler = require("@intlayer/core/transpiler");
let _intlayer_types_locales = require("@intlayer/types/locales");
_intlayer_types_locales = require_runtime.__toESM(_intlayer_types_locales);

//#region src/common.ts
const defaultLocale = _intlayer_types_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 = (0, _intlayer_core_transpiler.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: (0, _intlayer_core_localization.getLocalizedUrl)(relativeUrl, locale),
		url: (0, _intlayer_core_localization.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 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
exports.GITHUB_URL_PREFIX = GITHUB_URL_PREFIX;
exports.URL_PREFIX = URL_PREFIX;
exports.defaultLocale = defaultLocale;
exports.formatMetadata = formatMetadata;
exports.getFile = getFile;
exports.getFileBySlug = getFileBySlug;
exports.getFileMetadata = getFileMetadata;
exports.getFileMetadataBySlug = getFileMetadataBySlug;
exports.getFileMetadataRecord = getFileMetadataRecord;
exports.getFiles = getFiles;
exports.getKeys = getKeys;
//# sourceMappingURL=common.cjs.map