import { CSSResultGroup, CSSResult } from "lit"; export type ModalStyleSheetInput = | CSSResultGroup | CSSStyleSheet | Array; const styleSheetCache = new WeakMap(); function cssResultToStyleSheet(styleResult: CSSResult): CSSStyleSheet { const cachedStyleSheet = styleSheetCache.get(styleResult); if (cachedStyleSheet) return cachedStyleSheet; const styleSheet = new CSSStyleSheet(); styleSheet.replaceSync(styleResult.cssText); styleSheetCache.set(styleResult, styleSheet); return styleSheet; } function isCSSResult(value: unknown): value is CSSResult { return value instanceof CSSResult; } export function normalizeStyleSheets( styleSheet?: ModalStyleSheetInput ): Array { if (!styleSheet) return []; const styleSheets = Array.isArray(styleSheet) ? styleSheet : [styleSheet]; return styleSheets.flatMap((sheet) => { if (Array.isArray(sheet)) { return normalizeStyleSheets(sheet); } if (sheet instanceof CSSStyleSheet) { return [sheet]; } if (isCSSResult(sheet)) { return [cssResultToStyleSheet(sheet)]; } return []; }); }