import {isObj, isVal} from "../../core/types";

export function Store(reducer) {
    this.state = reducer(undefined, {});

    if (!isVal(this.state)) throw TypeError('Store: Initial state is null or undefined,' +
        ' reducer does not return properly on random/empty state')

    Object.defineProperty(this, 'listeners', {
        value: [],
        writable: false,
        configurable: false
    })

    Object.defineProperty(this, 'reducer', {
        value: reducer,
        writable: false,
        configurable: false
    })

    this.subscribe = function (callback) {
        this.listeners.push(callback)
    }

    this.unsubscribe = function (callback) {
        this.listeners.splice(this.listeners.indexOf(callback), 1)
    }

    this.getState = function () {
        return this.state
    }

    this.dispatch = function (action) {
        this.state = this.reducer(this.getState(), action)
        this.listeners.forEach((callback)=>{
            setTimeout(callback, 1)
        })
    }

}
