"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sqlite_1 = require("sqlite");
const sqlite3_1 = __importDefault(require("sqlite3"));
var Databases;
(function (Databases) {
    function open(dbname) {
        var database = new sqlite_1.Database({ filename: __dirname + `/databases/${dbname}.db`, driver: sqlite3_1.default.Database });
        database.open();
        return database;
    }
    Databases.open = open;
    function get(table, find) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            if (find == 'all') {
                return database.all(`SELECT * FROM '${table}'`);
            }
            else {
                var keys = Object.keys(find), vals = Object.values(find);
                var commands = [];
                for (var k in keys) {
                    commands.push(`${keys[k]} = '${vals[k]}'`);
                }
                var qu = commands.join(' AND ');
                return database.all(`SELECT * FROM '${table}' WHERE ${qu}`);
            }
        });
    }
    Databases.get = get;
    function getLast(table, find, count = 0) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            if (find == 'all') {
                var data = yield database.all(`SELECT * FROM '${table}'`);
                return data.reverse()[count];
            }
            else {
                var keys = Object.keys(find), vals = Object.values(find);
                var commands = [];
                for (var k in keys) {
                    commands.push(`${keys[k]} = '${vals[k]}'`);
                }
                var qu = commands.join(' AND ');
                var data = yield database.all(`SELECT * FROM '${table}' WHERE ${qu}`);
                return data.reverse()[count];
            }
        });
    }
    Databases.getLast = getLast;
    function getOne(table, find) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            if (find == 'all') {
                return database.all(`SELECT * FROM '${table}'`);
            }
            else {
                var keys = Object.keys(find), vals = Object.values(find);
                var commands = [];
                for (var k in keys) {
                    commands.push(`${keys[k]} = '${vals[k]}'`);
                }
                var qu = commands.join(' AND ');
                return database.get(`SELECT * FROM '${table}' WHERE ${qu}`);
            }
        });
    }
    Databases.getOne = getOne;
    function add(table, insert) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            var last = yield getLast(table, 'all');
            var addid = (last === null || last === void 0 ? void 0 : last.id) || 0;
            var vals = Object.values(insert);
            vals.unshift(addid + 1);
            var _vals = vals.map((x) => `'${x}'`).join(',');
            var keys = Object.keys(insert);
            keys.unshift('id');
            var _keys = keys.join(',');
            var query = `INSERT INTO ${table} (${_keys}) VALUES(${_vals})`;
            return database.exec(query);
        });
    }
    Databases.add = add;
    function del(table, del) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            if (del == 'all') {
                return database.exec(`DELETE FROM ${table}`);
            }
            else {
                var keys = Object.keys(del), vals = Object.values(del);
                var commands = [];
                for (var k in keys) {
                    commands.push(`${keys[k]} = '${vals[k]}'`);
                }
                var qu = commands.join(' AND ');
                return database.exec(`DELETE FROM '${table}' WHERE ${qu}`);
            }
        });
    }
    Databases.del = del;
    function search(table, find) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            if (find == 'all') {
                return database.all(`SELECT * FROM '${table}'`);
            }
            else {
                var keys = Object.keys(find), vals = Object.values(find);
                var commands = [];
                for (var k in keys) {
                    commands.push(`${keys[k]} LIKE '%${vals[k]}%'`);
                }
                var qu = commands.join(' AND ');
                return database.all(`SELECT * FROM '${table}' WHERE ${qu}`);
                // var qu = Object.entries(find)[0]
                // return database.all(`SELECT * FROM '${table}' WHERE ${qu[0]} LIKE '%${qu[1]}%'`)
            }
        });
    }
    Databases.search = search;
    function update(table, find, updates) {
        return __awaiter(this, void 0, void 0, function* () {
            var database = open('data');
            var query = `UPDATE ${table} SET `;
            var ukeys = Object.keys(updates), uvals = Object.values(updates);
            var commands = [];
            for (var k in ukeys) {
                commands.push(`${ukeys[k]} = '${uvals[k]}'`);
            }
            var qu = commands.join(',');
            var fin = Object.entries(find)[0];
            return database.exec(`UPDATE ${table} SET ${commands} WHERE ${fin[0]} = '${fin[1]}'`);
        });
    }
    Databases.update = update;
    class Sqlite extends sqlite_1.Database {
        constructor(name) {
            super({ filename: `./config/${name}.db`, driver: sqlite3_1.default.Database });
        }
        createTable(tbname, table) {
            this.open();
            var template = table.map((v, i) => `'${v}' varchar(255) NULL`).join(',');
            this.exec(`CREATE TABLE '${tbname}' (id int NOT NULL,${template},PRIMARY KEY (id));`);
            return this;
        }
        fetchbyId(id) {
        }
        fetchAll(tbname) {
            return __awaiter(this, void 0, void 0, function* () {
                var res = yield this.all(`select * from ${tbname}`);
                return res;
            });
        }
    }
    Databases.Sqlite = Sqlite;
    class Users {
        static findUser() {
        }
    }
    Databases.Users = Users;
})(Databases || (Databases = {}));
exports.default = Databases;
