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

        constructor() {
            super();
            this.name = "Breadcrumb";
            this.cssClasses = "";
            this.href = "#";
        }

        init() {
            if (this._private.isInitialized) {
                return;
            }

            super.init();
            return this;
        }

        async updateView(view) {
            if (view instanceof DefaultView) {
                view.clear();
                let container = view.container;
                if (container.element) {
                    container = container.element;
                }
                const list = this.getList(view);
                console.debug(list);
                //make sure all parents are loaded
                list.map(item => (item instanceof UcpModel && !item.isLoaded) ? item.load() : item);
                // await Promise.all(arr);

                list.map(async element => {
                    const item = await BreadcrumbContent.getInstance().init(this, element);
                    item.prepend(container);
                    return null;
                });
            }
        }

        getList(entity) {
            if (entity instanceof DefaultView) {
                return this.getListFromDefaultView(entity);
            }
            console.warn(`getting list from something else is not implemented`);
            return [];
        }

        getListFromDefaultView(view) {
            const list = [];
            let parent = view.model;
            while (parent) {
                list.push(parent);
                if (parent.parent === parent) {
                    return list;
                }
                parent = (parent.parent) ? parent.parent : parent.parentElement;
            }
            return list;
        }
    }
);

var BreadcrumbContent = Namespace.declare("com.twitter.Bootstrap.Breadcrumb",
    class BreadcrumbContent extends ItemView {
        init(breadcrumb, object) {
            return UcpComponentSupport.getUcpComponent4Object(object).then(ucpComponent => {
                const controller = breadcrumb.controller;
                super.init(controller);
                this._private.ucpModel = ucpComponent.ucpModel;
                ucpComponent.controller.addView(ucpComponent.ucpModel, this);
                this.aClass = ucpComponent.aClass || '';
                this.active = true;
                this.href = "#";
                return this;
            });
        }

        append(element) {
            super.append(element);
        }

    }
);