export function isJsonRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function jsonRecord(value) {
    return isJsonRecord(value) ? value : undefined;
}
export function stringValue(value) {
    return typeof value === "string" ? value : undefined;
}
export function trimmedString(value) {
    const text = stringValue(value)?.trim();
    return text === undefined || text.length === 0 ? undefined : text;
}
export function stringList(value) {
    if (!Array.isArray(value)) {
        return [];
    }
    return [...new Set(value.map((item) => trimmedString(item) ?? "").filter(Boolean))];
}
export function optionalStringArray(value, options = {}) {
    if (value === undefined) {
        return { ok: true, values: [] };
    }
    if (!Array.isArray(value)) {
        return { ok: false };
    }
    const trim = options.trim === true;
    const allowEmpty = options.allowEmpty === true;
    const output = [];
    for (const item of value) {
        if (typeof item !== "string") {
            return { ok: false };
        }
        const normalized = trim ? item.trim() : item;
        if (!allowEmpty && normalized.length === 0) {
            return { ok: false };
        }
        output.push(normalized);
    }
    return { ok: true, values: [...new Set(output)] };
}
export function hasAffirmativeGuard(context) {
    const guard = jsonRecord(jsonRecord(context)?.guard);
    return guard?.allowed === true || guard?.accepted === true;
}
export function explicitGuardDenied(context) {
    const guard = jsonRecord(jsonRecord(context)?.guard);
    return guard?.allowed === false || guard?.accepted === false;
}
export function runtimeIdIsMalformed(context) {
    const runtimeId = jsonRecord(context)?.runtimeId;
    return runtimeId !== undefined && typeof runtimeId !== "string";
}
export function contextAuditMetadata(context) {
    return jsonRecord(jsonRecord(context)?.auditMetadata) ?? {};
}
