// ***************************************
// ***************************************
// ***************************************
// THIS NEEDS TO BE TESTED
// ***************************************
// ***************************************
// ***************************************
import { AsyncDirective } from 'lit/async-directive.js';
import { directive } from 'lit/directive.js';
import { BehaviorSubject, map } from 'rxjs';
export class ObservableDirective extends AsyncDirective {
    _subscription;
    constructor(partInfo) {
        super(partInfo);
    }
    render(_observable, _mapper) {
        return this._subscription;
    }
    update(_part, [observable, mapper]) {
        if (this._subscription) {
            this._subscription.unsubscribe();
        }
        if (observable instanceof BehaviorSubject) {
            // Set the initial value of the BehaviorSubject
            this.setValue(observable.getValue());
            // this.commit();
        }
        if (observable.source instanceof BehaviorSubject) {
            // Set the initial value of the BehaviorSubject
            this.setValue(observable.source.getValue());
            // this.commit();
        }
        this._subscription = observable.pipe(map((x) => mapper ? mapper(x) : x)).subscribe((value) => {
            this.setValue(value);
            console.log('MOSAIK: ObservableDirective.update()');
            //  this.render(observable, mapper);
            // this.commit();
        });
        return this.render(observable);
    }
    // Unsubscribe when the directive is disconnected
    disconnected() {
        if (this._subscription) {
            this._subscription.unsubscribe();
        }
    }
}
export const observe = directive(ObservableDirective);
//# sourceMappingURL=ObserveDirective.js.map