import { randomID } from "../../../utils/utils.js";
class LayoutStylesController {
    hostConnected() {
        if (!this.host.id) this.host.id = randomID(this.componentPrefix);
        this.host.updateComplete.then(()=>this.updateStyles());
    }
    updateStyles() {
        const parent = this.host.getRootNode();
        const isDocumentOrShadowRoot = parent instanceof Document || parent instanceof ShadowRoot;
        if (!isDocumentOrShadowRoot) return;
        const newStylesCSS = this.stylesBuilder(this.host, this.host.mobileBreakpoint);
        if (!this.dynamicStyleSheet) {
            this.dynamicStyleSheet = new CSSStyleSheet();
            this.dynamicStyleSheet.replaceSync(newStylesCSS);
            parent.adoptedStyleSheets = [
                ...parent.adoptedStyleSheets,
                this.dynamicStyleSheet
            ];
            return;
        }
        this.dynamicStyleSheet.replaceSync(newStylesCSS);
    }
    constructor(host, stylesBuilder, componentPrefix){
        this.dynamicStyleSheet = null;
        this.host = host;
        this.stylesBuilder = stylesBuilder;
        this.componentPrefix = componentPrefix;
        this.host.addController(this);
    }
}
export { LayoutStylesController };
