import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs2/core-js/object/get-own-property-descriptor";
import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.array.iterator";
import _regeneratorRuntime from "@babel/runtime-corejs2/regenerator";
import "regenerator-runtime/runtime";
import _asyncToGenerator from "@babel/runtime-corejs2/helpers/asyncToGenerator";
import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import _applyDecoratedDescriptor from "@babel/runtime-corejs2/helpers/applyDecoratedDescriptor";

var _dec, _class, _init, _class2, _temp;

import Intact from 'intact-angular';
import Select from '../select';
import template from './index.vdt';
import '../../styles/kpc.styl';
import './index.styl';
var Cascader = (_dec = Intact.template(), (_class = (_temp = _class2 =
/*#__PURE__*/
function (_Select) {
  _inheritsLoose(Cascader, _Select);

  function Cascader() {
    return _Select.apply(this, arguments) || this;
  }

  var _proto = Cascader.prototype;

  _proto.defaults = function defaults() {
    return _Object$assign({}, _Select.prototype.defaults.call(this), {
      data: undefined,
      trigger: 'click',
      changeOnSelect: false,
      format: function format(values) {
        return values.map(function (value) {
          return value.label;
        }).join(' / ');
      },
      filter: function filter(keywords, props) {
        return props.label.indexOf(keywords) > -1;
      },
      loadData: function loadData(item) {},
      _value: []
    });
  };

  _proto._init = function _init() {
    var _this = this;

    this.on('$receive:value', function (c, v) {
      _this.set('_value', v);
    });
  };

  _proto._onClick = function _onClick(item, index) {
    var values = this.get('_value');

    if (!_Array$isArray(values)) {
      values = [];
    } else {
      values = values.slice(0, index);
    }

    values[index] = item.value;
    this.set('_value', values, {
      async: true
    });

    if (this.get('changeOnSelect')) {
      this.set('value', values);
    }
  };

  _proto._onSelect = function _onSelect() {
    this.set('value', this.get('_value').slice(0));
  }
  /**
   * on sub menu showed, load data if children is empty
   */
  ;

  _proto._onSubMenuShow =
  /*#__PURE__*/
  function () {
    var _onSubMenuShow2 = _asyncToGenerator(
    /*#__PURE__*/
    _regeneratorRuntime.mark(function _callee(item) {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(item.children && !item.children.length)) {
                _context.next = 4;
                break;
              }

              _context.next = 3;
              return this.get('loadData').call(this, item);

            case 3:
              this.update();

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    function _onSubMenuShow(_x) {
      return _onSubMenuShow2.apply(this, arguments);
    }

    return _onSubMenuShow;
  }();

  _proto._clearValue = function _clearValue() {};

  _proto.handleProps = function handleProps(props, labelObj, level) {
    var _this$get = this.get(),
        _value = _this$get._value,
        value = _this$get.value;

    var active = false;
    var selected = false;

    if (_Array$isArray(_value)) {
      if (_value[level] === props.value) {
        active = true;
      }
    }

    if (_Array$isArray(value)) {
      if (value[level] === props.value) {
        labelObj.values.push(props);
        selected = true;
      }
    }

    return {
      active: active,
      selected: selected
    };
  };

  _proto._onSearch = function _onSearch(e) {
    var keywords = e.target.value.trim();

    if (keywords) {
      _Select.prototype._onSearch.call(this, e);
    } else {
      this.set('keywords', keywords);
      this.refs.cascader.show();
    }
  };

  _proto._filter = function _filter() {
    var _this2 = this;

    var _this$get2 = this.get(),
        data = _this$get2.data,
        keywords = _this$get2.keywords,
        filter = _this$get2.filter;

    var ret = [];

    var walk = function walk(data, prefix, valid, disabled) {
      if (prefix === void 0) {
        prefix = [];
      }

      if (valid === void 0) {
        valid = false;
      }

      if (disabled === void 0) {
        disabled = false;
      }

      data.forEach(function (item) {
        var _valid = valid || filter.call(_this2, keywords, item);

        var _disabled = disabled || item.disabled;

        var _prefix = prefix.slice(0);

        _prefix.push(item);

        if (item.children) {
          walk(item.children, _prefix, _valid, _disabled);
        } else if (_valid) {
          _prefix.disabled = _disabled;
          ret.push(_prefix);
        }
      });
    };

    walk(data);
    return ret;
  };

  _proto._selectByFilter = function _selectByFilter(value) {
    var _value = value.map(function (item) {
      return item.value;
    });

    this.set({
      value: _value,
      _value: _value
    }, {
      async: true
    });

    this._resetSearch();
  };

  return Cascader;
}(Select), _defineProperty(_class2, "template", template), _defineProperty(_class2, "blocks", [].concat(Select.blocks, ['format']).filter(Boolean)), _defineProperty(_class2, "propTypes", _Object$assign({}, Select.propTypes, {
  data: Array,
  trigger: String,
  changeOnSelect: Boolean,
  format: Function,
  filter: Function,
  loadData: Function
})), _temp), (_applyDecoratedDescriptor(_class, "template", [_dec], (_init = _Object$getOwnPropertyDescriptor(_class, "template"), _init = _init ? _init.value : undefined, {
  enumerable: true,
  configurable: true,
  writable: true,
  initializer: function initializer() {
    return _init;
  }
}), _class)), _class));
export { Cascader as default };
export { Cascader };