'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callOperationFunctionAsAwait = callOperationFunctionAsAwait;
const harper_logger_ts_1 = __importDefault(require("./logging/harper_logger.js"));
const terms = __importStar(require("./hdbTerms.js"));
/**
 * Calls the operation function specified in the parameter with the input specified in the parameter.  Once complete,
 * calls the response function in the parameter with  the operation result as the first parameter.
 * @param promisifiedFunction - The operation which is in async/await format
 * @param functionInput - The input needed for the operationFunctionAsCallback function.
 * @param followupAsyncFunc - The response function that will be called with the operation function response as an input.  The function is expected to be promisifed, callbacks not supported.
 * @returns {Promise<{}>}
 */
async function callOperationFunctionAsAwait(promisifiedFunction, functionInput, followupAsyncFunc) {
    if (!promisifiedFunction || typeof promisifiedFunction !== 'function') {
        throw new Error('Invalid function parameter');
    }
    let result = undefined;
    try {
        result = await promisifiedFunction(functionInput);
        if (followupAsyncFunc) {
            //TODO: Passing result twice seems silly, why is this a thing?
            await followupAsyncFunc(functionInput, result);
        }
        // The result from insert, update, or upsert contains a properties new_attributes/txnTime. It is used by postOperationHandler to propagate
        // attribute metadata. After the property has been used we no longer need it and do not want the API returning it,
        // therefore we delete it from the result.
        if (functionInput.operation === terms.OPERATIONS_ENUM.INSERT ||
            functionInput.operation === terms.OPERATIONS_ENUM.UPDATE ||
            functionInput.operation === terms.OPERATIONS_ENUM.UPSERT) {
            delete result.new_attributes;
            delete result.txn_time;
        }
        else if (functionInput.operation === terms.OPERATIONS_ENUM.DELETE) {
            delete result.txn_time;
        }
        return result;
    }
    catch (err) {
        // This specific check was added to avoid an error message in the log which could make the error look worse than it
        // seems when scanning a log.  In reality a schema already existing isn't really an error, just a failure.
        if (err.message && typeof err.message === 'string' && err.message.includes('already exists')) {
            harper_logger_ts_1.default.info(err.message);
            throw err;
        }
        // This check is here to make sure a new HdbError is logged correctly
        if (err.http_resp_msg) {
            harper_logger_ts_1.default.error(`Error calling operation: ${promisifiedFunction.name}`);
            harper_logger_ts_1.default.error(err.http_resp_msg);
            throw err;
        }
        harper_logger_ts_1.default.error(`Error calling operation: ${promisifiedFunction.name}`);
        harper_logger_ts_1.default.error(err);
        throw err;
    }
}
//# sourceMappingURL=OperationFunctionCaller.js.map