import { noop } from "./util";
import Updater from "./Updater";
import { CurrentOwner } from "./createElement";
function alwaysNull() {
    return null;
}
export function instantiateComponent(type, vtype, props, context) {
    var instance;
    if (vtype === 2) {
        instance = new type(props, context);
        //props, context是不可变的
        instance.props = props;
        instance.context = context;
        new Updater(instance);
    } 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);
        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;
}
