import { initOrUpdateProperty } from '../property/init-or-update-property.function';
import { KEY_PROPERTY } from '../property/key-property.const';
import { IndexType } from './index-type.enum';
export function initOrUpdateIndex(indexType, indexData, target, propertyKey) {
    var properties = Reflect.getMetadata(KEY_PROPERTY, target.constructor) || [];
    var existingProperty = properties.find(function (property) { return property.name === propertyKey; });
    var propertyMetadata;
    switch (indexType) {
        case IndexType.GSI:
            propertyMetadata = initOrUpdateGSI(existingProperty && existingProperty.keyForGSI ? existingProperty.keyForGSI : {}, indexData);
            break;
        case IndexType.LSI:
            propertyMetadata = initOrUpdateLSI(existingProperty && existingProperty.sortKeyForLSI ? existingProperty.sortKeyForLSI : [], indexData);
            break;
        default:
            throw new Error("unsupported index type " + indexType);
    }
    initOrUpdateProperty(propertyMetadata, target, propertyKey);
}
function initOrUpdateGSI(indexes, indexData) {
    if (indexes[indexData.name]) {
        // TODO LOW:INVESTIGATE when we throw an error we have a problem where multiple different classes extend one base class, this will be executed multiple times
        // throw new Error(
        //   'the property with name is already registered as key for index - one property can only define one key per index'
        // )
    }
    else {
        indexes[indexData.name] = indexData.keyType;
    }
    return { keyForGSI: indexes };
}
function initOrUpdateLSI(indexes, indexData) {
    indexes.push(indexData.name);
    return { sortKeyForLSI: indexes };
}
//# sourceMappingURL=util.js.map