/*! bxs-vue-ui v3.3.0 */
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

require('../utils/entry-virtual.js');
var normalizeComponent = require('../utils/normalize-component.js');
require('../utils/array-iteration.js');
require('../utils/sloppy-array-method.js');
var forEach = require('../utils/for-each.js');
require('../utils/string-trim.js');
var _extends = require('../utils/extends.js');
var icon = require('../icon/index.js');
var trim = require('../utils/trim.js');
var splice = require('../utils/splice.js');
var keys = require('../utils/keys.js');
var stringify = require('../utils/stringify.js');
var setTimeout = require('../utils/set-timeout.js');

// `SameValue` abstract operation
// https://tc39.github.io/ecma262/#sec-samevalue
var sameValue = Object.is || function is(x, y) {
  // eslint-disable-next-line no-self-compare
  return x === y ? x !== 0 || 1 / x === 1 / y : x != x && y != y;
};

// @@search logic
normalizeComponent.fixRegExpWellKnownSymbolLogic('search', 1, function (SEARCH, nativeSearch, maybeCallNative) {
  return [
    // `String.prototype.search` method
    // https://tc39.github.io/ecma262/#sec-string.prototype.search
    function search(regexp) {
      var O = normalizeComponent.requireObjectCoercible(this);
      var searcher = regexp == undefined ? undefined : regexp[SEARCH];
      return searcher !== undefined ? searcher.call(regexp, O) : new RegExp(regexp)[SEARCH](String(O));
    },
    // `RegExp.prototype[@@search]` method
    // https://tc39.github.io/ecma262/#sec-regexp.prototype-@@search
    function (regexp) {
      var res = maybeCallNative(nativeSearch, regexp, this);
      if (res.done) return res.value;

      var rx = normalizeComponent.anObject(regexp);
      var S = String(this);

      var previousLastIndex = rx.lastIndex;
      if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0;
      var result = normalizeComponent.regExpExec(rx, S);
      if (!sameValue(rx.lastIndex, previousLastIndex)) rx.lastIndex = previousLastIndex;
      return result === null ? -1 : result.index;
    }
  ];
});

