import "core-js/modules/es6.regexp.split";
import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
import "core-js/modules/web.dom.iterable";
import Input from '../input';
import Dropdown, { DropdownItem, DropdownMenu } from '../dropdown';
export default function (obj, _Vdt, blocks, $callee) {
  _Vdt || (_Vdt = Vdt);
  obj || (obj = {});
  blocks || (blocks = {});
  var h = _Vdt.miss.h,
      hc = _Vdt.miss.hc,
      hu = _Vdt.miss.hu,
      widgets = this && this.widgets || {},
      _blocks = {},
      __blocks = {},
      __u = _Vdt.utils,
      extend = __u.extend,
      _e = __u.error,
      _className = __u.className,
      __slice = __u.slice,
      __noop = __u.noop,
      __m = __u.map,
      __o = __u.Options,
      _getModel = __o.getModel,
      _setModel = __o.setModel,
      _setCheckboxModel = __u.setCheckboxModel,
      _detectCheckboxChecked = __u.detectCheckboxChecked,
      _setSelectModel = __u.setSelectModel,
      self = this.data,
      $this = this,
      scope = obj,
      Animate = self && self.Animate,
      parent = ($callee || {})._super;

  var _self$get = self.get(),
      data = _self$get.data,
      className = _self$get.className,
      style = _self$get.style,
      value = _self$get.value,
      trigger = _self$get.trigger,
      changeOnSelect = _self$get.changeOnSelect,
      format = _self$get.format,
      filterable = _self$get.filterable,
      keywords = _self$get.keywords,
      _show = _self$get._show;

  var labelObj = {
    label: null,
    labels: [],
    values: []
  };
  var hasData = false;

  var Options = function Options(props) {
    var data = props.data || [];
    var level = props.level;
    var parentShowed = props.parentShowed;
    var ret = []; // show loading

    if (!data.length) {
      return h('i', null, null, 'ion-load-c icon-loading');
    }

    data.forEach(function (item, index) {
      var _self$handleProps = self.handleProps(item, labelObj, level),
          active = _self$handleProps.active,
          selected = _self$handleProps.selected;

      var Item = function Item() {
        var events = {};
        events['ev-click'] = self._onClick.bind(self, item, level);

        if (trigger === 'hover') {
          events['ev-mouseenter'] = self._onClick.bind(self, item, level);
        }

        if (!changeOnSelect) {
          events['ev-select'] = self._onSelect;
        }

        return h(DropdownItem, _Object$assign({
          'disabled': function () {
            try {
              return item.disabled;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'className': _className(function () {
            try {
              return {
                'k-active': active
              };
            } catch (e) {
              _e(e);
            }
          }.call($this))
        }, function () {
          try {
            return events;
          } catch (e) {
            _e(e);
          }
        }.call($this), {
          'children': [function () {
            try {
              return item.template ? item.template(item, index) : item.label;
            } catch (e) {
              _e(e);
            }
          }.call($this), function () {
            try {
              return item.children;
            } catch (e) {
              _e(e);
            }
          }.call($this) ? h('i', null, null, 'k-arrow ion-ios-arrow-right') : undefined],
          '_context': $this
        }));
      };

      var show = parentShowed && active;
      ret.push(!item.children ? h(Item, {
        '_context': $this
      }) : h(Dropdown, {
        'position': function () {
          try {
            return {
              my: 'left top',
              at: 'right top',
              collisionDirection: ['left']
            };
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'of': 'parent',
        'disabled': function () {
          try {
            return item.disabled;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'trigger': function () {
          try {
            return trigger;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'value': function () {
          try {
            return show;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'children': [h(Item, {
          '_context': $this
        }), h(DropdownMenu, {
          'className': _className(function () {
            try {
              return {
                "k-select-dropdown": true,
                "k-cascader-dropdown": true
              };
            } catch (e) {
              _e(e);
            }
          }.call($this)),
          'children': function () {
            try {
              return show || selected;
            } catch (e) {
              _e(e);
            }
          }.call($this) ? h(Options, {
            'data': function () {
              try {
                return item.children;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            'level': function () {
              try {
                return level + 1;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            'parentShowed': function () {
              try {
                return show;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            '_context': $this
          }) : undefined,
          '_context': $this,
          'ev-show': function () {
            try {
              return self._onSubMenuShow.bind(self, item);
            } catch (e) {
              _e(e);
            }
          }.call($this)
        })],
        '_context': $this
      }));
    });
    return ret;
  };

  var Menu;

  if (!filterable || !keywords) {
    Menu = h(DropdownMenu, {
      'ref': function ref(i) {
        widgets['cascader'] = i;
      },
      'className': _className(function () {
        try {
          return {
            "k-select-dropdown": true,
            "k-cascader-dropdown": true
          };
        } catch (e) {
          _e(e);
        }
      }.call($this)),
      'key': 'cascader',
      'children': function () {
        try {
          return data && data.length;
        } catch (e) {
          _e(e);
        }
      }.call($this) ? h(Options, {
        'data': function () {
          try {
            return data;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'level': function () {
          try {
            return 0;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'parentShowed': function () {
          try {
            return _show;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        '_context': $this
      }) : h('div', null, '无数据', 'k-no-data'),
      '_context': $this,
      'ev-$change:value': function () {
        try {
          return self._onChangeShow;
        } catch (e) {
          _e(e);
        }
      }.call($this)
    });
  } else {
    Menu = h(DropdownMenu, {
      'ref': function ref(i) {
        widgets['menu'] = i;
      },
      'className': _className(function () {
        try {
          return {
            "k-select-dropdown": true,
            "k-cascader-filterable-dropdown": true
          };
        } catch (e) {
          _e(e);
        }
      }.call($this)),
      'key': 'filterable',
      'children': function () {
        try {
          return function () {
            var items = self._filter();

            if (!items.length) return h('div', null, '无匹配数据', 'k-no-data');
            return __m(function () {
              try {
                return items;
              } catch (e) {
                _e(e);
              }
            }.call($this), function (value, key) {
              return h(DropdownItem, {
                'disabled': function () {
                  try {
                    return value.disabled;
                  } catch (e) {
                    _e(e);
                  }
                }.call($this),
                'children': function () {
                  try {
                    var label, labels, length;
                    return label = value.map(function (item) {
                      return item.label;
                    }).join(' / '), labels = label.split(keywords), length = labels.length, labels.map(function (item, index) {
                      var ret = [item];

                      if (index !== length - 1) {
                        ret.push(h('em', null, function () {
                          try {
                            return keywords;
                          } catch (e) {
                            _e(e);
                          }
                        }.call($this)));
                      }

                      return ret;
                    });
                  } catch (e) {
                    _e(e);
                  }
                }.call($this),
                '_context': $this,
                'ev-select': function () {
                  try {
                    return self._selectByFilter.bind(self, value);
                  } catch (e) {
                    _e(e);
                  }
                }.call($this)
              });
            }, $this);
          }();
        } catch (e) {
          _e(e);
        }
      }.call($this),
      '_context': $this,
      'ev-$changed:value': function () {
        try {
          return self._position;
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'ev-$change:value': function () {
        try {
          return self._onChangeShow;
        } catch (e) {
          _e(e);
        }
      }.call($this)
    });
  }

  labelObj.label = blocks.format ? (_blocks['format'] = function (parent) {
    return function () {
      try {
        return labelObj.values.map(function (value) {
          return value.label;
        }).join(' / ');
      } catch (e) {
        _e(e);
      }
    }.call($this);
  }) && (__blocks['format'] = function (parent) {
    var args = arguments;
    return blocks['format'] ? blocks['format'].apply($this, [function () {
      return _blocks['format'].apply($this, args);
    }].concat(__slice.call(args, 1))) : _blocks['format'].apply($this, args);
  }) && __blocks['format'].apply($this, [__noop].concat(function () {
    try {
      return [labelObj.values];
    } catch (e) {
      _e(e);
    }
  }.call($this))) : format.call(this, labelObj.values);
  return function () {
    var _obj = {
      'Menu': function () {
        try {
          return Menu;
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'className': 'k-cascader',
      'labelObj': function () {
        try {
          return labelObj;
        } catch (e) {
          _e(e);
        }
      }.call($this)
    };
    return parent.call($this, _obj, _Vdt, blocks, parent);
  }.call($this);
}