import * as tslib_1 from "tslib";
import { getMetadataType } from '../../util';
import { KEY_PROPERTY } from './key-property.const';
export function initOrUpdateProperty(propertyMetadata, target, propertyKey) {
    if (propertyMetadata === void 0) { propertyMetadata = {}; }
    // the new or updated property
    var property;
    // Update the attribute array
    var properties = Reflect.getMetadata(KEY_PROPERTY, target.constructor) || [];
    var existingProperty = properties.find(function (p) { return p.name === propertyKey; });
    if (existingProperty) {
        // create new property with merged options
        property = tslib_1.__assign({}, existingProperty, propertyMetadata);
        // remove existing from array
        properties = properties.filter(function (p) { return p !== existingProperty; });
    }
    else {
        // add new options
        property = createNewProperty(propertyMetadata, target, propertyKey);
    }
    Reflect.defineMetadata(KEY_PROPERTY, tslib_1.__spread(properties, [property]), target.constructor);
}
function createNewProperty(propertyOptions, target, propertyKey) {
    if (propertyOptions === void 0) { propertyOptions = {}; }
    var propertyType = getMetadataType(target, propertyKey);
    var isCustom = isCustomType(propertyType);
    var typeInfo = {
        type: propertyType,
        isCustom: isCustom,
    };
    propertyOptions = tslib_1.__assign({ name: propertyKey, nameDb: propertyKey, typeInfo: typeInfo }, propertyOptions);
    return propertyOptions;
}
/**
 * TODO LOW:BINARY make sure to implement the context dependant details of Binary (Buffer vs. Uint8Array)
 * @returns {boolean} true if the type cannot be mapped by dynamo document client
 */
function isCustomType(type) {
    return type !== String && type !== Number && type !== Boolean && type !== Uint8Array;
}
//# sourceMappingURL=init-or-update-property.function.js.map