import Updater from "./Updater";
import { CurrentOwner } from "./createElement";

export function instantiateComponent(type, vnode, vtype, props, context) {
    var instance;
    if (vtype === 2) {
        instance = new type(props, context);
        //props, context是不可变的
        instance.props = props;
        instance.context = context;
        new Updater(instance,vnode);
    } else {
        instance = {
            refs: {},
            render: function() {
                return type(this.props, this.context);
            },
            __isStateless: 1,
            state: null,
            props: props,
            context: context,
            //  __pendingCallbacks: [],
            //  __current: noop,
            //  __mergeStates: alwaysNull
        };
        var updater = new Updater(instance,vnode);
        let lastOwn = CurrentOwner.cur;
        CurrentOwner.cur = instance;
        try {
            var mixin = type(props, context);
        } finally {
            CurrentOwner.cur = lastOwn;
        }
        if (mixin && mixin.render) {
            //支持module pattern component
            delete instance.__isStateless;
            Object.assign(instance, mixin);
        } else {
            updater.rendered = mixin;
        }
    }

    return instance;
}
