"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerJsonPathAssertSteps = registerJsonPathAssertSteps;
function resolveJsonPath(from, path) {
    let normalized = path.trim();
    if (normalized.startsWith("$.")) {
        normalized = normalized.slice(2);
    }
    else if (normalized === "$") {
        return from;
    }
    normalized = normalized.replace(/\[(\d+)\]/g, ".$1");
    const parts = normalized.split(".").filter(Boolean);
    let current = from;
    for (const part of parts) {
        if (current == null)
            return undefined;
        current = current[part];
    }
    return current;
}
function registerJsonPathAssertSteps(registry) {
    registry.register("assert.jsonPath", async (_ctx, step) => {
        const input = (step.with ?? {});
        if (!("from" in input)) {
            throw new Error('Missing "from" in step.with for op "assert.jsonPath"');
        }
        if (!input.path) {
            throw new Error('Missing "path" in step.with for op "assert.jsonPath"');
        }
        if (!("equals" in input)) {
            throw new Error('Missing "equals" in step.with for op "assert.jsonPath"');
        }
        const actual = resolveJsonPath(input.from, input.path);
        if (actual !== input.equals) {
            throw new Error(`Expected JSONPath "${input.path}" to equal ${String(input.equals)} but got ${String(actual)}`);
        }
    });
}
//# sourceMappingURL=jsonpath.step.js.map