import { ContextConsumer } from "@lit/context";
import { bindingsContext } from "../components/common/context/bindings-context.js";
function bindings() {
    return (target)=>{
        const connectedCallback = target.prototype.connectedCallback;
        target.prototype.connectedCallback = function() {
            new ContextConsumer(this, {
                context: bindingsContext,
                callback: (value)=>{
                    if (this.initialized || 0 === Object.keys(value).length) return;
                    this.initialized = true;
                    try {
                        this.bindings = value;
                        const updateLanguage = ()=>this.requestUpdate();
                        this.bindings.i18n.on('languageChanged', updateLanguage);
                        const unsubscribeLanguage = ()=>this.bindings?.i18n.off('languageChanged', updateLanguage);
                        this.initialize?.();
                        this.unsubscribeLanguage = unsubscribeLanguage;
                    } catch (error) {
                        this.error = error;
                    }
                },
                subscribe: true
            });
            if (connectedCallback) connectedCallback.call(this);
        };
        const disconnectedCallback = target.prototype.disconnectedCallback;
        target.prototype.disconnectedCallback = function() {
            this.unsubscribeLanguage?.();
            if (disconnectedCallback) disconnectedCallback.call(this);
        };
    };
}
export { bindings };
