// cache maps
const templateMap = new Map();
const expressionMap = new Map();
const astMap = new Map();
const splitMap = new Map();
let observerKeys = {};

// helper for splitting up expressions strings
const splitter = function (stringValue: string) {
    // splits into array
    // persons.name.cool = ['persons','name','cool'];
    // persons.name[4].cool = ['persons','name','4', 'cool'];

    return stringValue.split(/\.|\[([^\]]+)\]\.?/).filter((x) => x ? x : null);
};

/**
 * Cache for storing ast, templates etc o speed up misc
 *
 */
export class Cache {

    public static templateMap = templateMap;
    public static expressionMap = expressionMap;
    public static astMap = astMap;
    public static splitMap = splitMap;
    public static keyMaps = {
        has(x: any): boolean {
            if (observerKeys[x]) {

                return true;
            } else {

                return false;
            }
        },
        get(x: any): any[string] {

            return observerKeys[x];
        },
        set(x: string): any[string] {
            const y = splitter(x);
            observerKeys[x] = y;

            return y;
        },
        getCreate(x: string): any[string] {
            if (observerKeys[x]) {
                return observerKeys[x];
            } else {
                const y = splitter(x);
                observerKeys[x] = y;

                return y;
            }
        }
    };

    public static clear() {
        templateMap.clear();
        expressionMap.clear();
        astMap.clear();
        splitMap.clear();
        observerKeys = {};
    }


}
