import { CategoryView } from "./CategoryView.js";
import { BaseView } from "../../BaseView.js";
import { FilterCommand } from "./FilterCommand.js";
import { GridLabelResizer } from "./GridLabelResizer.js";
export default class ContentView extends BaseView {
    constructor(options) {
        super(options);
    }
    get source() {
        return super.source;
    }
    get categories() {
        return this.controls.getByType(CategoryView);
    }
    template() {
        return `<div class="-fb-grid-content">
                    <div data-controls></div>
                </div>`;
    }
    onActivate(e) {
        super.onActivate(e);
        this.populate();
        this.node.on(Jade.BrowserEvent.SCROLL, _.bind(this.onScroll, this));
        this.controls.add(new GridLabelResizer({
            parent: this,
            source: this.source
        }));
        this.addEventSubscriptions();
    }
    onComplete(e) {
        super.onComplete(e);
        let scroll = this.source
            .locals.storage?.scroll;
        if (scroll != null) {
            this.node.scrollTop(scroll);
        }
        let filter = this.source
            .locals.storage?.filter;
        if (filter != null) {
            FilterCommand.invoke(this, filter);
        }
    }
    onScroll(e) {
        this.source.locals.storage.scroll = this.node.scrollTop();
    }
    populate() {
        let categories = [];
        let categorized = {};
        this.source.getEditors().forEach(editor => {
            if (categories.indexOf(editor.category) < 0) {
                categories[categories.length] = editor.category;
            }
            if (categorized[editor.category] == null) {
                categorized[editor.category] = [];
            }
            let categorizedArray = categorized[editor.category];
            categorizedArray[categorizedArray.length] = editor;
        });
        categories.sort((value1, value2) => {
            return value1.localeCompare(value2);
        });
        Object.keys(categorized).forEach((key) => {
            let category = new CategoryView({
                label: key,
                editors: categorized[key],
                source: this.source
            });
            this.controls.add(category);
        });
        let collpased = this.source.locals.storage.collapsed;
        this.categories.each(category => {
            if (collpased.indexOf(category.label) > -1) {
                category.collapse();
            }
        });
    }
    addEventSubscriptions() {
        this.source.locals.events.inputSelectedSubscribe(() => {
            this.node.find(".-fb-input-selected")
                .removeClass("-fb-input-selected");
        });
        this.source.locals.events.filterSubscribe((e) => {
            FilterCommand.invoke(this, e.term);
            this.source.locals.storage.filter = e.term;
        });
        this.source.locals.events.categoriesExpandAllSubscribe(() => {
            this.categories.each(category => {
                category.expand();
            });
            this.addCategoryCollapseStateToStorage();
        });
        this.source.locals.events.categoriesCollapseAllSubscribe(() => {
            this.categories.each(category => {
                category.collapse();
            });
            this.addCategoryCollapseStateToStorage();
        });
        this.source.locals.events.categoryToggleSubscribe(() => {
            this.addCategoryCollapseStateToStorage();
        });
    }
    addCategoryCollapseStateToStorage() {
        let collapsed = [];
        this.categories.each((category) => {
            if (!category.isExpanded) {
                collapsed.push(category.label);
            }
        });
        this.source.locals.storage.collapsed = collapsed;
    }
}
