'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.default = mountHdb;
const { mkdirpSync, copySync } = require('fs-extra');
const path = __importStar(require("path"));
const terms = __importStar(require("../utility/hdbTerms.js"));
const harper_logger_ts_1 = __importDefault(require("../utility/logging/harper_logger.js"));
const harperBridge_ts_1 = __importDefault(require("../dataLayer/harperBridge/harperBridge.js"));
const systemSchema_json_1 = __importDefault(require("../json/systemSchema.json"));
const initPaths = __importStar(require("../dataLayer/harperBridge/lmdbBridge/lmdbUtility/initializePaths.js"));
const packageUtils_js_1 = require("../utility/packageUtils.js");
async function mountHdb(hdbPath) {
    harper_logger_ts_1.default.trace('Mounting Harper');
    makeDirectory(hdbPath);
    makeDirectory(path.join(hdbPath, 'backup'));
    makeDirectory(path.join(hdbPath, 'keys'));
    makeDirectory(path.join(hdbPath, 'log'));
    makeDirectory(path.join(hdbPath, 'database'));
    makeDirectory(path.join(hdbPath, 'components'));
    copySync(path.join(packageUtils_js_1.PACKAGE_ROOT, 'static/README.md'), path.join(hdbPath, 'README.md'));
    await createTables();
}
/**
 * creates the environments & dbis needed for lmdb  based on the systemSchema
 * @returns {Promise<void>}
 */
async function createTables() {
    const CreateTableObject = require('../dataLayer/CreateTableObject').default || require('../dataLayer/CreateTableObject');
    let tables = Object.keys(systemSchema_json_1.default);
    for (const tableName of tables) {
        let hash_attribute = systemSchema_json_1.default[tableName].hash_attribute;
        try {
            initPaths.initSystemSchemaPaths(terms.SYSTEM_SCHEMA_NAME, tableName);
            let createTable = new CreateTableObject(terms.SYSTEM_SCHEMA_NAME, tableName, hash_attribute);
            createTable.attributes = systemSchema_json_1.default[tableName].attributes;
            let primaryKeyAttribute = createTable.attributes.find(({ attribute }) => attribute === hash_attribute);
            primaryKeyAttribute.isPrimaryKey = true;
            // with RocksDB at least, we need to audit everything or there will be lost data
            createTable.audit = true;
            await harperBridge_ts_1.default.createTable(tableName, createTable);
        }
        catch (e) {
            harper_logger_ts_1.default.error(`issue creating environment for ${terms.SYSTEM_SCHEMA_NAME}.${tableName}: ${e}`);
            throw e;
        }
    }
}
function makeDirectory(targetDir) {
    mkdirpSync(targetDir, { mode: terms.HDB_FILE_PERMISSIONS });
    harper_logger_ts_1.default.info(`Directory ${targetDir} created`);
}
//# sourceMappingURL=mount_hdb.js.map