"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MotionMasterClientIndexedDB = void 0;
const DB_NAME = 'mmc';
const ESI_STORE_NAME = 'esi';
class MotionMasterClientIndexedDB {
    constructor() {
        this.db = null;
    }
    open() {
        return new Promise((resolve, reject) => {
            if (this.db) {
                return resolve(this.db);
            }
            const request = indexedDB.open(DB_NAME, 1);
            request.onsuccess = (event) => {
                // console.log('onsuccess');
                this.db = event.target.result;
                resolve(this.db);
            };
            request.onupgradeneeded = (event) => {
                // console.log('onupgradeneeded');
                const db = event.target.result;
                // Create an object store if it doesn't exist
                if (!db.objectStoreNames.contains(ESI_STORE_NAME)) {
                    db.createObjectStore(ESI_STORE_NAME, { keyPath: 'id' });
                }
            };
            request.onerror = (event) => {
                // console.error('Error opening database:', (event.target as IDBOpenDBRequest).error);
                reject(event.target.error);
            };
        });
    }
    putEsi(key, data) {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                reject('Database not initialized');
                return;
            }
            const transaction = this.db.transaction(ESI_STORE_NAME, 'readwrite');
            const store = transaction.objectStore(ESI_STORE_NAME);
            const record = { id: key, data: data.buffer };
            const request = store.put(record);
            request.onsuccess = () => {
                // console.log(`Data stored successfully for key: ${key}`);
                resolve();
            };
            request.onerror = (event) => {
                const error = event.target.error;
                // console.error('Error storing data:', error);
                reject(error);
            };
        });
    }
    getEsi(key) {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                reject('Database not initialized');
                return;
            }
            const transaction = this.db.transaction(ESI_STORE_NAME, 'readonly');
            const store = transaction.objectStore(ESI_STORE_NAME);
            const request = store.get(key);
            request.onsuccess = () => {
                const record = request.result;
                if (record) {
                    resolve(new Uint8Array(record.data));
                }
                else {
                    resolve(null);
                }
            };
            request.onerror = (event) => {
                const error = event.target.error;
                // console.error('Error loading data:', error);
                reject(error);
            };
        });
    }
}
exports.MotionMasterClientIndexedDB = MotionMasterClientIndexedDB;
//# sourceMappingURL=mmcidb.js.map