// #region Imports
import { AsyncDirective, PartType, directive } from 'lit/async-directive.js';
import { TranslatorServiceLocator } from './TranslatorService';
// #endregion
export class TranslateDirective extends AsyncDirective {
    _translator;
    _subscription;
    constructor(partInfo) {
        super(partInfo);
        if (partInfo.type !== PartType.PROPERTY) {
            throw new Error('The translate directive can only be used in property bindings.');
        }
        this._subscription = null;
        this._translator = TranslatorServiceLocator.current;
    }
    update(_part, [key, ...args]) {
        this._subscription ??= this._translator.languageChanged.subscribe(() => {
            this.render(key, ...args);
        });
        return this.render(key, ...args);
    }
    render(key, ...args) {
        const value = this._translator.translate(key, ...args);
        this.setValue(value);
        return value;
    }
    disconnected() {
        super.disconnected();
        if (this._subscription) {
            this._subscription.dispose();
            this._subscription = null;
        }
    }
}
/**
 * @public
 */
export const translate = directive(TranslateDirective);
//# sourceMappingURL=TranslateDirective.js.map