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' ; /**
 * Adds static "style" getter within the class
 * const MyComponent = class {
 *   static get style() { return "styles"; }
 * }
 */ export const addStaticStyleGetterWithinClass = ( classMembers , cmp , commentOriginalSelector ) => { const styleLiteral = getStyleLiteral ( cmp , commentOriginalSelector ) ; if ( styleLiteral ) { classMembers . push ( createStaticGetter ( 'style' , styleLiteral ) ) ; } } ; /**
 * Adds static "style" property to the class variable.
 * const MyComponent = class {}
 * MyComponent.style = "styles";
 */ export const addStaticStylePropertyToClass = ( styleStatements , cmp , commentOriginalSelector ) => { const styleLiteral = getStyleLiteral ( cmp , commentOriginalSelector ) ; if ( styleLiteral ) { const statement = ts . createStatement ( ts . createAssignment ( ts . createPropertyAccess ( ts . createIdentifier ( cmp . componentClassName ) , 'style' ) , styleLiteral ) ) ; styleStatements . push ( statement ) ; } } ; const getStyleLiteral = ( cmp , commentOriginalSelector ) => { 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 ) ) { return getMultipleModeStyle ( cmp , cmp . styles , commentOriginalSelector ) ; } else { return getSingleStyle ( cmp , cmp . styles [ 0 ] , commentOriginalSelector ) ; } } return null ; } ; const getMultipleModeStyle = ( cmp , styles , commentOriginalSelector ) => { const styleModes = [ ] ; styles . forEach ( ( style ) => { if ( typeof style . styleStr === 'string' ) { const styleLiteral = createStyleLiteral ( cmp , style , commentOriginalSelector ) ; const propStr = createPropertyAssignment ( style . modeName , styleLiteral ) ; styleModes . push ( propStr ) ; } else if ( typeof style . styleIdentifier === 'string' ) { const styleIdentifier = ts . createIdentifier ( style . styleIdentifier ) ; const propIdentifier = createPropertyAssignment ( style . modeName , styleIdentifier ) ; styleModes . push ( propIdentifier ) ; } else if ( Array . isArray ( style . externalStyles ) && style . externalStyles . length > 0 ) { const styleUrlIdentifier = createStyleIdentifierFromUrl ( cmp , style ) ; const propUrlIdentifier = createPropertyAssignment ( style . modeName , styleUrlIdentifier ) ; styleModes . push ( propUrlIdentifier ) ; } } ) ; return ts . createObjectLiteral ( styleModes , true ) ; } ; const createPropertyAssignment = ( mode , initializer ) => { const node = ts . createPropertyAssignment ( mode , initializer ) ; return node ; } ; const getSingleStyle = ( cmp , style , commentOriginalSelector ) => { if ( typeof style . styleStr === 'string' ) { return createStyleLiteral ( cmp , style , commentOriginalSelector ) ; } if ( typeof style . styleIdentifier === 'string' ) { return ts . createIdentifier ( style . styleIdentifier ) ; } if ( Array . isArray ( style . externalStyles ) && style . externalStyles . length > 0 ) { return createStyleIdentifierFromUrl ( cmp , style ) ; } return null ; } ; const createStyleLiteral = ( cmp , style , commentOriginalSelector ) => { if ( cmp . encapsulation === 'scoped' || ( commentOriginalSelector && cmp . encapsulation === 'shadow' ) ) { const scopeId = getScopeId ( cmp . tagName , style . modeName ) ; return ts . createStringLiteral ( scopeCss ( style . styleStr , scopeId , commentOriginalSelector ) ) ; } 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 ) ; } ;