import { hasGenericType } from '../../decorator/metadata';
function enumToDb(value, propertyMetadata) {
    if (Number.isInteger(value)) {
        if (hasGenericType(propertyMetadata) && propertyMetadata.typeInfo.genericType[value] === undefined) {
            throw new Error(`${value} is not a valid value for enum ${propertyMetadata.typeInfo.genericType}`);
        }
        return { N: value.toString() };
    }
    else {
        throw new Error('only integer is a supported value for an enum');
    }
}
function enumFromDb(attributeValue, propertyMetadata) {
    if (!isNaN(parseInt(attributeValue.N, 10))) {
        const enumValue = parseInt(attributeValue.N, 10);
        if (propertyMetadata && propertyMetadata.typeInfo && propertyMetadata.typeInfo.genericType) {
            if (propertyMetadata.typeInfo.genericType[enumValue] === undefined) {
                throw new Error(`${enumValue} is not a valid value for enum ${propertyMetadata.typeInfo.genericType}`);
            }
        }
        return enumValue;
    }
    else {
        throw new Error('make sure the value is a N(umber), which is the only supported for EnumMapper right now');
    }
}
/**
 * Enums are mapped to numbers by default
 */
export const EnumMapper = {
    fromDb: enumFromDb,
    toDb: enumToDb,
};
//# sourceMappingURL=enum.mapper.js.map