import {AnalyticalScope, Filter, FilterComponent, FilterUtil} from "@webbpm/base-package";
import {DropdownTreeViewComponent} from "../field/DropdownTreeViewComponent";

@AnalyticalScope(DropdownTreeViewComponent)
export class DropdownTreeViewFilterComponent extends FilterComponent {

  public isBusinessId: boolean;

  public getFilter(): Filter {
    let treeViewComponent = this.getScript(DropdownTreeViewComponent) as DropdownTreeViewComponent;
    let model = treeViewComponent.value;

    if (!model && treeViewComponent.cachedValue) {
      let value = this.isBusinessId
                  ? treeViewComponent.cachedValue.businessId
                  : treeViewComponent.cachedValue.id;
      return FilterUtil.singleValueFilter(this.getObjectId(), value, this.operation);
    }
    else if (model) {
      let value = this.isBusinessId ? model.businessId : model.id;
      return FilterUtil.singleValueFilter(this.getObjectId(), value, this.operation);
    }
    else {
      return null;
    }
  }
}
