"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../../util");
const key_property_const_1 = require("./key-property.const");
function initOrUpdateProperty(propertyMetadata = {}, target, propertyKey) {
    // the new or updated property
    let property;
    // Update the attribute array
    let properties = Reflect.getMetadata(key_property_const_1.KEY_PROPERTY, target.constructor) || [];
    const existingProperty = properties.find(p => p.name === propertyKey);
    if (existingProperty) {
        // create new property with merged options
        property = Object.assign({}, existingProperty, propertyMetadata);
        // remove existing from array
        properties = properties.filter(p => p !== existingProperty);
    }
    else {
        // add new options
        property = createNewProperty(propertyMetadata, target, propertyKey);
    }
    Reflect.defineMetadata(key_property_const_1.KEY_PROPERTY, [...properties, property], target.constructor);
}
exports.initOrUpdateProperty = initOrUpdateProperty;
function createNewProperty(propertyOptions = {}, target, propertyKey) {
    const propertyType = util_1.getMetadataType(target, propertyKey);
    const isCustom = isCustomType(propertyType);
    const typeInfo = {
        type: propertyType,
        isCustom,
    };
    propertyOptions = Object.assign({ name: propertyKey, nameDb: propertyKey, 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