"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.emptyResult = emptyResult;
exports.mergeResults = mergeResults;
exports.fromZodIssues = fromZodIssues;
exports.errorResult = errorResult;
exports.warningResult = warningResult;
function pathToJsonPath(path) {
    return path.filter((p) => typeof p !== 'symbol');
}
function emptyResult() {
    return { valid: true, errors: [], warnings: [] };
}
function mergeResults(...results) {
    const merged = emptyResult();
    for (const result of results) {
        merged.errors.push(...result.errors);
        merged.warnings.push(...result.warnings);
    }
    merged.valid = merged.errors.length === 0;
    return merged;
}
function fromZodIssues(issues, pathPrefix = [], enrichIssue) {
    const result = emptyResult();
    for (const issue of issues) {
        const refined = refineIssue(issue);
        const advisory = enrichIssue?.(issue);
        result.errors.push({
            path: [...pathPrefix, ...refined.path],
            message: refined.message,
            code: refined.code,
            ...(advisory?.suggestion !== undefined && {
                suggestion: advisory.suggestion,
            }),
            ...(advisory?.example !== undefined && { example: advisory.example }),
        });
    }
    result.valid = result.errors.length === 0;
    return result;
}
function refineIssue(issue) {
    if (issue.code === 'invalid_union') {
        const findTargetIssue = locateFindTargetUnionIssue(issue);
        if (findTargetIssue) {
            return {
                path: [...pathToJsonPath(issue.path), ...findTargetIssue.path],
                message: 'Find target has no locator — set a trained selector, CSS query, or XPath.',
                code: 'find_target.missing_locator',
            };
        }
    }
    return {
        path: pathToJsonPath(issue.path),
        message: issue.message,
        code: `zod.${issue.code}`,
    };
}
function locateFindTargetUnionIssue(issue) {
    for (const branchIssues of issue.errors) {
        for (const branchIssue of branchIssues) {
            if (branchIssue.code === 'invalid_union' &&
                branchIssue.path[branchIssue.path.length - 1] === 'findTarget') {
                return { path: pathToJsonPath(branchIssue.path) };
            }
        }
    }
    return undefined;
}
function errorResult(path, message, code, advisory) {
    return {
        valid: false,
        errors: [
            {
                path,
                message,
                code,
                ...(advisory?.suggestion !== undefined && {
                    suggestion: advisory.suggestion,
                }),
                ...(advisory?.example !== undefined && { example: advisory.example }),
            },
        ],
        warnings: [],
    };
}
function warningResult(path, message, code, advisory) {
    return {
        valid: true,
        errors: [],
        warnings: [
            {
                path,
                message,
                code,
                ...(advisory?.suggestion !== undefined && {
                    suggestion: advisory.suggestion,
                }),
                ...(advisory?.example !== undefined && { example: advisory.example }),
            },
        ],
    };
}
