import TWMixin_default from "./TWMixin.js";
let twSheet = null;
if (typeof window !== "undefined" && typeof CSSStyleSheet !== "undefined") try {
	twSheet = new CSSStyleSheet();
	if (typeof twSheet.replaceSync === "function") twSheet.replaceSync(TWMixin_default);
} catch (_error) {
	twSheet = null;
}
function TWMixin(Base) {
	class TWElement extends Base {
		createRenderRoot() {
			const renderRoot = super.createRenderRoot();
			if (!(renderRoot instanceof ShadowRoot)) throw new Error("TWMixin can only be applied to elements with shadow roots");
			if (!twSheet) throw new Error("twSheet not found. Probable cause: CSSStyleSheet not supported in this environment");
			const constructorStylesheets = [];
			const constructorStyles = "styles" in this.constructor && this.constructor.styles || [];
			if (Array.isArray(constructorStyles)) {
				for (const item of constructorStyles) if (item.styleSheet) constructorStylesheets.push(item.styleSheet);
			} else if (constructorStyles.styleSheet) constructorStylesheets.push(constructorStyles.styleSheet);
			if (renderRoot?.adoptedStyleSheets) renderRoot.adoptedStyleSheets = [
				twSheet,
				...renderRoot.adoptedStyleSheets,
				...constructorStylesheets
			];
			else renderRoot.adoptedStyleSheets = [twSheet, ...constructorStylesheets];
			return renderRoot;
		}
	}
	return TWElement;
}
export { TWMixin };
