import { noChange } from 'lit';
import { directive } from 'lit/directive.js';
import { AsyncDirective } from 'lit/async-directive.js';
class LiveDataDirective extends AsyncDirective {
    liveData;
    path;
    unsubscribe;
    lastValue;
    render(liveData, path, defaultVaue) {
        if (this.liveData !== liveData) {
            this.liveData = liveData;
            this.path = path;
            this.unsubscribe?.();
            this.unsubscribe = undefined;
            if (this.isConnected) {
                this.subscribe();
            }
        }
        let value = liveData.get(path);
        if (value === undefined) {
            value = defaultVaue;
        }
        if (value !== this.lastValue) {
            this.lastValue = value;
            return value;
        }
        return noChange;
    }
    subscribe() {
        const { liveData, path } = this;
        if (liveData && path) {
            this.unsubscribe = liveData.observeProperties([path], (data) => {
                this.setValue(data.value);
            });
        }
    }
    disconnected() {
        this.unsubscribe?.();
        this.unsubscribe = undefined;
    }
    reconnected() {
        this.subscribe();
    }
}
/**
 * A Lit directive that connects a `LiveData` property to a LitElement property.
 * When the `LiveData` property changes, the LitElement re-renders.
 *
 * @param liveData - The `LiveData` instance.
 * @param path - The dot-notation path to the property to observe (e.g., "user.name").
 * @returns The current value of the `LiveData` property.
 *
 * @example
 * ```typescript
 * import { LitElement, html } from 'lit';
 * import { customElement } from 'lit/decorators.js';
 * import { live, LiveData } from './LiveData.js';
 *
 * @customElement('my-element')
 * export class MyElement extends LitElement {
 *   // Create a LiveData instance
 *   userData = new LiveData({ name: 'Initial Name' });
 *
 *   override render() {
 *     return html`
 *       <div>User Name: ${live(this.userData, 'name')}</div>
 *     `;
 *   }
 * }
 * ```
 */
export const live = directive(LiveDataDirective);
//# sourceMappingURL=live.js.map