import sqliteService from '../service/sqlite.js';
import sharedRepository, {SharedRepository} from './shared.js';

var dirtyKeySet = new Set();

function transformKey(key) {
    return `config:${String(key).toLowerCase()}`;
}

async function syncLoop() {
    if (dirtyKeySet.size > 0) {
        try {
            await sqliteService.executeNonQuery('BEGIN');
            try {
                for (var key of dirtyKeySet) {
                    var value = sharedRepository.getString(key);
                    if (value === null) {
                        await sqliteService.executeNonQuery(
                            'DELETE FROM configs WHERE `key` = @key',
                            {
                                '@key': key
                            }
                        );
                    } else {
                        await sqliteService.executeNonQuery(
                            'INSERT OR REPLACE INTO configs (`key`, `value`) VALUES (@key, @value)',
                            {
                                '@key': key,
                                '@value': value
                            }
                        );
                    }
                }
                dirtyKeySet.clear();
            } finally {
                await sqliteService.executeNonQuery('COMMIT');
            }
        } catch (err) {
            console.error(err);
        }
    }
    setTimeout(syncLoop, 100);
}

class ConfigRepository extends SharedRepository {
    async init() {
        await sqliteService.executeNonQuery(
            'CREATE TABLE IF NOT EXISTS configs (`key` TEXT PRIMARY KEY, `value` TEXT)'
        );
        await sqliteService.execute(
            ([key, value]) => sharedRepository.setString(key, value),
            'SELECT `key`, `value` FROM configs'
        );
        syncLoop();
    }

    remove(key) {
        var _key = transformKey(key);
        sharedRepository.remove(_key);
        dirtyKeySet.add(_key);
    }

    getString(key, defaultValue = null) {
        var _key = transformKey(key);
        return sharedRepository.getString(_key, defaultValue);
    }

    setString(key, value) {
        var _key = transformKey(key);
        var _value = String(value);
        sharedRepository.setString(_key, _value);
        dirtyKeySet.add(_key);
    }
}

var self = new ConfigRepository();
window.configRepository = self;

export {self as default, ConfigRepository};
