"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../primordials/error.js');
const require_primordials_number = require('../primordials/number.js');
const require_primordials_array = require('../primordials/array.js');
const require_primordials_object = require('../primordials/object.js');

//#region src/schema/validate.ts
/**
* @file Universal schema validator — non-throwing. Accepts any Zod-shaped
*   schema (`.safeParse`-exposing) and returns a tagged result `{ ok: true,
*   value } | { ok: false, errors }` with normalized `{ path, message }`
*   issues. No runtime dependency on `zod` — detection is purely structural.
*
* @example
*   ;```ts
*   import { z } from 'zod'
*   import { validateSchema } from '@socketsecurity/lib/schema/validate'
*
*   const User = z.object({ name: z.string() })
*   const r = validateSchema(User, data)
*   if (r.ok)
*     r.value.name // string
*   else r.errors // ValidationIssue[]
*   ```
*
* @internal
* socket-lib additionally recognizes TypeBox schemas for its own internal
* use (e.g. `src/ipc.ts`'s stub-file validation). That path is not a
* supported consumer API.
*/
/**
* Detect a TypeBox schema structurally: object with a symbol key whose
* description is `'TypeBox.Kind'`, holding a string value.
*
* @internal
*/
function isTypeBoxSchema(schema) {
	if (schema === null || typeof schema !== "object") return false;
	for (const sym of require_primordials_object.ObjectGetOwnPropertySymbols(schema)) if (sym.description === "TypeBox.Kind") return typeof schema[sym] === "string";
	return false;
}
/**
* Normalize a TypeBox `ValueError` iterator into plain issues. TypeBox paths
* are JSON Pointers (`/user/0/name`); convert to arrays.
*
* @internal
*/
function normalizeTypeBoxErrors(errors) {
	const out = [];
	for (const err of errors) {
		const segs = err.path.split("/").filter(Boolean);
		out.push({
			path: segs.map((s) => {
				const n = Number(s);
				return require_primordials_number.NumberIsInteger(n) && String(n) === s ? n : s;
			}),
			message: err.message
		});
	}
	return out;
}
/**
* Normalize a Zod error object (v3 or v4) into plain issues. Both versions
* expose `.issues: Array<{ path, message }>`.
*
* @internal
*/
function normalizeZodError(err) {
	if (err === null || typeof err !== "object") return [{
		path: [],
		message: String(err)
	}];
	const issues = err.issues;
	if (!require_primordials_array.ArrayIsArray(issues)) return [{
		path: [],
		message: "Unknown validation error"
	}];
	return issues.map((issue) => {
		const i = issue;
		return {
			path: require_primordials_array.ArrayIsArray(i.path) ? i.path : [],
			message: typeof i.message === "string" ? i.message : "Invalid value"
		};
	});
}
/**
* Validate `data` against a Zod-style `schema`. Non-throwing.
*
* The return type narrows `value` to `Infer<S>`, so callers get `z.infer<typeof
* S>` with no casts. Errors are normalized to `{ path, message }` regardless of
* the underlying validator.
*
* @throws {TypeError} When `schema` is not a recognized validator kind.
*/
function validateSchema(schema, data) {
	if (isTypeBoxSchema(schema)) {
		const { Value } = require("../external/@sinclair/typebox/value");
		if (Value.Check(schema, data)) return {
			ok: true,
			value: data
		};
		return {
			ok: false,
			errors: normalizeTypeBoxErrors(Value.Errors(schema, data))
		};
	}
	if (schema !== null && typeof schema === "object" && typeof schema.safeParse === "function") {
		const result = schema.safeParse(data);
		if (result.success === true) return {
			ok: true,
			value: result.data
		};
		return {
			ok: false,
			errors: normalizeZodError(result.error)
		};
	}
	throw new require_primordials_error.TypeErrorCtor("validateSchema: unsupported schema kind. Expected a Zod schema or an object with a safeParse method.");
}

//#endregion
exports.isTypeBoxSchema = isTypeBoxSchema;
exports.normalizeTypeBoxErrors = normalizeTypeBoxErrors;
exports.normalizeZodError = normalizeZodError;
exports.validateSchema = validateSchema;