import en_default from "../locales/en.js";
import { getErrorMap } from "../errors.js";
const makeIssue = (e) => {
	let { data: i, path: a, errorMaps: o, issueData: s } = e, c = [...a, ...s.path || []], l = {
		...s,
		path: c
	};
	if (s.message !== void 0) return {
		...s,
		path: c,
		message: s.message
	};
	let u = "", d = o.filter((e) => !!e).slice().reverse();
	for (let e of d) u = e(l, {
		data: i,
		defaultError: u
	}).message;
	return {
		...s,
		path: c,
		message: u
	};
};
function addIssueToContext(o, s) {
	let c = getErrorMap(), l = makeIssue({
		issueData: s,
		data: o.data,
		path: o.path,
		errorMaps: [
			o.common.contextualErrorMap,
			o.schemaErrorMap,
			c,
			c === en_default ? void 0 : en_default
		].filter((e) => !!e)
	});
	o.common.issues.push(l);
}
var ParseStatus = class e {
	constructor() {
		this.value = "valid";
	}
	dirty() {
		this.value === "valid" && (this.value = "dirty");
	}
	abort() {
		this.value !== "aborted" && (this.value = "aborted");
	}
	static mergeArray(e, i) {
		let a = [];
		for (let o of i) {
			if (o.status === "aborted") return INVALID;
			o.status === "dirty" && e.dirty(), a.push(o.value);
		}
		return {
			status: e.value,
			value: a
		};
	}
	static async mergeObjectAsync(i, a) {
		let o = [];
		for (let e of a) {
			let i = await e.key, a = await e.value;
			o.push({
				key: i,
				value: a
			});
		}
		return e.mergeObjectSync(i, o);
	}
	static mergeObjectSync(e, i) {
		let a = {};
		for (let o of i) {
			let { key: i, value: s } = o;
			if (i.status === "aborted" || s.status === "aborted") return INVALID;
			i.status === "dirty" && e.dirty(), s.status === "dirty" && e.dirty(), i.value !== "__proto__" && (s.value !== void 0 || o.alwaysSet) && (a[i.value] = s.value);
		}
		return {
			status: e.value,
			value: a
		};
	}
};
const INVALID = Object.freeze({ status: "aborted" }), DIRTY = (e) => ({
	status: "dirty",
	value: e
}), OK = (e) => ({
	status: "valid",
	value: e
}), isAborted = (e) => e.status === "aborted", isDirty = (e) => e.status === "dirty", isValid = (e) => e.status === "valid", isAsync = (e) => typeof Promise < "u" && e instanceof Promise;
export { DIRTY, INVALID, OK, ParseStatus, addIssueToContext, isAborted, isAsync, isDirty, isValid, makeIssue };
