import type * as d from '../../../declarations' ; import { createStaticGetter } from '../transform-utils' ; import { DEFAULT_STYLE_MODE , dashToPascalCase } from '@utils' ; import { getScopeId } from '../../style/scope-css' ; import { scopeCss } from '../../../utils/shadow-css' ; import ts from 'typescript' ; export const addNativeStaticStyle = ( classMembers , cmp ) => { if ( Array . isArray ( cmp . styles ) && cmp . styles . length > 0 ) { if ( cmp . styles . length > 1 || ( cmp . styles . length === 1 && cmp . styles [ 0 ] . modeName !== DEFAULT_STYLE_MODE ) ) { addMultipleModeStyleGetter ( classMembers , cmp , cmp . styles ) ; } else { addSingleStyleGetter ( classMembers , cmp , cmp . styles [ 0 ] ) ; } } } ; const addMultipleModeStyleGetter = ( classMembers , cmp , styles ) => { const styleModes = [ ] ; styles . forEach ( ( style ) => { if ( typeof style . styleStr === 'string' ) { const styleLiteral = createStyleLiteral ( cmp , style ) ; const propStr = ts . createPropertyAssignment ( style . modeName , styleLiteral ) ; styleModes . push ( propStr ) ; } else if ( typeof style . styleIdentifier === 'string' ) { const styleIdentifier = ts . createIdentifier ( style . styleIdentifier ) ; const propIdentifier = ts . createPropertyAssignment ( style . modeName , styleIdentifier ) ; styleModes . push ( propIdentifier ) ; } else if ( Array . isArray ( style . externalStyles ) && style . externalStyles . length > 0 ) { const styleUrlIdentifier = createStyleIdentifierFromUrl ( cmp , style ) ; const propUrlIdentifier = ts . createPropertyAssignment ( style . modeName , styleUrlIdentifier ) ; styleModes . push ( propUrlIdentifier ) ; } } ) ; const styleObj = ts . createObjectLiteral ( styleModes , true ) ; classMembers . push ( createStaticGetter ( 'style' , styleObj ) ) ; } ; const addSingleStyleGetter = ( classMembers , cmp , style ) => { if ( typeof style . styleStr === 'string' ) { const styleLiteral = createStyleLiteral ( cmp , style ) ; classMembers . push ( createStaticGetter ( 'style' , styleLiteral ) ) ; } else if ( typeof style . styleIdentifier === 'string' ) { const styleIdentifier = ts . createIdentifier ( style . styleIdentifier ) ; classMembers . push ( createStaticGetter ( 'style' , styleIdentifier ) ) ; } else if ( Array . isArray ( style . externalStyles ) && style . externalStyles . length > 0 ) { const styleUrlIdentifier = createStyleIdentifierFromUrl ( cmp , style ) ; classMembers . push ( createStaticGetter ( 'style' , styleUrlIdentifier ) ) ; } } ; const createStyleLiteral = ( cmp , style ) => { if ( cmp . encapsulation === 'scoped' ) { const scopeId = getScopeId ( cmp . tagName , style . modeName ) ; return ts . createStringLiteral ( scopeCss ( style . styleStr , scopeId , false ) ) ; } return ts . createStringLiteral ( style . styleStr ) ; } ; const createStyleIdentifierFromUrl = ( cmp , style ) => { style . styleIdentifier = dashToPascalCase ( cmp . tagName ) ; style . styleIdentifier = style . styleIdentifier . charAt ( 0 ) . toLowerCase ( ) + style . styleIdentifier . substring ( 1 ) ; if ( style . modeName !== DEFAULT_STYLE_MODE ) { style . styleIdentifier += dashToPascalCase ( style . modeName ) ; } style . styleIdentifier += 'Style' ; style . externalStyles = [ style . externalStyles [ 0 ] ] ; return ts . createIdentifier ( style . styleIdentifier ) ; } ;