"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStructuredCommandInput = parseStructuredCommandInput;
exports.requireString = requireString;
exports.requireStringArray = requireStringArray;
exports.optionalBoundedInteger = optionalBoundedInteger;
function parseStructuredCommandInput(raw, allowedFields) {
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        throw new Error("--input-json must be valid JSON.");
    }
    if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
        throw new Error("--input-json must decode to an object.");
    }
    const allowed = new Set(allowedFields);
    const unknown = Object.keys(parsed).filter((field) => !allowed.has(field)).sort();
    if (unknown.length > 0) {
        throw new Error(`--input-json contains unknown field(s): ${unknown.join(", ")}.`);
    }
    return parsed;
}
function requireString(value, field) {
    if (typeof value !== "string" || value.trim().length === 0) {
        throw new Error(`Structured field '${field}' must be a non-empty string.`);
    }
    return value.trim();
}
function requireStringArray(value, field, required) {
    if (value === undefined && !required)
        return [];
    if (!Array.isArray(value) || (required && value.length === 0)
        || value.some((entry) => typeof entry !== "string" || entry.trim().length === 0)) {
        throw new Error(`Structured field '${field}' must be ${required ? "a non-empty" : "an"} array of non-empty strings.`);
    }
    return Object.freeze(value.map((entry) => entry.trim()));
}
function optionalBoundedInteger(value, field, defaultValue, minimum, maximum) {
    if (value === undefined)
        return defaultValue;
    if (!Number.isInteger(value) || value < minimum || value > maximum) {
        throw new Error(`Structured field '${field}' must be an integer from ${minimum} to ${maximum}.`);
    }
    return value;
}
