export function readRecord(value) {
    if (value === null || typeof value !== "object" || Array.isArray(value)) {
        return undefined;
    }
    return value;
}
function isPlainObject(value) {
    const prototype = Object.getPrototypeOf(value);
    return prototype === Object.prototype || prototype === null;
}
export function isPlainJsonValue(value, seen = new WeakSet()) {
    if (value === null || typeof value === "string" || typeof value === "boolean") {
        return true;
    }
    if (typeof value === "number") {
        return Number.isFinite(value);
    }
    if (typeof value !== "object") {
        return false;
    }
    if (seen.has(value)) {
        return false;
    }
    seen.add(value);
    if (Array.isArray(value)) {
        return value.every((item) => isPlainJsonValue(item, seen));
    }
    if (!isPlainObject(value)) {
        return false;
    }
    return Object.values(value).every((item) => isPlainJsonValue(item, seen));
}
export function plainJsonRecord(value) {
    const record = readRecord(value);
    if (record === undefined || !isPlainJsonValue(record)) {
        return undefined;
    }
    return record;
}
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 booleanValue(value) {
    return typeof value === "boolean" ? value : undefined;
}
export function integerValue(value) {
    return typeof value === "number" && Number.isSafeInteger(value) ? value : undefined;
}
export function cleanStringList(values) {
    if (!Array.isArray(values)) {
        return [];
    }
    return [
        ...new Set(values
            .map((value) => trimmedString(value))
            .filter((value) => value !== undefined)),
    ];
}
export function cleanNumberList(values) {
    if (!Array.isArray(values)) {
        return [];
    }
    return [...new Set(values.filter((value) => Number.isSafeInteger(value)))];
}
export function normalizeDirectory(directory) {
    const trimmed = directory.trim();
    return trimmed === "/" ? trimmed : trimmed.replace(/\/+$/, "");
}
export function safeMetadata(value) {
    return plainJsonRecord(value) ?? {};
}
export function safeRecordKeys(value) {
    const record = readRecord(value);
    return record === undefined ? [] : cleanStringList(Object.keys(record));
}
export function approvalRecord(context) {
    return readRecord(readRecord(context)?.approval);
}
