"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readRecord = readRecord;
exports.readArray = readArray;
exports.readStringArray = readStringArray;
exports.readRequiredNumber = readRequiredNumber;
exports.readGeneratedAt = readGeneratedAt;
exports.hasRequiredValue = hasRequiredValue;
exports.formatFieldList = formatFieldList;
exports.toErrorMessage = toErrorMessage;
function readRecord(input, label) {
    if (typeof input !== "object" || input === null || Array.isArray(input)) {
        throw new TypeError(`${label} must be an object.`);
    }
    return input;
}
function readArray(input, label) {
    if (!Array.isArray(input)) {
        throw new TypeError(`${label} must be an array.`);
    }
    return input;
}
function readStringArray(input, label) {
    const values = readArray(input, label);
    for (const value of values) {
        if (typeof value !== "string") {
            throw new TypeError(`${label} must only contain strings.`);
        }
    }
    return values;
}
function readRequiredNumber(record, field) {
    const value = record[field];
    if (typeof value !== "number" || Number.isNaN(value)) {
        throw new TypeError(`Spec index document field '${field}' must be a number.`);
    }
    return value;
}
function readGeneratedAt(input) {
    if (input === null) {
        return null;
    }
    if (typeof input !== "string") {
        throw new TypeError("Spec index document field 'generated_at' must be a string or null.");
    }
    return input;
}
function hasRequiredValue(value) {
    if (value === undefined || value === null) {
        return false;
    }
    if (typeof value === "string") {
        return value.trim().length > 0;
    }
    return true;
}
function formatFieldList(fields) {
    if (fields.length === 1) {
        return `'${fields[0]}'`;
    }
    return fields.map((field) => `'${field}'`).join(", ");
}
function toErrorMessage(error, fallback) {
    return error instanceof Error ? error.message : fallback;
}
