var Typeahead = Namespace.declare("com.twitter.Bootstrap",
    class Typeahead extends UcpComponent {
        static get implements() { return []; };
        static start() {}
            /* static get dependencies() {
                return [
                    "/EAMD.ucp/Components/com/twitter/Bootstrap/3.3.7/Bootstrap.component.xml"
                ];
            } */


        static get dependencies() {
            return [
                "/EAMD.ucp/Components/com/twitter/Bootstrap/3.3.7/Bootstrap.component.xml",
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Typeahead/1.0.0/dist/bootstrap3-typeahead.min.js"
            ];
        }

        /* constructor() {
            super();
            this.controller = UcpComponentSupport.lookup("Namespaces.org.JQuery").controller;
            this.controller.singleton = this;
        } */

        constructor() {
            super();
            this.source = [
                { id: "someId1", name: "Display name 1" },
                { id: "someId2", name: "Display name 2" }
            ];
            this.cssClasses = "";
        }

        init(controller) {
            if (this._private.isInitialized)
                return;
            super.init(controller);
            // this.initializeModel();
            return this;
        }

        /* initializeModel() {
            this.model = {
                source: [
                    { id: "someId1", name: "Display name 1" },
                    { id: "someId2", name: "Display name 2" }
                ]
            }
        } */

        attachModel(model) {
            var $input = $("#" + this.defaultView.id);
            $input.typeahead('destroy');
            if (model) {
                $input.typeahead(model);
            } else {
                $input.typeahead(this._private.ucpModel.properties);
            }

        }

        get model() {
            return super.model;
        }
        set model(newValue) {
            //         	 if (!newValue.cssClass)
            //         		newValue.cssClass = "";
            //         	if (!newValue.href)
            //         		newValue.href = "#";
            //         	if (!newValue.badge)
            //         		newValue.badge = "0";
            //         	if (!newValue.description)
            //                 newValue.description = "DefaultView"; 

            super.model = newValue;
            let typeAheadModel = this.convertModelToTypeAheadModel(newValue);
            if (typeAheadModel) {
                this.attachModel(typeAheadModel);
            }

        }

        convertModelToTypeAheadModel(newModel) {
            try {
                var finalObject;
                if (newModel) {
                    if (newModel instanceof Array) {
                        let tempArray = new Array();
                        let finalArray = new Array();
                        newModel.forEach(model => {
                            if (typeof(model) == 'string' || model instanceof String) {
                                tempArray.push({ id: model, name: model });
                            }
                        });

                        /* {
                            source: [
                                { id: "someId1", name: "Display name 1" },
                                { id: "someId2", name: "Display name 2" }
                            ],
                            autoSelect: true
                        } */

                        //finalArray.push({ source: tempArray, autoSelect: true }); 
                        if (tempArray.length > 0) {
                            finalObject = { source: tempArray };
                        }
                    }

                }
            } catch (err) {
                console.debug("Please provide correct model(model array)." + err);
            }
            return finalObject;
        }

        createDefaultView() {
            return new TypeaheadDefaultView();
        }

        /* handleSelection(event, item) {
            console.info(this.constructor.name + ": handleSelection", item);
            this.convertModelToTypeAheadModel(item);
            this.attachModel();
        } */

    }
);

var TypeaheadDefaultView = Namespace.declare("com.twitter.Bootstrap.Typeahead",
    class TypeaheadDefaultView extends UcpView {

        append(element) {
            super.append(element);
            this.ucpModel.ucpComponent.attachModel();
        }

    }
);