import { logger } from "../../../logger/index.js";
import { ExternalHostError } from "../../../types/errors/external-host-error.js";
import { PRESET_DEP_NOT_FOUND, PRESET_INVALID, fetchPreset, parsePreset } from "../util.js";
import { getRepoContents } from "../../../modules/platform/forgejo/forgejo-helper.js";
//#region lib/config/presets/forgejo/index.ts
const Endpoint = "https://code.forgejo.org/";
async function fetchJSONFile(repo, fileName, endpoint, tag) {
	let res;
	try {
		res = await getRepoContents(repo, fileName, tag, { baseUrl: endpoint });
	} catch (err) {
		if (err instanceof ExternalHostError) throw err;
		logger.debug(`Preset file ${fileName} not found in ${repo}: ${err.message}`);
		throw new Error(PRESET_DEP_NOT_FOUND);
	}
	let contentString;
	if (res.type === "file") contentString = res.contentString;
	else {
		logger.debug(`Preset ${fileName} has unexpected type '${res.type}'. Only \`file\` is supported`);
		throw new Error(PRESET_INVALID);
	}
	return parsePreset(contentString, fileName);
}
function getPresetFromEndpoint(repo, filePreset, presetPath, endpoint = Endpoint, tag) {
	return fetchPreset({
		repo,
		filePreset,
		presetPath,
		endpoint,
		tag,
		fetch: fetchJSONFile
	});
}
function getPreset({ repo, presetName = "default", presetPath, tag }) {
	return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
}
//#endregion
export { Endpoint, fetchJSONFile, getPreset, getPresetFromEndpoint };

//# sourceMappingURL=index.js.map