import type * as d from '../declarations' ; import { BUILD } from '@app-data' ; import { consoleDevWarn , getHostRef , plt } from '@platform' ; import { getValue , setValue } from './set-value' ; import { HOST_FLAGS , MEMBER_FLAGS } from '../utils/constants' ; import { PROXY_FLAGS } from './runtime-constants' ; export const proxyComponent = ( Cstr , cmpMeta , flags ) => { if ( BUILD . member && cmpMeta . $members$ ) { if ( BUILD . watchCallback && Cstr . watchers ) { cmpMeta . $watchers$ = Cstr . watchers ; } const members = Object . entries ( cmpMeta . $members$ ) ; const prototype = ( Cstr as any ) . prototype ; members . map ( ( [ memberName , [ memberFlags ] ] ) => { if ( ( BUILD . prop || BUILD . state ) && ( memberFlags & MEMBER_FLAGS . Prop || ( ( ! BUILD . lazyLoad || flags & PROXY_FLAGS . proxyState ) && memberFlags & MEMBER_FLAGS . State ) ) ) { Object . defineProperty ( prototype , memberName , { get ( this ) { return getValue ( this , memberName ) ; } , set ( this , newValue ) { if ( BUILD . isDev ) { const ref = getHostRef ( this ) ; if ( ( flags & PROXY_FLAGS . isElementConstructor ) === 0 && ( ref . $flags$ & HOST_FLAGS . isConstructingInstance ) === 0 && ( memberFlags & MEMBER_FLAGS . Prop ) !== 0 && ( memberFlags & MEMBER_FLAGS . Mutable ) === 0 ) { consoleDevWarn ( `@Prop() "${ memberName }" on <${ cmpMeta . $tagName$ }> is immutable but was modified from within the component.\nMore information: https://stenciljs.com/docs/properties#prop-mutability` ) ; } } setValue ( this , memberName , newValue , cmpMeta ) ; } , configurable : true , enumerable : true , } ) ; } else if ( BUILD . lazyLoad && BUILD . method && flags & PROXY_FLAGS . isElementConstructor && memberFlags & MEMBER_FLAGS . Method ) { Object . defineProperty ( prototype , memberName , { value ( this , ... args ) { const ref = getHostRef ( this ) ; return ref . $onInstancePromise$ . then ( ( ) => ref . $lazyInstance$ [ memberName ] ( ... args ) ) ; } , } ) ; } } ) ; if ( BUILD . observeAttribute && ( ! BUILD . lazyLoad || flags & PROXY_FLAGS . isElementConstructor ) ) { const attrNameToPropName = new Map ( ) ; prototype . attributeChangedCallback = function ( attrName , _oldValue , newValue ) { plt . jmp ( ( ) => { const propName = attrNameToPropName . get ( attrName ) ; if ( this . hasOwnProperty ( propName ) ) { newValue = this [ propName ] ; delete this [ propName ] ; } else if ( prototype . hasOwnProperty ( propName ) && typeof this [ propName ] === 'number' && this [ propName ] == newValue ) { return ; } this [ propName ] = newValue === null && typeof this [ propName ] === 'boolean' ? false : newValue ; } ) ; } ; Cstr . observedAttributes = members . filter ( ( [ _ , m ] ) => m [ 0 ] & MEMBER_FLAGS . HasAttribute ) . map ( ( [ propName , m ] ) => { const attrName = m [ 1 ] || propName ; attrNameToPropName . set ( attrName , propName ) ; if ( BUILD . reflect && m [ 0 ] & MEMBER_FLAGS . ReflectAttr ) { cmpMeta . $attrsToReflect$ . push ( [ propName , attrName ] ) ; } return attrName ; } ) ; } } return Cstr ; } ;