/**
 * Supports both shared and dedicated workers, based on how the worker is constructed (new SharedWorker vs new Worker()).
 */
import '@journeyapps/wa-sqlite';
import { createBaseLogger, createLogger } from '@powersync/common';
import * as Comlink from 'comlink';
import { isSharedWorker, MultiDatabaseServer } from './MultiDatabaseServer.js';
const baseLogger = createBaseLogger();
baseLogger.useDefaults();
const logger = createLogger('db-worker');
const server = new MultiDatabaseServer(logger);
const exposedFunctions = {
    connect: (config) => server.handleConnection(config),
    connectToExisting: ({ identifier, lockName }) => server.connectToExisting(identifier, lockName)
};
// Check if we're in a SharedWorker context
if (isSharedWorker) {
    const _self = self;
    _self.onconnect = function (event) {
        const port = event.ports[0];
        Comlink.expose(exposedFunctions, port);
    };
}
else {
    // A dedicated worker can be shared externally
    Comlink.expose(exposedFunctions);
}
addEventListener('unload', () => {
    server.closeAll();
});
