import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
import "core-js/modules/web.dom.iterable";
import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
import "core-js/modules/es6.function.name";
import Input from '../input';
import Dropdown, { DropdownItem, DropdownMenu } from '../dropdown';
import { isStringOrNumber, getTextByChildren, addStyle, _$, getRestProps, noop } from '../utils';
import Option from './option';
import OptionGroup from './group';
import { Tabs, Tab } from '../tabs';
import Icon from '../icon';
export default function (obj, _Vdt, blocks, $callee) {
  var _classNameObj;

  _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(),
      className = _self$get.className,
      clearable = _self$get.clearable,
      data = _self$get.data,
      value = _self$get.value,
      disabled = _self$get.disabled,
      filterable = _self$get.filterable,
      keywords = _self$get.keywords,
      _show = _self$get._show,
      placeholder = _self$get.placeholder,
      size = _self$get.size,
      style = _self$get.style,
      name = _self$get.name,
      children = _self$get.children,
      fluid = _self$get.fluid,
      width = _self$get.width,
      allowUnmatch = _self$get.allowUnmatch,
      card = _self$get.card,
      hideIcon = _self$get.hideIcon,
      container = _self$get.container,
      inline = _self$get.inline,
      loading = _self$get.loading,
      position = _self$get.position;
  /* istanbul ignore if */


  if (filterable && blocks.value) {
    _e(new Error("You can't specify \"value\" extension point with 'filterable'."));
  }

  var multiple = self._isMultiple();

  var _activeLabel = self.get('_activeLabel');

  var _placeholder = placeholder == null ? allowUnmatch && filterable ? _$('请输入或选择') : _$('请选择') : placeholder;

  var classNameObj = (_classNameObj = {
    'k-select': true
  }, _classNameObj[className] = className, _classNameObj[scope.className] = scope.className, _classNameObj['k-disabled'] = disabled, _classNameObj['k-show'] = _show, _classNameObj['k-clearable'] = clearable, _classNameObj["k-" + size] = size !== 'default', _classNameObj['k-fluid'] = fluid, _classNameObj['k-with-prefix'] = blocks.prefix, _classNameObj['k-with-suffix'] = !hideIcon || loading || blocks.suffix, _classNameObj['k-inline'] = inline, _classNameObj);
  var hasValue = value != null && (!multiple && value !== '' || multiple && value.length);

  var isGroup = _Array$isArray(self.get('data.0.data'));

  var labelObj = scope.labelObj || {
    // only show value as label when allowUnmatch is true, #40
    label: allowUnmatch && isStringOrNumber(value) ? value : null,
    labels: []
  };
  var groupIndex = 0;
  var hasData = isGroup;

  var Options = function Options(props) {
    var data = props.data;
    var ret = [];
    data.forEach(function (item, index) {
      // deprecate text, use label instead of
      if (item.text) item.label = item.text;

      var _self$handleProps = self.handleProps(item, labelObj),
          valid = _self$handleProps.valid,
          active = _self$handleProps.active;

      if (valid) {
        ret.push(h(DropdownItem, {
          '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)),
          'hideOnSelect': function () {
            try {
              return !multiple;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'children': function () {
            try {
              return item.template ? item.template(item, index) : item.label;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          '_context': $this,
          'ev-select': function () {
            try {
              return self._onSelect.bind(self, item.value);
            } catch (e) {
              _e(e);
            }
          }.call($this)
        })); // for card type groups, we need to highlight the parent label

        if (active && card) {
          if (_activeLabel == null && _show) {
            _activeLabel = groupIndex;

            self._setActiveLabelSilent(_activeLabel);
          }
        }

        hasData = true;
      }
    });
    return ret;
  };

  var OptionsVNodes = function OptionsVNodes(_ref) {
    var children = _ref.children;
    return __u.map(children ? _Array$isArray(children) ? children : [children] : children, function (vNode) {
      if (vNode.tag === Option) {
        var props = vNode.props;
        props = _Object$assign({}, props, {
          'ev-select': self._onSelect.bind(self, props.value),
          hideOnSelect: !multiple,
          label: props.label || getTextByChildren(props.children),
          children: props.children || props.label
        });

        var _self$handleProps2 = self.handleProps(props, labelObj),
            active = _self$handleProps2.active,
            valid = _self$handleProps2.valid;

        if (!valid) {
          return hc('', vNode.key);
        } else {
          var _className2;

          props.className = _className((_className2 = {}, _className2[props.className] = props.className, _className2['k-active'] = active, _className2));
          hasData = true;
        } // for card type groups, we need to highlight the parent label


        if (active && card) {
          if (_activeLabel == null && _show) {
            _activeLabel = groupIndex;

            self._setActiveLabelSilent(_activeLabel);
          }
        } // if the multiple select option is active
        // add a √ icon


        if (active && multiple) {
          props.children = [props.children, h('i', null, null, 'k-checkmark ion-ios-checkmark-empty')];
        } // return the cloned vNode


        vNode = _Vdt.miss.clone(vNode);
        vNode.props = props;
      } else if (vNode.tag === OptionGroup) {
        hasData = true;
        var _props = vNode.props;
        _props = _Object$assign({}, _props, {
          children: h(OptionsVNodes, {
            'children': function () {
              try {
                return _props.children;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            '_context': $this
          })
        });
        vNode = _Vdt.miss.clone(vNode);
        vNode.props = _props;
        groupIndex++;
      }

      return vNode;
    });
  };

  var labelList = [];
  var Menu = scope.Menu || h(DropdownMenu, {
    'ref': function ref(i) {
      widgets['menu'] = i;
    },
    'className': _className(function () {
      try {
        return {
          "k-select-dropdown": true,
          "k-card": card
        };
      } catch (e) {
        _e(e);
      }
    }.call($this)),
    'children': function () {
      try {
        return function () {
          var _children = [];

          if (!isGroup) {
            _children.push(h(Options, {
              'data': function () {
                try {
                  return data;
                } catch (e) {
                  _e(e);
                }
              }.call($this),
              '_context': $this
            }));
          } else {
            _children.push(__m(function () {
              try {
                return data;
              } catch (e) {
                _e(e);
              }
            }.call($this), function (value, key) {
              return h(OptionGroup, {
                'label': function () {
                  try {
                    return value.label;
                  } catch (e) {
                    _e(e);
                  }
                }.call($this),
                'children': h(Options, {
                  'data': function () {
                    try {
                      return value.data;
                    } catch (e) {
                      _e(e);
                    }
                  }.call($this),
                  'parentLabel': function () {
                    try {
                      return value.label;
                    } catch (e) {
                      _e(e);
                    }
                  }.call($this),
                  '_context': $this
                }),
                '_context': $this
              });
            }, $this));
          }

          _children.push(h(OptionsVNodes, {
            'children': function () {
              try {
                return children;
              } catch (e) {
                _e(e);
              }
            }.call($this),
            '_context': $this
          })); // if it is card type group, we extract the label as navBar


          if (card) {
            var flattenEach = function flattenEach(children) {
              children.forEach(function (vNode) {
                if (_Array$isArray(vNode)) {
                  flattenEach(vNode);
                } else if (vNode) {
                  if (vNode.tag === OptionGroup) {
                    var props = vNode.props;
                    var label = (props._blocks || {}).label || props.label;
                    labelList.push({
                      label: label
                    });
                    vNode.props = _Object$assign({}, props, {
                      _card: true
                    }); // if the _activeLabel is undefined
                    // we active the first label

                    if (_activeLabel == null && _show) {
                      _activeLabel = index;

                      self._setActiveLabelSilent(index);
                    } // only render the active options


                    if (_activeLabel === index) {
                      vNodes.push(vNode);
                    }

                    index++;
                  } else {
                    vNodes.push(vNode);
                  }
                }
              });
            };

            var vNodes = [];
            var index = 0;
            flattenEach(_children);
            _children = [h(Tabs, {
              'vertical': function () {
                try {
                  return true;
                } catch (e) {
                  _e(e);
                }
              }.call($this),
              'type': 'border-card',
              'children': __m(function () {
                try {
                  return labelList;
                } catch (e) {
                  _e(e);
                }
              }.call($this), function (value, key) {
                return h(Tab, {
                  'value': function () {
                    try {
                      return key;
                    } catch (e) {
                      _e(e);
                    }
                  }.call($this),
                  'children': function () {
                    try {
                      return typeof value.label === 'function' ? // pass by block
                      value.label(noop) : // pass by property
                      value.label;
                    } catch (e) {
                      _e(e);
                    }
                  }.call($this),
                  '_context': $this
                });
              }, $this),
              '_context': $this,
              'value': _getModel(self, '_activeLabel'),
              'ev-$change:value': function ev$changeValue(__c, __n) {
                _setModel(self, '_activeLabel', __n, $this);
              }
            }), h('div', null, function () {
              try {
                return vNodes;
              } catch (e) {
                _e(e);
              }
            }.call($this), 'k-groups')];
          }

          if (!hasData) {
            return h('div', null, function () {
              try {
                return _$('无数据');
              } catch (e) {
                _e(e);
              }
            }.call($this), 'k-no-data');
          }

          return _children;
        }();
      } 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)
  }); // if the value is not in options, then set hasValue to false

  if (hasValue && labelObj.label == null && !labelObj.labels.length) {
    hasValue = false; // if this is a value, but the value does not exist in options
    // we set it to empty, #41

    self._clearValue();
  }

  return h('div', _Object$assign({}, function () {
    try {
      return getRestProps(self);
    } catch (e) {
      _e(e);
    }
  }.call($this), {
    'style': function () {
      try {
        return width != null ? addStyle(style, {
          width: width + "px"
        }) : style;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'tabindex': function () {
      try {
        return disabled ? "-1" : "0";
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'ev-keypress': function () {
      try {
        return self._onKeypress;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'ev-keydown': function () {
      try {
        return self._onKeydown;
      } catch (e) {
        _e(e);
      }
    }.call($this)
  }), h(Dropdown, {
    'position': function () {
      try {
        return position;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'trigger': 'click',
    'disabled': function () {
      try {
        return disabled;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'ref': function ref(i) {
      widgets['dropdown'] = i;
    },
    'container': function () {
      try {
        return container;
      } catch (e) {
        _e(e);
      }
    }.call($this),
    'children': [h('div', {
      'tabindex': '-1',
      'ev-click': function () {
        try {
          return self._focusInput;
        } catch (e) {
          _e(e);
        }
      }.call($this)
    }, [function () {
      try {
        return blocks.prefix;
      } catch (e) {
        _e(e);
      }
    }.call($this) ? h('div', null, (_blocks['prefix'] = function (parent) {
      return null;
    }) && (__blocks['prefix'] = function (parent) {
      var args = arguments;
      return blocks['prefix'] ? blocks['prefix'].apply($this, [function () {
        return _blocks['prefix'].apply($this, args);
      }].concat(__slice.call(args, 1))) : _blocks['prefix'].apply($this, args);
    }) && __blocks['prefix'].apply($this, [__noop]), 'k-prefix') : undefined, h(Animate, {
      'a:disabled': function () {
        try {
          return true;
        } catch (e) {
          _e(e);
        }
      }.call($this),
      'className': 'k-main',
      'children': [h('input', {
        'type': 'hidden',
        'value': function () {
          try {
            return value;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'name': function () {
          try {
            return name;
          } catch (e) {
            _e(e);
          }
        }.call($this)
      }), function () {
        try {
          return !multiple && filterable;
        } catch (e) {
          _e(e);
        }
      }.call($this) ? h(Input, {
        'value': function () {
          try {
            return keywords == null ? labelObj.label : keywords;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'disabled': function () {
          try {
            return disabled;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'placeholder': function () {
          try {
            return _placeholder;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'ref': function ref(i) {
          widgets['input'] = i;
        },
        'size': function () {
          try {
            return size;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'fluid': function () {
          try {
            return true;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        '_context': $this,
        'ev-input': function () {
          try {
            return self._onSearch;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'ev-blur': function () {
          try {
            return self._onBlur;
          } catch (e) {
            _e(e);
          }
        }.call($this),
        'ev-focus': function () {
          try {
            return self._selectInput;
          } catch (e) {
            _e(e);
          }
        }.call($this)
      }) : function () {
        try {
          return !filterable && !hasValue;
        } catch (e) {
          _e(e);
        }
      }.call($this) ? h('span', null, function () {
        try {
          return _placeholder;
        } catch (e) {
          _e(e);
        }
      }.call($this), 'k-placeholder c-ellipsis') : function () {
        try {
          return !multiple;
        } catch (e) {
          _e(e);
        }
      }.call($this) ? h('span', null, (_blocks['value'] = function (parent) {
        return function () {
          try {
            return labelObj.label;
          } catch (e) {
            _e(e);
          }
        }.call($this);
      }) && (__blocks['value'] = function (parent) {
        var args = arguments;
        return blocks['value'] ? blocks['value'].apply($this, [function () {
          return _blocks['value'].apply($this, args);
        }].concat(__slice.call(args, 1))) : _blocks['value'].apply($this, args);
      }) && __blocks['value'].apply($this, [__noop].concat(function () {
        try {
          return [value, labelObj.label];
        } catch (e) {
          _e(e);
        }
      }.call($this))), 'k-value c-ellipsis') : h(Animate, {
        'a:tag': 'span',
        'a:transition': 'c-fade',
        'className': 'k-values',
        'children': [__m(function () {
          try {
            return labelObj.labels;
          } catch (e) {
            _e(e);
          }
        }.call($this), function (item, index) {
          return h(Animate, {
            'a:tag': 'span',
            'className': 'k-select-tag',
            'key': function () {
              try {
                return value[index];
              } catch (e) {
                _e(e);
              }
            }.call($this),
            'a:transition': 'c-fade',
            'children': [h('span', null, (_blocks['value'] = function (parent) {
              return function () {
                try {
                  return item;
                } catch (e) {
                  _e(e);
                }
              }.call($this);
            }) && (__blocks['value'] = function (parent) {
              var args = arguments;
              return blocks['value'] ? blocks['value'].apply($this, [function () {
                return _blocks['value'].apply($this, args);
              }].concat(__slice.call(args, 1))) : _blocks['value'].apply($this, args);
            }) && __blocks['value'].apply($this, [__noop].concat(function () {
              try {
                return [value[index], item];
              } catch (e) {
                _e(e);
              }
            }.call($this))), 'k-text'), h(Icon, {
              'className': 'ion-ios-close-empty k-close',
              '_context': $this,
              'ev-click': function () {
                try {
                  return self._delete.bind(self, value[index]);
                } catch (e) {
                  _e(e);
                }
              }.call($this)
            })],
            '_context': $this
          });
        }, $this), function () {
          try {
            return filterable;
          } catch (e) {
            _e(e);
          }
        }.call($this) ? h(Input, {
          'value': function () {
            try {
              return keywords;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'disabled': function () {
            try {
              return disabled;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'placeholder': function () {
            try {
              return !hasValue ? _placeholder : '';
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'ref': function ref(i) {
            widgets['input'] = i;
          },
          'autoWidth': function () {
            try {
              return true;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'size': function () {
            try {
              return size;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          '_context': $this,
          'ev-input': function () {
            try {
              return self._onSearch;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'ev-blur': function () {
            try {
              return self._onBlur;
            } catch (e) {
              _e(e);
            }
          }.call($this),
          'ev-focus': function () {
            try {
              return self._onFocus;
            } catch (e) {
              _e(e);
            }
          }.call($this)
        }) : undefined],
        '_context': $this
      })],
      '_context': $this
    }), function () {
      try {
        return !hideIcon || clearable || loading || blocks.suffix;
      } catch (e) {
        _e(e);
      }
    }.call($this) ? h('span', null, [function () {
      try {
        return clearable;
      } catch (e) {
        _e(e);
      }
    }.call($this) ? h(Icon, {
      'className': _className(function () {
        try {
          return {
            "k-clear ion-ios-close": true,
            "k-show": hasValue
          };
        } catch (e) {
          _e(e);
        }
      }.call($this)),
      '_context': $this,
      'ev-click': function () {
        try {
          return self._onClear;
        } catch (e) {
          _e(e);
        }
      }.call($this)
    }) : undefined, h('span', null, (_blocks['suffix'] = function (parent) {
      return function () {
        try {
          return loading;
        } catch (e) {
          _e(e);
        }
      }.call($this) ? h(Icon, {
        'className': 'ion-load-c icon-loading',
        '_context': $this
      }) : function () {
        try {
          return !hideIcon;
        } catch (e) {
          _e(e);
        }
      }.call($this) ? h('i', null, null, 'k-arrow ion-arrow-down-b') : undefined;
    }) && (__blocks['suffix'] = function (parent) {
      var args = arguments;
      return blocks['suffix'] ? blocks['suffix'].apply($this, [function () {
        return _blocks['suffix'].apply($this, args);
      }].concat(__slice.call(args, 1))) : _blocks['suffix'].apply($this, args);
    }) && __blocks['suffix'].apply($this, [__noop]), 'k-suffix-icon')], 'k-suffix') : undefined], 'k-wrapper', null, function (i) {
      widgets['wrapper'] = i;
    }), function () {
      try {
        return Menu;
      } catch (e) {
        _e(e);
      }
    }.call($this)],
    '_context': $this
  }), _className(function () {
    try {
      return _Object$assign({}, classNameObj, {
        'k-has-value': hasValue
      });
    } catch (e) {
      _e(e);
    }
  }.call($this)));
}