"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.connect = exports.DATABASE_VERSION = exports.DATABASE_NAME = void 0;
const NOOP_1 = require("../NOOP");
exports.DATABASE_NAME = 'unit';
exports.DATABASE_VERSION = 1;
function connect(system, name, version, onupgradeneeded = NOOP_1.NOOP) {
    return __awaiter(this, void 0, void 0, function* () {
        const { api: { db }, } = system;
        return new Promise((resolve, reject) => {
            const request = db.open(name, version);
            request.onerror = (event) => {
                reject(
                // @ts-ignore
                new Error('Could not connect to IndexedDB: ' + event.target.errorCode));
            };
            request.onsuccess = (event) => {
                if (event && event.target) {
                    const db = event.target.result;
                    resolve(db);
                }
            };
            request.onupgradeneeded = (event) => {
                const db = event.target.result;
                onupgradeneeded(db);
            };
        });
    });
}
exports.connect = connect;
//# sourceMappingURL=database.js.map