import WarpError from "../errors/WarpError.js";
/** Runtime-backed source writer chain for graph-model migration planning. */
export default class GraphModelMigrationWriterChainDescriptor {
    writerId;
    patchIds;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.writerId = requireNonEmptyString(checkedFields.writerId, 'writerId');
        this.patchIds = freezePatchIds(checkedFields.patchIds);
        Object.freeze(this);
    }
}
/** Validates the constructor envelope. */
function requireFields(fields) {
    if (fields === null || fields === undefined) {
        throw new WarpError('GraphModelMigrationWriterChainDescriptor fields must be provided', 'E_VALIDATION');
    }
    return fields;
}
/** Validates and freezes patch ids. */
function freezePatchIds(patchIds) {
    const checked = requireArray(patchIds, 'patchIds').map((patchId) => requireNonEmptyString(patchId, 'patchId'));
    requireUnique(checked, 'writer chain patch id');
    return Object.freeze(checked);
}
/** Requires an array field. */
function requireArray(items, label) {
    if (items === null || items === undefined || !Array.isArray(items)) {
        throw new WarpError(`GraphModelMigrationWriterChainDescriptor ${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 chain section. */
function requireUnique(keys, label) {
    const seen = new Set();
    for (const key of keys) {
        if (seen.has(key)) {
            throw new WarpError(`GraphModelMigrationWriterChainDescriptor duplicates ${label} ${key}`, 'E_VALIDATION');
        }
        seen.add(key);
    }
}
