"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveBindingPreDestroy = resolveBindingPreDestroy;
const common_1 = require("@inversifyjs/common");
const BindingType_1 = require("../../binding/models/BindingType");
const CACHE_KEY_TYPE = 'cache';
function resolveBindingPreDestroy(params, binding) {
    if (binding.type === BindingType_1.bindingTypeValues.Instance) {
        const classMetadata = params.getClassMetadata(binding.implementationType);
        const instance = binding.cache
            .value;
        if ((0, common_1.isPromise)(instance)) {
            return instance.then((instance) => resolveInstancePreDestroyMethods(classMetadata, instance));
        }
        else {
            return resolveInstancePreDestroyMethods(classMetadata, instance);
        }
    }
}
function resolveInstancePreDestroyMethod(instance, methodName) {
    if (typeof instance[methodName] === 'function') {
        const result = instance[methodName]();
        return result;
    }
}
function resolveInstancePreDestroyMethods(classMetadata, instance) {
    const preDestroyMethodNames = classMetadata.lifecycle.preDestroyMethodNames;
    if (preDestroyMethodNames.size === 0) {
        return;
    }
    let result = undefined;
    for (const methodName of preDestroyMethodNames) {
        if (result === undefined) {
            result = resolveInstancePreDestroyMethod(instance, methodName);
        }
        else {
            result = result.then(() => resolveInstancePreDestroyMethod(instance, methodName));
        }
    }
    return result;
}
//# sourceMappingURL=resolveBindingPreDestroy.js.map