import { fetchBindings } from "../utils/initialization-common-utils.js";
function initializeBindings(instance) {
    return new Promise((resolve, reject)=>{
        instance.initialized = true;
        fetchBindings(instance).then((bindings)=>{
            instance.bindings = bindings;
            const updateLanguage = ()=>instance.requestUpdate();
            instance.bindings?.i18n?.on('languageChanged', updateLanguage);
            const unsubscribeLanguage = ()=>instance.bindings?.i18n?.off('languageChanged', updateLanguage);
            resolve(unsubscribeLanguage);
            instance.initialize?.();
        }).catch((error)=>{
            instance.error = error;
            reject(error);
        });
    });
}
class BindingController {
    hostConnected() {
        initializeBindings(this.host).then((unsubscribeLanguage)=>{
            this.unsubscribeLanguage = unsubscribeLanguage;
        }).catch((error)=>{
            this.host = error;
        });
    }
    hostDisconnected() {
        this.unsubscribeLanguage();
    }
    constructor(host){
        this.unsubscribeLanguage = ()=>{};
        this.host = host;
        this.host.addController(this);
    }
}
const InitializeBindingsMixin = (superClass)=>{
    class BindingControllerMixinClass extends superClass {
        initBindings() {
            new BindingController(this);
        }
        constructor(...args){
            super(...args);
            this.initBindings();
        }
    }
    return BindingControllerMixinClass;
};
export { InitializeBindingsMixin };
