import WarpError from "../../errors/WarpError.js";
const ERROR_CODE = 'E_TTD_MERGE_INSPECTION_INVALID';
export function requireNonEmptyText(value, fieldName) {
    if (typeof value !== 'string' || value.trim().length === 0) {
        throw new WarpError(`${fieldName} must be a non-empty string`, ERROR_CODE);
    }
    return value;
}
function requireRecordContainer(fields, fieldName) {
    if (fields === null || typeof fields !== 'object' || Array.isArray(fields)) {
        throw new WarpError(`${fieldName} must be an object`, ERROR_CODE);
    }
}
function requireRecordEntry(key, value, fieldName) {
    if (key.length === 0) {
        throw new WarpError(`${fieldName} cannot contain an empty key`, ERROR_CODE);
    }
    if (typeof value !== 'string') {
        throw new WarpError(`${fieldName}.${key} must be a string`, ERROR_CODE);
    }
}
function copyStringRecord(fields, fieldName) {
    const copy = {};
    for (const [key, value] of Object.entries(fields)) {
        requireRecordEntry(key, value, fieldName);
        copy[key] = value;
    }
    return copy;
}
export function requireStringRecord(fields, fieldName) {
    requireRecordContainer(fields, fieldName);
    const copy = copyStringRecord(fields, fieldName);
    return freezeSortedRecord(copy);
}
export function freezeSortedRecord(fields) {
    const sorted = {};
    for (const key of Object.keys(fields).sort()) {
        const value = fields[key];
        if (value === undefined) {
            throw new WarpError(`${key} must have a string value`, ERROR_CODE);
        }
        sorted[key] = value;
    }
    return Object.freeze(sorted);
}
export function freezeSortedTexts(values, fieldName) {
    for (const value of values) {
        requireNonEmptyText(value, fieldName);
    }
    return Object.freeze([...new Set(values)].sort());
}
export function requireNonNegativeInteger(value, fieldName) {
    if (!Number.isInteger(value) || value < 0) {
        throw new WarpError(`${fieldName} must be a non-negative integer`, ERROR_CODE);
    }
    return value;
}