var _components;
var script = normalizeComponent.create({
  name: 'search',
  props: {
    placeholder: {
      type: String,
      default: '请输入搜索关键字'
    },
    showAction: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    background: {
      type: String,
      default: '#f2f2f2'
    },
    history: {
      type: String
    },
    value: {
      type: String,
      default: ''
    }
  },
  data: function data() {
    return {
      attachModuleShow: false,
      historyList: null,
      vl: ''
    };
  },
  components: (_components = {}, _components[icon.default.name] = icon.default, _components),
  watch: {
    value: function value(a) {
      if (typeof a !== 'undefined') {
        this.vl = a;
      }
    }
  },
  computed: {
    listeners: function listeners() {
      var _context;

      var self = this;

      var listeners = _extends._extends({}, this.$listeners, {
        keypress: self.keypress,
        input: self.input,
        clear: self.clear,
        focus: self.focus
      });

      var searchListeners = {};

      forEach._forEachInstanceProperty(_context = keys._Object$keys(listeners)).call(_context, function (attr) {
        searchListeners[attr] = function (ev) {
          if (self[attr] && typeof self[attr] === 'function') {
            self[attr](ev);
          } else {
            self.doFn(attr);
          }
        };
      });

      return self.disabled ? {} : searchListeners;
    },
    attachModuleDeploy: function attachModuleDeploy() {
      return typeof this.history !== 'undefined';
    },
    historyTag: function historyTag() {
      return this.history ? this.history : 'common';
    }
  },
  methods: {
    doFn: function doFn(str, v) {
      if (this.disabled) {
        return;
      }

      if (v) {
        this.$emit(str, v);
      } else {
        this.$emit(str);
      }
    },
    clear: function clear() {
      this.vl = this.disabled ? this.vl : '';
      this.doFn('clear');
    },
    search: function search() {
      this.doFn('search');
      this.attachModuleShow = false;
      if (this.attachModuleDeploy) this.addHistory();
    },
    goSearch: function goSearch(str) {
      this.vl = str;
      this.search();
    },
    cancel: function cancel() {
      this.vl = this.disabled ? this.vl : '';
      this.doFn('cancel');
    },
    focus: function focus() {
      var self = this;

      if (this.attachModuleDeploy) {
        this.attachModuleShow = true;
        this.getHistoryLocal(true);

        setTimeout._setTimeout(function () {
          self.$refs.attachModule.focus();
        }, 100);
      }
    },
    input: function input() {
      this.doFn('input', this.vl);
    },
    keypress: function keypress(event) {
      if (event && (event.keyCode === 13 || event.code === 'Enter') && !this.disabled) {
        event.preventDefault();
        this.search();
      }

      this.doFn('keypress', event);
    },
    getHistoryLocal: function getHistoryLocal(b) {
      var self = this;
      var type = self.historyTag;
      var historyLocal;

      try {
        historyLocal = JSON.parse(window.localStorage.bxsSearchHis);
        historyLocal = typeof historyLocal !== 'object' ? {} : historyLocal;
      } catch (e) {
        historyLocal = {};
      }

      if (b) {
        if (historyLocal[type] && historyLocal[type] instanceof Array) {
          self.historyList = historyLocal[type];
        } else {
          self.historyList = [];
        }
      }

      return historyLocal;
    },
    addHistory: function addHistory(b) {
      var _context2;

      if (b) {
        this.attachModuleShow = false;
      }

      if (!trim._trimInstanceProperty(_context2 = this.vl).call(_context2)) {
        return;
      }

      var historyLocal = this.getHistoryLocal();
      var type = this.historyTag;
      var arr = historyLocal[type] ? historyLocal[type] : [];
      arr = arr instanceof Array ? arr : [];
      arr.unshift(this.vl);
      arr = splice._spliceInstanceProperty(arr).call(arr, 0, 4);
      this.historyList = arr;
      historyLocal[type] = arr;
      window.localStorage.bxsSearchHis = stringify._JSON$stringify(historyLocal);
    },
    clearHistory: function clearHistory(n) {
      var history = this.historyList;

      if (typeof n !== 'undefined' && history[n]) {
        splice._spliceInstanceProperty(history).call(history, n, 1);
      } else if (typeof n === 'undefined') {
        this.historyList = null;
      }

      var historyLocal = this.getHistoryLocal();
      var type = this.historyTag;
      historyLocal[type] = this.historyList && this.historyList.length ? this.historyList : null;
      window.localStorage.bxsSearchHis = stringify._JSON$stringify(historyLocal);
    }
  },
  created: function created() {
    this.vl = this.value;
  }
});

