import { __assign } from "tslib";
import { hasOwnProperty } from '../utils/hasOwn';
var Store = (function () {
    function Store() {
        var _this = this;
        this.state = {};
        this.listeners = {};
        this.setState = function (nextState) {
            _this.state = __assign(__assign({}, _this.state), nextState);
            Object.keys(nextState).forEach(function (stateName) {
                var _a;
                ((_a = _this.listeners[stateName]) !== null && _a !== void 0 ? _a : []).forEach(function (listener) {
                    listener();
                });
            });
        };
        this.trigger = function (eventName) {
            var _a;
            ((_a = _this.listeners[eventName]) !== null && _a !== void 0 ? _a : []).forEach(function (listener) {
                listener();
            });
        };
        this.subscribe = function (eventName, listener) {
            _this.listeners[eventName] = _this.listeners[eventName] || [];
            _this.listeners[eventName].push(listener);
            return function () {
                var _a;
                var listeners = (_a = _this.listeners[eventName]) !== null && _a !== void 0 ? _a : [];
                var index = listeners.indexOf(listener);
                if (index !== -1) {
                    _this.listeners[eventName].splice(index, 1);
                }
            };
        };
    }
    Store.prototype.getState = function (propsName, callBack) {
        var _a;
        if (propsName) {
            if (callBack && !hasOwnProperty(this.state, propsName)) {
                this.setState((_a = {},
                    _a[propsName] = callBack(),
                    _a));
                return this.getState(propsName);
            }
            return this.state[propsName];
        }
        return this.state;
    };
    return Store;
}());
export default Store;
