import { Condition } from "./environment" ; const MATCH_ALL = "all" ; const MATCH_NONE = "not all" ; type BaseStylesheet = Readonly < { css : string ; condition : Condition ; } > ; type StylesheetWithoutId = BaseStylesheet & Readonly < { id ? : undefined ; } > ; type StylesheetWithId = BaseStylesheet & Readonly < { id : string ; } > ; type Stylesheet = StylesheetWithId | StylesheetWithoutId ; export function stylesheet ( spec ) ; export function stylesheet ( spec ) ; export function stylesheet ( spec ) { return spec ; } export type Stylesheets = Readonly < { [ _ ] : Stylesheet ; } > ; export function insert ( stylesheets ) { const fragment = document . createDocumentFragment ( ) ; Object . entries ( stylesheets ) . forEach ( ( [ _ , sheet ] ) => { const style = document . createElement ( "style" ) ; if ( sheet . id !== undefined ) style . id = sheet . id ; style . textContent = sheet . css ; style . media = sheet . condition ( window ) ? MATCH_ALL : MATCH_NONE ; fragment . appendChild ( style ) ; } ) ; document . documentElement . appendChild ( fragment ) ; } const setMediaQuery = ( m ) => ( s ) => { const element = document . getElementById ( s . id ) ; if ( element !== null ) { element . setAttribute ( "media" , m ) ; } } ; export const enable = setMediaQuery ( MATCH_ALL ) ; export const disable = setMediaQuery ( MATCH_NONE ) ;