
import { observe, unobserve } from 'james-bond'
import Class from 'lowclass'
import Mixin from 'lowclass/Mixin'
import {getInheritedDescriptor} from 'lowclass/utils'

export default
Mixin(Base => Class( 'ForwardProps' ).extends( Base, ({ Super, Public, Protected, Private }) => ({

    connectedCallback() {
        Super( this ).connectedCallback && Super( this ).connectedCallback()
        Private( this ).__receivePropsFromObject()
    },

    disconnectedCallback() {
        Super( this ).disconnectedCallback && Super( this ).disconnectedCallback()
        Private( this ).__unreceivePropsFromObject()
    },

    private: {
        propChangedCallback: ( propName, value ) => undefined,

        __receivePropsFromObject() {
            const publicThis = Public( this )
            this.propChangedCallback = ( propName, value ) => publicThis[ propName ] = value
            observe( Protected( this )._observedObject, this.__getProps(), this.propChangedCallback, {
                // inherited: true, // XXX the 'inherited' option doesn't work in this case. Why?
            } )
        },

        __unreceivePropsFromObject() {
            unobserve(Protected( this )._observedObject, this.__getProps(), this.propChangedCallback )
        },

        __getProps() {
            let result
            const props = Public( this ).constructor.props

            if ( Array.isArray( props ) ) result = props
            else {
                result = []
                if ( typeof props === 'object' )
                    for ( const prop in props )
                        result.push( prop )
            }

            return result
        },
    },

    protected: {
        get _observedObject() {
            throw new TypeError(`
                The subclass using ForwardProps must define a protected
                _observedObject property defining the object from which props
                are forwarded.
            `)
        },
    }

})))

@@ -405,38 +405,28 @@
 his ).__
-recei
+obser
 veProps
-FromObject
 ()%0A    %7D
@@ -568,38 +568,28 @@
 s ).__un
-recei
+obser
 veProps
-FromObject
 ()%0A    %7D
@@ -614,16 +614,18 @@
         
+__
 propChan
@@ -682,38 +682,28 @@
       __
-recei
+obser
 veProps
-FromObject
 () %7B%0A   
@@ -758,24 +758,26 @@
        this.
+__
 propChangedC
@@ -912,32 +912,34 @@
 etProps(), this.
+__
 propChangedCallb
@@ -1087,30 +1087,20 @@
 __un
-recei
+obser
 veProps
-FromObject
 () %7B
@@ -1181,16 +1181,18 @@
 ), this.
+__
 propChan

rename private prop/methods in ForwardProps class to make more sense and follow the __underscore convention for private names