import { isUndefined } from '@kirei/shared' ; import { warn } from '../warn' ; /**
 * StyleSheets to use with adoptStyleSheets
 */ export type StyleSheet = CSSResult | CSSStyleSheet ; private function mapStyleSheetsToCssText ( sheet ) { if ( sheet instanceof CSSStyleSheet ) { let cssText = '' ; for ( let i = 0 ; i < sheet . cssRules . length ; i ++ ) { cssText += `${ sheet . cssRules [ i ] . cssText }\n` ; } return cssText ; } else if ( sheet instanceof CSSResult ) { return sheet . toString ( ) ; } else if ( __DEV__ ) { warn ( `Invlaid stylesheet, expected a css literal or a native CSSStyleSheet, got ${ typeof sheet }` ) ; } } private async function mapStyleSheetToCssStyleSheet ( sheet ) { if ( sheet instanceof CSSStyleSheet ) { return sheet ; } else if ( sheet instanceof CSSResult ) { return sheet . generate ( ) ; } else if ( __DEV__ ) { warn ( `Invlaid stylesheet, expected a css literal or a native CSSStyleSheet, got ${ typeof sheet }` ) ; } } param shadowRoot param tag param styles returns private export async function adoptStyleSheets ( shadowRoot , tag , styles ) { if ( styles != null && styles . length ) { const { ShadyCSS } = window ; if ( ShadyCSS ?. nativeShadow === false ) { const cssTexts = styles . map ( mapStyleSheetsToCssText ) . filter ( sheet => sheet ) ; ShadyCSS . ScopingShim . prepareAdoptedCssText ( cssTexts , tag ) ; } else if ( CSSResult . supportsAdoptingStyleSheets ) { const styleSheets = await Promise . all ( styles . map ( mapStyleSheetToCssStyleSheet ) ) ; shadowRoot . adoptedStyleSheets = styleSheets . filter ( sheet => sheet ) ; } else { return false ; } } return true ; } /**
 * Class to easily construct and cache style sheets, through template literals
 */ export class CSSResult { private static supportsAdoptingStyleSheets = 'adoptedStyleSheets' in Document . prototype && 'replace' in CSSStyleSheet . prototype ; /**
     * Cached stylesheet, created by the generate() method
     */ private styleSheet : CSSStyleSheet ; /**
     * Raw CSS styles, as passed in to the template literal
     */ readonly cssText : string ; param strings param values constructor ( strings , values ) { this . cssText = values . reduce < string > ( ( acc , value , idx ) => { return acc + String ( value ) + strings [ idx + 1 ] ; } , strings [ 0 ] ) ; } returns async generate ( ) { if ( isUndefined ( this . styleSheet ) ) { if ( CSSResult . supportsAdoptingStyleSheets ) { this . styleSheet = new CSSStyleSheet ( ) ; await this . styleSheet . replace ( this . cssText ) ; } else { this . styleSheet = null ; } } return this . styleSheet ; } returns toString ( ) { return this . cssText ; } } param strings param values returns export function css ( strings , ... values ) { return new CSSResult ( strings , values ) ; }