import { Component } from 'react';
import storage from 'somes/storage';
var global_state_components = new Map();
var global_states = {};
function set_global_state_value(name, value) {
    global_states[name] = value;
    if (name[1] == '$') {
        storage.set('global_state_' + name, value);
    }
}
export default class GlobalState extends Component {
    constructor() {
        super(...arguments);
        this.state = {};
    }
    _fillGlobalStateValue() {
        var set = new Set();
        var state = this.state;
        if (state) {
            var change = false;
            for (var name of Object.keys(state)) {
                if (name[0] == '$') {
                    if (name[1] == '$') {
                        if (!(name in global_states)) {
                            global_states[name] = storage.get('global_state_' + name);
                        }
                    }
                    if (name in global_states) {
                        var fill_value = global_states[name];
                        if (fill_value !== state[name]) {
                            change = true;
                            state[name] = fill_value;
                        }
                    }
                    else {
                        set_global_state_value(name, state[name]);
                    }
                    set.add(name);
                }
            }
            if (change) {
                Component.prototype.setState.call(this, state);
            }
        }
        return set;
    }
    setState(state, callback) {
        GlobalState.setGlobalState(state, this, callback);
    }
    UNSAFE_componentWillMount() {
        this.__initialize__();
    }
    __initialize__() {
        if (!global_state_components.has(this)) {
            var set = this._fillGlobalStateValue();
            if (set.size) {
                global_state_components.set(this, set);
                this._is_global = true;
            }
        }
    }
    componentWillUnmount() {
        if (this._is_global)
            global_state_components.delete(this);
    }
    static getGlobalState(name) {
        var r = global_states[name];
        if (r === undefined) {
            global_states[name] = r = storage.get('global_state_' + name);
        }
        return r;
    }
    static setGlobalState(state, self, callback) {
        if (!state)
            return;
        var global_state = [];
        for (var name of Object.keys(state)) {
            if (name[0] == '$') {
                global_state.push(name);
                set_global_state_value(name, state[name]);
            }
        }
        if (global_state.length) {
            for (var [com, set] of global_state_components) {
                if (com !== self) {
                    var _state = {}, update = false;
                    for (var name of global_state) {
                        if (set.has(name)) {
                            update = true;
                            _state[name] = global_states[name];
                        }
                    }
                    if (update) {
                        Component.prototype.setState.call(com, _state);
                    }
                }
            }
        }
        if (self) {
            Component.prototype.setState.call(self, state, callback);
        }
    }
}
