import is from "@sindresorhus/is";
import { z } from "zod";
//#region src/lib/utilities/schema-primitives.ts
/**
* Shared Zod schema primitives for parser schemas.
*
* These handle common coercions:
*   - Empty/whitespace-only strings → undefined
*   - URL validation with empty → undefined fallback
*   - String arrays with empty-element filtering
*/
/**
* A string that treats empty or whitespace-only values as undefined.
* Use `.optional()` on the result for optional fields.
*/
const nonEmptyString = z.preprocess((value) => {
	if (typeof value !== "string") return;
	const trimmed = value.trim();
	return trimmed.length > 0 ? trimmed : void 0;
}, z.string().optional());
/**
* A URL string that treats empty/whitespace as undefined.
* Does not reject invalid URLs — just ensures non-empty.
* Use z.url() in place of this if strict URL validation is desired.
*/
const optionalUrl = nonEmptyString.describe("A URL string");
/**
* An array of strings, filtering out empty/whitespace-only elements.
*/
const stringArray = z.preprocess((value) => {
	if (!Array.isArray(value)) return [];
	return value.filter((item) => typeof item === "string" && item.trim().length > 0);
}, z.array(z.string()));
/**
* Parse a JSON string into a plain object.
* Returns `undefined` for malformed JSON or non-object values (arrays, primitives).
*/
function parseJsonRecord(content) {
	try {
		const parsed = JSON.parse(content);
		return is.plainObject(parsed) ? parsed : void 0;
	} catch {
		return;
	}
}
//#endregion
export { nonEmptyString, optionalUrl, parseJsonRecord, stringArray };
