import utils from 'somes';
import * as React from 'react';
import GlobalState from './state';
import { Component } from 'react';
const persistentState = new Map();
class _ {
}
class ViewControllerIMPL extends GlobalState {
    get isLoaded() {
        return !!this.m_loaded;
    }
    get isMounted() {
        return !!this.m_mounted;
    }
    get isDestroy() {
        return !!this._destroy;
    }
    get persistentID() {
        var id = this.constructor.__persistentID__;
        if (!id) {
            this.constructor.__persistentID__ = id = String(utils.getId());
        }
        return id;
    }
    saveState() {
        return null;
    }
    recoveryState() {
        return persistentState.get(this.persistentID);
    }
    __initialize__() {
        var state = this.recoveryState();
        if (state) {
            this.state = Object.assign(this.state || {}, state);
            Component.prototype.setState.call(this, state);
        }
        super.__initialize__();
        var r = this.triggerLoad();
        if (r instanceof Promise) {
            r.then(() => {
                this.m_loaded = true;
                this.forceUpdate();
            });
        }
        else {
            this.m_loaded = true;
            this.forceUpdate();
        }
    }
    async componentDidMount() {
        this.m_mounted = true;
        this.triggerMounted();
    }
    componentWillUnmount() {
        this._destroy = true;
        var state = this.saveState();
        if (state) {
            persistentState.set(this.persistentID, state);
        }
        else {
            persistentState.delete(this.persistentID);
        }
        super.componentWillUnmount();
        this.triggerRemove();
    }
    componentDidUpdate(prevProps, prevState) {
        this.triggerUpdate(prevProps, prevState);
    }
    componentDidCatch(error, errorInfo) {
        this.triggerError(error, errorInfo);
    }
    triggerLoad() { }
    triggerMounted() { }
    triggerUpdate(prevProps, prevState) { }
    triggerRemove() { }
    triggerError(error, errorInfo) { }
}
export { React };
export const ViewController = ViewControllerIMPL;