var __vue_script__ = script;
/* template */

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

  var _h = _vm.$createElement;

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

  return _c('section', [_c('section', {
    staticClass: "bx-search"
  }, [_c('div', {
    class: ['bx-search-box', _vm.showAction ? 'bx-search-box--with-action' : ''],
    style: "background:" + _vm.background
  }, [_c('p', {
    staticClass: "bx-search-icon",
    on: {
      "click": _vm.search
    }
  }, [_c('bxs-icon', {
    attrs: {
      "name": "search",
      "size": "18px"
    }
  })], 1), _c('p', {
    staticClass: "bx-search-form"
  }, [_c('input', _vm._g({
    directives: [{
      name: "model",
      rawName: "v-model",
      value: _vm.vl,
      expression: "vl"
    }],
    staticClass: "bx-search-input",
    attrs: {
      "disabled": _vm.disabled,
      "type": "text",
      "placeholder": _vm.placeholder
    },
    domProps: {
      "value": _vm.vl
    },
    on: {
      "input": function input($event) {
        if ($event.target.composing) {
          return;
        }

        _vm.vl = $event.target.value;
      }
    }
  }, _vm.listeners))]), !!_vm.vl ? _c('p', {
    staticClass: "bx-search-clear",
    on: {
      "click": _vm.clear
    }
  }, [_c('bxs-icon', {
    attrs: {
      "name": "close-solid",
      "size": "18px"
    }
  })], 1) : _vm._e()]), _vm.showAction ? _c('div', {
    staticClass: "bx-search-action"
  }, [_vm._t("action", [_c('div', {
    on: {
      "click": _vm.cancel
    }
  }, [_vm._v("\n          取消\n        ")])], {
    addHistory: _vm.addHistory
  })], 2) : _vm._e()]), _vm.attachModuleShow ? _c('div', {
    staticClass: "bx-search-suggestion-overlay"
  }, [_c('section', {
    staticClass: "bx-search"
  }, [_c('div', {
    class: ['bx-search-box', _vm.showAction ? 'bx-search-box--with-action' : ''],
    style: "background:" + _vm.background
  }, [_c('p', {
    staticClass: "bx-search-icon",
    on: {
      "click": _vm.search
    }
  }, [_c('bxs-icon', {
    attrs: {
      "name": "search",
      "size": "18px"
    }
  })], 1), _c('p', {
    staticClass: "bx-search-form"
  }, [_c('input', _vm._g({
    directives: [{
      name: "model",
      rawName: "v-model",
      value: _vm.vl,
      expression: "vl"
    }],
    ref: "attachModule",
    staticClass: "bx-search-input",
    attrs: {
      "disabled": _vm.disabled,
      "type": "text",
      "placeholder": _vm.placeholder
    },
    domProps: {
      "value": _vm.vl
    },
    on: {
      "input": function input($event) {
        if ($event.target.composing) {
          return;
        }

        _vm.vl = $event.target.value;
      }
    }
  }, _vm.listeners))]), !!_vm.vl ? _c('p', {
    staticClass: "bx-search-clear",
    on: {
      "click": _vm.clear
    }
  }, [_c('bxs-icon', {
    attrs: {
      "name": "close-solid",
      "size": "18px"
    }
  })], 1) : _vm._e()]), _vm.showAction ? _c('div', {
    staticClass: "bx-search-action"
  }, [_vm._t("action", [_c('div', {
    on: {
      "click": _vm.cancel
    }
  }, [_vm._v("\n            取消\n          ")])], {
    addHistory: _vm.addHistory
  })], 2) : _vm._e()]), _vm.historyList && _vm.historyList.length ? _c('div', {
    staticClass: "bx-search-block"
  }, [_c('p', {
    staticClass: "bx-search-block-title"
  }, [_vm._v("\n        搜索历史\n      ")]), _c('ul', {
    staticClass: "bx-search-history"
  }, _vm._l(_vm.historyList, function (item, index) {
    return _c('li', {
      key: index,
      staticClass: "bx-search-history-one"
    }, [_c('bxs-icon', {
      attrs: {
        "name": "clock"
      }
    }), _c('em', {
      staticClass: "bx-search-history-text",
      on: {
        "click": function click($event) {
          _vm.goSearch(item);
        }
      }
    }, [_vm._v("\n            " + _vm._s(item) + "\n          ")]), _c('p', {
      staticClass: "bx-search-history-close",
      on: {
        "click": function click($event) {
          _vm.clearHistory(index);
        }
      }
    }, [_c('bxs-icon', {
      attrs: {
        "name": "close"
      }
    })], 1)], 1);
  })), _c('p', {
    staticClass: "bx-search-history-clear",
    on: {
      "click": function click($event) {
        _vm.clearHistory();
      }
    }
  }, [_vm._v("\n        清除搜索历史\n      ")])]) : _vm._e()]) : _vm._e()]);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var Search = normalizeComponent.__vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = Search;
