"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.convertDelete = convertDelete;
const alasql = __importStar(require("alasql"));
const search = __importStar(require("../dataLayer/search.js"));
const harper_logger_ts_1 = __importDefault(require("../utility/logging/harper_logger.js"));
const harperBridge_ts_1 = __importDefault(require("../dataLayer/harperBridge/harperBridge.js"));
const util = __importStar(require("util"));
const hdbUtils = __importStar(require("../utility/common_utils.js"));
const terms = __importStar(require("../utility/hdbTerms.js"));
const globalSchema = __importStar(require("../utility/globalSchema.js"));
const RECORD = 'record';
const SUCCESS = 'successfully deleted';
const pSearchSearch = util.promisify(search.search);
const pGetTableSchema = util.promisify(globalSchema.getTableSchema);
function generateReturnMessage(deleteResultsObject) {
    return `${deleteResultsObject.deleted_hashes.length} ${RECORD}${deleteResultsObject.deleted_hashes.length === 1 ? `` : `s`} ${SUCCESS}`;
}
async function convertDelete({ statement, hdb_user }) {
    //convert this update statement to a search capable statement
    let tableInfo = await pGetTableSchema(statement.table.databaseid, statement.table.tableid);
    //convert this delete statement to a SQL search capable statement
    hdbUtils.backtickASTSchemaItems(statement);
    let { table: from, where } = statement;
    let whereString = hdbUtils.isEmpty(where) ? '' : ` WHERE  ${where.toString()}`;
    let selectString = `SELECT ${tableInfo.hash_attribute} FROM ${from.toString()} ${whereString}`;
    let searchStatement = alasql.parse(selectString).statements[0];
    let deleteObj = {
        operation: terms.OPERATIONS_ENUM.DELETE,
        schema: from.databaseid_orig,
        table: from.tableid_orig,
        hdb_user,
    };
    try {
        //let result = await transaction.writeTransaction(tableInfo.schema, tableInfo.name, async () => {
        deleteObj.records = await pSearchSearch(searchStatement);
        let result = await harperBridge_ts_1.default.deleteRecords(deleteObj);
        //});
        //await write.flush({ schema: tableInfo.schema, table: tableInfo.name });
        if (hdbUtils.isEmptyOrZeroLength(result.message)) {
            result.message = generateReturnMessage(result);
        }
        delete result.txn_time;
        return result;
    }
    catch (err) {
        harper_logger_ts_1.default.error(err);
        if (err.hdb_code) {
            throw err.message;
        }
        throw err;
    }
}
//# sourceMappingURL=deleteTranslator.js.map