import { log } from "./log.js";
import { formatPath } from "./utilities/formatting.js";
//#region src/lib/source.ts
/**
* Define a metadata source with `discover` + `parse`.
* Automatically wires them into an `extract` implementation that handles:
* - Empty input check (returns undefined)
* - Per-input try/catch with log.warn
* - Filtering undefined results from parse
* - OneOrMany wrapping (single result unwrapped, multiple as array)
*/
function defineSource(config) {
	return {
		...config,
		async extract(context) {
			const inputs = await config.discover(context);
			if (inputs.length === 0) return void 0;
			const results = [];
			for (const input of inputs) try {
				const result = await config.parse(input, context);
				if (result) {
					result.source = formatPath(result.source, context.options.path, context.options.absolute);
					results.push(result);
				}
			} catch (error) {
				log.warn(`Failed to process "${input}": ${error instanceof Error ? error.message : String(error)}`);
			}
			if (results.length === 0) return void 0;
			return results.length === 1 ? results[0] : results;
		}
	};
}
//#endregion
export { defineSource };
