import { shortcut, extend, emptyFn, createWidget, bind, isNotNull } from "@/core";
import { AbstractTreeValueChooser } from "./abstract.treevaluechooser";
import { MultiSelectTree } from "@/widget/multiselecttree/multiselecttree";
import { MultiSelectTreePopup } from "@/widget/multiselecttree/multiselecttree.popup";

@shortcut()
export class TreeValueChooserPane extends AbstractTreeValueChooser {
    static xtype = "bi.tree_value_chooser_pane";

    static EVENT_CHANGE = "EVENT_CHANGE";

    _defaultConfig() {
        return extend(super._defaultConfig(...arguments), {
            baseCls: "bi-tree-value-chooser-pane",
            items: null,
            itemsCreator: emptyFn,
            showLine: true,
        });
    }

    _init() {
        super._init(...arguments);
        const o = this.options;
        this.pane = createWidget({
            type: o.hideSearch ? MultiSelectTreePopup.xtype : MultiSelectTree.xtype,
            element: this,
            showLine: o.showLine,
            itemsCreator: bind(this._itemsCreator, this),
        });

        this.pane.on(MultiSelectTree.EVENT_CHANGE, () => {
            this.fireEvent(TreeValueChooserPane.EVENT_CHANGE);
        });
        if (isNotNull(o.value)) {
            const selectedValues = this.assertSelectedValue(o.value, o.items);
            this.pane.setSelectedValue(selectedValues);
        }
        if (isNotNull(o.items)) {
            this._initData(o.items);
            this.pane.populate();
        }
    }

    setSelectedValue(v) {
        this.pane.setSelectedValue(v);
    }

    setValue(v) {
        this.pane.setValue(v);
    }

    getValue() {
        return this.pane.getValue();
    }

    getAllValue() {
        return this.buildCompleteTree(this.pane.getValue());
    }

    populate(items) {
        if (isNotNull(items)) {
            this._initData(items);
        }
        this.pane.populate();
    }
}