import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import ksSearch from '../search';
import './style'; // 防抖

var debounce = function debounce(fn, wait) {
  var timeout = null;
  return function () {
    var t = this;
    var args = arguments;

    if (timeout) {
      clearTimeout(timeout);
    }

    timeout = setTimeout(function () {
      return fn.apply(t, args);
    }, wait);
  };
};

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('ks-search', _vm._b({
    ref: "search",
    staticClass: "ks-input-search",
    on: {
      "blur": _vm.blur,
      "cancel": _vm.cancel,
      "clear": _vm.clear,
      "focus": _vm.focus,
      "input": _vm.input,
      "search": _vm.search
    },
    model: {
      value: _vm.inputValue,
      callback: function callback($v) {
        _vm.inputValue = typeof $v === 'string' ? $v.trim() : $v;
      },
      expression: "inputValue"
    }
  }, 'ks-search', _vm.$attrs, false), [_vm._l(Object.keys(this.$slots), function (name) {
    return _vm._t(name, null, {
      "slot": name
    });
  })], 2);
};

var __vue_staticRenderFns__ = [];
export default {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: 'ks-input-search',
  components: {
    ksSearch: ksSearch
  },
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data: function data() {
    return {
      inputValue: this.value
    };
  },
  mounted: function mounted() {
    this.inputDebounce = debounce(this.search, 300);
    this.inputKeyDownDebounce = debounce(this.keyDownHandler, 200);
    this.inputDom = this.$el.querySelector('input[type="search"]');
    this.inputDom.addEventListener('compositionupdate', this.inputDebounce);
    this.inputDom.addEventListener('keydown', this.inputKeyDownDebounce);
  },
  beforeDestroy: function beforeDestroy() {
    this.inputDom.removeEventListener('compositionupdate', this.inputDebounce);
    this.inputDom.removeEventListener('keydown', this.inputKeyDownDebounce);
  },
  methods: {
    input: function input() {
      this.inputDebounce();
    },
    blur: function blur() {
      this.$emit('blur');
    },
    clear: function clear() {
      this.$emit('clear');
    },
    keyDownHandler: function keyDownHandler(e) {
      if (e.key === 'Backspace') {
        this.$emit('deleteKeyEvent', e);
      }
    },
    cancel: function cancel() {
      this.$emit('cancel');
    },
    focus: function focus() {
      this.$emit('focus');
    },
    setValue: function setValue(val) {
      this.inputValue = val;
    },
    search: function search(e) {
      var _this = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var val, text;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                val = e && e.data || '';
                text = _this.inputValue + val;

                _this.$emit('change', text);

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