import { ColumnValidator } from '../../base/models/column-validator';
import { resolveKeyWithType } from './mapping-key-resolver';
export const enumBaseValidityFlagNames = [
    'invalidMappingKeyValueForType',
    'duplicateMappingKey',
    'missingKeyValue'
];
/**
 * Validator base class for table columns containing mappings. Implementations MUST include enumBaseValidityFlagNames in validity flag names set.
 */
export class TableColumnEnumBaseValidator extends ColumnValidator {
    constructor(configValidityKeys) {
        super(configValidityKeys);
    }
    validate(mappings, keyType) {
        this.untrackAll();
        const keys = mappings.map(mapping => mapping.key);
        this.validateKeyValuesForType(keys, keyType);
        this.validateUniqueKeys(keys, keyType);
        this.validateNoMissingKeys(mappings);
    }
    validateKeyValuesForType(keys, keyType) {
        // Ignore undefined keys, because validateNoMissingKeys covers that case.
        // We should only set 'invalidMappingKeyValueForType' when there is a key,
        // but it isn't appropriate for the type.
        const invalid = keys.some(key => key !== undefined
            && resolveKeyWithType(key, keyType) === undefined);
        this.setConditionValue('invalidMappingKeyValueForType', invalid);
    }
    validateUniqueKeys(keys, keyType) {
        const typedKeys = keys.map(x => resolveKeyWithType(x, keyType));
        const invalid = new Set(typedKeys).size !== typedKeys.length;
        this.setConditionValue('duplicateMappingKey', invalid);
    }
    validateNoMissingKeys(mappings) {
        const invalid = mappings.some(mapping => mapping.key === undefined);
        this.setConditionValue('missingKeyValue', invalid);
    }
}
//# sourceMappingURL=table-column-enum-base-validator.js.map