import { compareStrings } from "../utils/StringComparison.js";
import WarpError from "../errors/WarpError.js";
/** Runtime-backed patch frontier evidence for later migration equivalence checks. */
export default class GraphModelMigrationPatchFrontierEvidence {
    frontierKey;
    parentPatchIds;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.frontierKey = requireNonEmptyString(checkedFields.frontierKey, 'frontierKey');
        this.parentPatchIds = freezeParentPatchIds(checkedFields.parentPatchIds);
        Object.freeze(this);
    }
}
/** Validates the constructor envelope. */
function requireFields(fields) {
    if (fields === null || fields === undefined) {
        throw new WarpError('GraphModelMigrationPatchFrontierEvidence fields must be provided', 'E_VALIDATION');
    }
    return fields;
}
/** Validates and freezes parent patch ids as a deterministic set. */
function freezeParentPatchIds(parentPatchIds) {
    const checked = requireArray(parentPatchIds, 'parentPatchIds')
        .map((patchId) => requireNonEmptyString(patchId, 'parentPatchId'));
    requireUnique(checked, 'parent patch id');
    return Object.freeze([...checked].sort(compareStrings));
}
/** Requires an array field. */
function requireArray(items, label) {
    if (items === null || items === undefined || !Array.isArray(items)) {
        throw new WarpError(`GraphModelMigrationPatchFrontierEvidence ${label} must be an array`, 'E_VALIDATION');
    }
    const checkedItems = items;
    return checkedItems;
}
/** Validates a required non-empty string. */
function requireNonEmptyString(value, name) {
    if (typeof value !== 'string' || value.length === 0) {
        throw new WarpError(`${name} must be a non-empty string`, 'E_VALIDATION');
    }
    return value;
}
/** Requires no duplicate keys in a frontier section. */
function requireUnique(keys, label) {
    const seen = new Set();
    for (const key of keys) {
        if (seen.has(key)) {
            throw new WarpError(`GraphModelMigrationPatchFrontierEvidence duplicates ${label} ${key}`, 'E_VALIDATION');
        }
        seen.add(key);
    }
}
