import { ForeachMapAction, IMap } from "./IMap";

/*
* name;
*/
export class StringMap<T> implements IMap<string, T> {

    private items: { [index: string]: T } = {};

    private count: number = 0;

    public Count(): number {
        return this.count;
    }

    public Add(key: string, value: T): T {
        if (!this.items.hasOwnProperty(key))
            this.count++;

        this.items[key] = value;
        return value;
    }

    public Remove(key: string): T {
        var val = this.items[key];
        delete this.items[key];
        this.count--;
        return val;
    }


    public ContainsKey(key: string): boolean {
        return this.items.hasOwnProperty(key);
    }


    public Value(key: string): T {
        return this.items[key];
    }


    public Replace(key: string, value: T): boolean {
        if (this.ContainsKey(key)) {
            this.items[key] = value;
            return true;
        }
        return false;
    }

    public Keys(): string[] {
        var keySet: string[] = [];

        for (var prop in this.items) {
            if (this.items.hasOwnProperty(prop))
                keySet.push(prop);
        }
        return keySet;
    }

    public Values(): T[] {
        var values: T[] = [];

        for (var prop in this.items) {
            if (this.items.hasOwnProperty(prop))
                values.push(this.items[prop]);
        }

        return values;
    }

    public Foreach(callback: ForeachMapAction<string, T>, thisArg?: any): void {
        if (callback) {
            for (var prop in this.items) {
                callback.call(thisArg, prop, this.items[prop]);
            }
        }
    }

    public Clear(): void {
        this.items = {};
        this.count = 0;
    }
}