"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 __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Path = __importStar(require("path"));
const FS = __importStar(require("fs"));
const DB = __importStar(require("./db"));
const Utils = __importStar(require("../../../util/src"));
const configPath = Path.join(process.env.HOME ? process.env.HOME : '', '.antcube/rubik.json');
const config = FS.existsSync(configPath) ? JSON.parse(FS.readFileSync(configPath).toString()) : {};
const dbPath = `${config['CaseStore']}`;
function updateCases(cases) {
    return __awaiter(this, void 0, void 0, function* () {
        const db = new DB.DB();
        Utils.Console.info(`打开数据库 [${dbPath}]`);
        yield db.open(dbPath);
        let index = 0;
        for (const acase of cases) {
            Utils.Console.info(`更新用例信息 : [${++index}]\n${JSON.stringify(acase)}`);
            yield db.update(`INSERT OR REPLACE INTO rubik_case (ID, poles, scheme, type, subtype, version) VALUES (?,?,?,?,?,?)`, [
                acase.ID,
                `${acase.poles}`,
                acase.scheme,
                acase.type,
                acase.subtype,
                acase.version
            ]);
        }
        yield db.close();
        Utils.Console.info(`关闭数据库 [${dbPath}]`);
        return Promise.resolve();
    });
}
exports.updateCases = updateCases;
function selectCases(IDs) {
    return __awaiter(this, void 0, void 0, function* () {
        const db = new DB.DB();
        Utils.Console.info(`打开数据库 [${dbPath}]`);
        yield db.open(dbPath);
        let cases = [];
        if (IDs && IDs.length > 0) {
            const mask = new Array();
            for (let i = 0; i < IDs.length; i++) {
                mask.push('?');
            }
            cases = yield db.select(`SELECT * FROM rubik_case WHERE ID IN (${mask.join(',')})`, IDs);
        }
        else {
            cases = yield db.select(`SELECT * FROM rubik_case`);
        }
        yield db.close();
        return Promise.resolve(cases);
    });
}
exports.selectCases = selectCases;
//# sourceMappingURL=index.js.map