/// <reference path="leaf.ts" />
namespace Leaf {
    export class Namespace {
        private Widgets: { [index: string]: WidgetConstructor } = {}
        public include(W, name: string = W.prototype.name) {
            if (W.prototype instanceof Widget) {
                if (this.Widgets[name]) {
                    throw new Error(`Widget namespace conflicting: ${name}`)
                }

                this.Widgets[name] = W
            } else {
                console.warn(W, "not inherit WidgetBase")
            }
        }
        public getWidgetByName(name: string) {
            name = Leaf.Util.slugToCamel(name)
            name = Leaf.Util.capitalize(name)
            for (let name in this.Widgets) {
                let W = this.Widgets[name]
                if (W["name"] === name || W.prototype.name === name || W.constructor["name"] == name) {
                    return W
                }
            }
            return null
        }
        public createWidgetByName(name: string, props?: any): WidgetAny {
            let W = this.getWidgetByName(name)
            if (!W) return
            let w = new W(props) as WidgetAny
            return w
        }
        public isTagWidgetName(name: string, who: WidgetAny) {
            name = Leaf.Util.slugToCamel(name)
            name = Leaf.Util.capitalize(name)
            if (name === who.name) {
                return true
            }
            return false
        }
        public clear() {
            this.Widgets = {}
        }
        public has(name: string) {
            name = Leaf.Util.capitalize(Leaf.Util.slugToCamel(name))
            if (this.Widgets[name]) return true
            return false
        }
    }
}
