import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject } from '@angular/core';
import { AgentType } from '@metad/ocap-core';
import { ReplaySubject, BehaviorSubject, filter, switchMap, takeUntil } from 'rxjs';

const NGM_WASM_AGENT_WORKER = new InjectionToken('NgmWASMAgentWorkerUrl');
class WasmAgentService {
    get agent() {
        return this.agent$.value;
    }
    set agent(value) {
        this.agent$.next(value);
    }
    constructor(workerUrl) {
        this.workerUrl = workerUrl;
        this.type = AgentType.Wasm;
        // Should be used only in ngOnDestroy.
        this.destroySubject$ = new ReplaySubject(1);
        // Exposed to any extending Store to be used for the teardown.
        this.destroy$ = this.destroySubject$.asObservable();
        this.agent$ = new BehaviorSubject(null);
    }
    selectStatus() {
        return this.agent$.pipe(filter((agent) => !!agent), switchMap((agent) => agent.selectStatus()), takeUntil(this.destroy$));
    }
    selectError() {
        return this.agent$.pipe(filter((agent) => !!agent), switchMap((agent) => agent.selectError()), takeUntil(this.destroy$));
    }
    error(err) {
        this.getWASMAgent().then((agent) => agent.error(err));
    }
    async request(dataSource, options) {
        await this.getWASMAgent();
        return this.agent.request(dataSource, options);
    }
    async registerModel(model) {
        await this.getWASMAgent();
        await this.agent.registerModel(model);
    }
    async getWASMAgent() {
        if (!this.agent) {
            const { DuckdbWasmAgent } = await import('@metad/ocap-duckdb');
            this.agent = new DuckdbWasmAgent([], this.workerUrl);
        }
        return this.agent;
    }
    ngOnDestroy() {
        this.destroySubject$.next();
        this.destroySubject$.complete();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: WasmAgentService, deps: [{ token: NGM_WASM_AGENT_WORKER }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: WasmAgentService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: WasmAgentService, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Inject,
                    args: [NGM_WASM_AGENT_WORKER]
                }] }] });

/**
 * Generated bundle index. Do not edit.
 */

export { NGM_WASM_AGENT_WORKER, WasmAgentService };
//# sourceMappingURL=metad-ocap-angular-wasm-agent.mjs.map
