import { jsonFormat } from "./json.js";
import { tomlFormat } from "./toml.js";
import { yamlFormat } from "./yaml.js";
const formatRegistry = {
    json: jsonFormat,
    toml: tomlFormat,
    yaml: yamlFormat
};
const extensionMap = {
    ".json": "json",
    ".toml": "toml",
    ".yaml": "yaml",
    ".yml": "yaml"
};
/**
 * Get a format handler by path (auto-detect from extension) or explicit format name.
 */
export function getConfigFormat(pathOrFormat) {
    // Check if it's an explicit format name
    if (Object.prototype.hasOwnProperty.call(formatRegistry, pathOrFormat)) {
        return formatRegistry[pathOrFormat];
    }
    // Try to detect from extension
    const ext = getExtension(pathOrFormat);
    const formatName = extensionMap[ext];
    if (!formatName) {
        throw new Error(`Unsupported config format. Cannot detect format from "${pathOrFormat}". ` +
            `Supported extensions: ${Object.keys(extensionMap).join(", ")}. ` +
            `Supported format names: ${Object.keys(formatRegistry).join(", ")}.`);
    }
    return formatRegistry[formatName];
}
/**
 * Detect format name from a file path.
 */
export function detectFormat(path) {
    const ext = getExtension(path);
    return extensionMap[ext];
}
function getExtension(path) {
    const lastDot = path.lastIndexOf(".");
    if (lastDot === -1) {
        return "";
    }
    return path.slice(lastDot).toLowerCase();
}
export { jsonFormat } from "./json.js";
export { tomlFormat } from "./toml.js";
export { yamlFormat } from "./yaml.js";
