"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveFeatureResourceName = deriveFeatureResourceName;
exports.resolveInputResourcePath = resolveInputResourcePath;
exports.resolvePayloadResourcePath = resolvePayloadResourcePath;
exports.resolveExpectedResourcePath = resolveExpectedResourcePath;
exports.resolveActualResourcePath = resolveActualResourcePath;
const node_path_1 = __importDefault(require("node:path"));
const LEGACY_PREFIXES = ['test-data/', 'src/test/resources/'];
function deriveFeatureResourceName(featureUri) {
    const normalized = featureUri.replace(/\\/g, '/');
    const baseName = node_path_1.default.posix.basename(normalized, node_path_1.default.posix.extname(normalized));
    if (!baseName) {
        throw new Error(`Unable to derive a feature resource folder from "${featureUri}".`);
    }
    return baseName;
}
function resolveInputResourcePath(config, featureName, filePath, cwd = process.cwd()) {
    return resolveFeatureScopedResourcePath(config.resourcePaths.input, featureName, filePath, cwd, 'input data');
}
function resolvePayloadResourcePath(config, featureName, filePath, cwd = process.cwd()) {
    return resolveFeatureScopedResourcePath(config.resourcePaths.payload, featureName, filePath, cwd, 'payload');
}
function resolveExpectedResourcePath(config, featureName, filePath, cwd = process.cwd()) {
    return resolveFeatureScopedResourcePath(config.resourcePaths.expected, featureName, filePath, cwd, 'expected result');
}
function resolveActualResourcePath(config, featureName, filePath, cwd = process.cwd()) {
    return resolveFeatureScopedResourcePath(config.resourcePaths.actual, featureName, filePath, cwd, 'actual artifact');
}
function resolveFeatureScopedResourcePath(root, featureName, filePath, cwd, purpose) {
    const normalizedPath = normalizeResourceRelativePath(filePath, purpose);
    if (!featureName) {
        throw new Error(`Cannot resolve ${purpose} "${filePath}" without an active feature context.`);
    }
    return node_path_1.default.resolve(cwd, root, featureName, normalizedPath);
}
function normalizeResourceRelativePath(filePath, purpose) {
    const normalizedPath = filePath.replace(/\\/g, '/').trim();
    if (!normalizedPath) {
        throw new Error(`A relative ${purpose} path is required.`);
    }
    if (node_path_1.default.isAbsolute(normalizedPath) || normalizedPath.split('/').includes('..')) {
        throw new Error(`Invalid relative ${purpose} path "${filePath}".`);
    }
    if (normalizedPath.startsWith('resources/')) {
        throw new Error(`Resource path "${filePath}" must be relative to its resource root, not start with "resources/".`);
    }
    const legacyPrefix = LEGACY_PREFIXES.find((prefix) => normalizedPath.startsWith(prefix));
    if (legacyPrefix) {
        throw new Error(`Legacy path "${filePath}" is not supported. Use a path relative to the configured resource root instead.`);
    }
    return normalizedPath;
}
