import yaml from "js-yaml";
//#region src/browser/pipeline/schema.ts
/**
* Pipeline YAML schema — parse and validate brocli-style browser pipeline documents.
*/
function parseBrowserPipeline(yamlSource) {
	const errors = [];
	let raw;
	try {
		raw = yaml.load(yamlSource);
	} catch (e) {
		return {
			ok: false,
			errors: [{
				path: "",
				message: `YAML parse error: ${e instanceof Error ? e.message : String(e)}`
			}]
		};
	}
	if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {
		ok: false,
		errors: [{
			path: "",
			message: "Pipeline must be a YAML mapping (object)."
		}]
	};
	const doc = raw;
	const name = typeof doc.name === "string" ? doc.name.trim() : "";
	if (!name) errors.push({
		path: "name",
		message: "`name` is required."
	});
	const description = typeof doc.description === "string" ? doc.description : void 0;
	const provider = typeof doc.provider === "string" ? doc.provider : void 0;
	const include = Array.isArray(doc.include) ? doc.include.filter((x) => typeof x === "string") : void 0;
	const argsRaw = doc.args && typeof doc.args === "object" && !Array.isArray(doc.args) ? doc.args : {};
	const args = {};
	for (const [k, v] of Object.entries(argsRaw)) if (v && typeof v === "object" && !Array.isArray(v)) {
		const def = v;
		args[k] = {
			type: typeof def.type === "string" ? def.type : "string",
			required: def.required === true,
			default: def.default,
			description: typeof def.description === "string" ? def.description : void 0,
			choices: Array.isArray(def.choices) ? def.choices : void 0
		};
	} else args[k] = {
		type: "string",
		default: v
	};
	const pipelineRaw = doc.pipeline;
	if (!Array.isArray(pipelineRaw)) {
		errors.push({
			path: "pipeline",
			message: "`pipeline` must be an array of steps."
		});
		return {
			ok: false,
			errors
		};
	}
	const pipeline = [];
	for (let i = 0; i < pipelineRaw.length; i++) {
		const step = pipelineRaw[i];
		if (!step || typeof step !== "object" || Array.isArray(step)) {
			errors.push({
				path: `pipeline[${i}]`,
				message: "Each step must be a YAML mapping."
			});
			continue;
		}
		const stepObj = step;
		const actionKeys = Object.keys(stepObj);
		if (actionKeys.length !== 1) {
			errors.push({
				path: `pipeline[${i}]`,
				message: `Each step must have exactly one action key, found ${actionKeys.length}: [${actionKeys.join(", ")}].`
			});
			continue;
		}
		const action = actionKeys[0];
		const actionArgs = stepObj[action];
		if (typeof actionArgs === "string") pipeline.push({
			action,
			args: actionArgs
		});
		else if (actionArgs && typeof actionArgs === "object" && !Array.isArray(actionArgs)) pipeline.push({
			action,
			args: actionArgs
		});
		else if (actionArgs === null || actionArgs === void 0) pipeline.push({
			action,
			args: {}
		});
		else pipeline.push({
			action,
			args: { value: actionArgs }
		});
	}
	const onErrorRaw = doc.on_error;
	let onError;
	if (Array.isArray(onErrorRaw)) {
		onError = [];
		for (let i = 0; i < onErrorRaw.length; i++) {
			const step = onErrorRaw[i];
			if (!step || typeof step !== "object" || Array.isArray(step)) continue;
			const stepObj = step;
			const actionKeys = Object.keys(stepObj);
			if (actionKeys.length !== 1) continue;
			const action = actionKeys[0];
			const actionArgs = stepObj[action];
			if (typeof actionArgs === "string") onError.push({
				action,
				args: actionArgs
			});
			else if (actionArgs && typeof actionArgs === "object" && !Array.isArray(actionArgs)) onError.push({
				action,
				args: actionArgs
			});
			else onError.push({
				action,
				args: {}
			});
		}
	}
	if (errors.length > 0) return {
		ok: false,
		errors
	};
	return {
		ok: true,
		document: {
			name,
			description,
			provider,
			include,
			args,
			pipeline,
			onError
		},
		errors: []
	};
}
//#endregion
export { parseBrowserPipeline };

//# sourceMappingURL=schema.js.map