'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 });
const environmentUtility = __importStar(require("./environmentUtility.js"));
const harper_logger_ts_1 = __importDefault(require("../logging/harper_logger.js"));
const commonErrors_ts_1 = require("../errors/commonErrors.js");
exports.default = cleanLMDBMap;
/**
 * this function strips away the cached environments from global when a schema item is removed
 * @param msg
 */
async function cleanLMDBMap(msg) {
    try {
        if (global.lmdb_map !== undefined && msg.operation !== undefined) {
            let keys = Object.keys(global.lmdb_map);
            let cachedEnvironment = undefined;
            switch (msg.operation) {
                case 'drop_schema':
                    for (let x = 0; x < keys.length; x++) {
                        let key = keys[x];
                        if (key.startsWith(`${msg.schema}.`) || key.startsWith(`txn.${msg.schema}.`)) {
                            try {
                                await environmentUtility.closeEnvironment(global.lmdb_map[key]);
                            }
                            catch (err) {
                                if (err.message !== commonErrors_ts_1.LMDB_ERRORS_ENUM.ENV_REQUIRED) {
                                    throw err;
                                }
                            }
                        }
                    }
                    break;
                case 'drop_table':
                    // eslint-disable-next-line no-case-declarations
                    let schemaTableName = `${msg.schema}.${msg.table}`;
                    // eslint-disable-next-line no-case-declarations
                    let txnSchemaTableName = `txn.${schemaTableName}`;
                    try {
                        await environmentUtility.closeEnvironment(global.lmdb_map[schemaTableName]);
                        await environmentUtility.closeEnvironment(global.lmdb_map[txnSchemaTableName]);
                    }
                    catch (err) {
                        if (err.message !== commonErrors_ts_1.LMDB_ERRORS_ENUM.ENV_REQUIRED) {
                            throw err;
                        }
                    }
                    break;
                case 'drop_attribute':
                    cachedEnvironment = global.lmdb_map[`${msg.schema}.${msg.table}`];
                    if (cachedEnvironment !== undefined &&
                        typeof cachedEnvironment.dbis === 'object' &&
                        cachedEnvironment.dbis[`${msg.attribute}`] !== undefined) {
                        delete cachedEnvironment.dbis[`${msg.attribute}`];
                    }
                    break;
                default:
                    break;
            }
        }
    }
    catch (e) {
        harper_logger_ts_1.default.error(e);
    }
}
//# sourceMappingURL=cleanLMDBMap.js.map