"use strict";

var _interopRequireDefault = require("D:/git/\u6211\u7684/opu-components-vue/node_modules/@babel/runtime/helpers/interopRequireDefault.js").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _toConsumableArray2 = _interopRequireDefault(require("D:/git/\u6211\u7684/opu-components-vue/node_modules/@babel/runtime/helpers/esm/toConsumableArray.js"));

var _objectSpread2 = _interopRequireDefault(require("D:/git/\u6211\u7684/opu-components-vue/node_modules/@babel/runtime/helpers/esm/objectSpread2.js"));

require("core-js/modules/es.array.map.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.number.constructor.js");

require("core-js/modules/es.array.concat.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.string.search.js");

require("core-js/modules/es.array.filter.js");

require("core-js/modules/es.regexp.to-string.js");

require("core-js/modules/es.object.keys.js");

require("core-js/modules/es.array.find.js");

var _autoComplete = require("ant-design-vue/lib/auto-complete");

var _utils = _interopRequireDefault(require("../../utils"));

var _conf = _interopRequireDefault(require("../conf"));

// 处理表单项的可选数据结构为 antd所需的
function handleItemPropsOptions(options, _vm) {
  var pValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
  var vF = _vm.vF,
      lF = _vm.lF,
      optionsFilter = _vm.optionsFilter,
      value = _vm.value;

  if (options && _utils.default.isArray(options)) {
    var cloneOptions = _utils.default.clone(options);

    return cloneOptions.map(function (item) {
      if (Object.prototype.toString.call(item) === "[object Object]") {
        if (item.value && vF != "value" && !item[_conf.default.originalValueKey]) {
          item[_conf.default.originalValueKey] = item.value;
        }

        if (vF) {
          item.value = item[vF] + "";
        }

        if (lF) {
          item.label = item[lF];
        }

        if (pValue) {
          item._pValue = pValue;
        }
      }

      return item;
    });
  }

  if (optionsFilter) {
    options = optionsFilter(options, value);
  }

  return options;
}

var _default = {
  name: "optionsComponent",
  components: {},
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _autoComplete.AutoCompleteProps), {}, {
    disabled: {
      type: Boolean,
      default: false
    },
    renderName: {
      type: String,
      default: "a-auto-complete"
    },
    valueField: String,
    labelField: String,
    value: [Number, String, Object, Array],
    options: Array,
    renderOptionLabel: Function
  }),
  model: {
    prop: "value",
    event: "update"
  },
  data: function data() {
    return {
      optionsData: []
    };
  },
  computed: {
    vF: function vF() {
      return this.valueField ? this.valueField : _conf.default.getSelectOptions.valueField;
    },
    lF: function lF() {
      return this.labelField ? this.labelField : _conf.default.getSelectOptions.labelField;
    },
    componentProps: function componentProps() {
      var _this = this;

      var $listeners = this.$listeners,
          $options = this.$options;
      var propsData = $options.propsData;
      var ons = {};

      _utils.default.each($listeners, function (cb, type) {
        ons[type] = function () {
          for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
          }

          _this.$emit.apply(_this, [type].concat(args));
        };
      });

      var props = {
        props: (0, _objectSpread2.default)({}, propsData),
        on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
          change: this.updateValue
        })
      };
      return props;
    }
  },
  watch: {
    options: function options(value) {
      this.setOptionsData(value);
    }
  },
  created: function created() {
    this.init();
  },
  methods: {
    init: function init() {
      var options = this.options;

      if (options && options.length) {
        this.optionsData = handleItemPropsOptions(options, this);
      }
    },
    updateValue: function updateValue(value) {
      this.$emit("update", value);
      this.$emit("change", value);
    },
    setOptionsData: function setOptionsData(data) {
      this.optionsData = handleItemPropsOptions(data, this);
      return data;
    },
    getOptionsData: function getOptionsData() {
      return this.optionsData;
    },
    focus: function focus() {
      var that = this;
      var inputComponent = this.$refs.inputComponent;
      var el = inputComponent.$el; // select获得输入焦点，弹出下拉面板

      var inputDom = el.getElementsByClassName("ant-select-search__field")[0];

      if (inputDom) {
        inputDom.focus && inputDom.focus();
        inputDom.click && inputDom.click();
        inputDom.removeEventListener("keyup", that.onKeyUpEnter);
        inputDom.addEventListener("keyup", that.onKeyUpEnter);
      } // if (box && box.length) {
      //   box[0].click();
      // }

    },
    blur: function blur() {
      var inputComponent = this.$refs.inputComponent;
      var el = inputComponent.$el;
      var inputDom = el.getElementsByClassName("ant-select-search__field")[0];

      if (inputDom) {
        inputDom.removeEventListener("keyup", this.onKeyUpEnter);
        inputDom.blur();
      }
    },
    onKeyUpEnter: function onKeyUpEnter(e) {
      if (e.key === "Enter") {
        e.stopPropagation();
        this.$emit("inputPressEnter", e);
      }
    },
    autoCompleteFilterRender: function autoCompleteFilterRender(props) {
      var h = this.$createElement;
      var vF = this.vF,
          lF = this.lF,
          optionsData = this.optionsData,
          value = this.value,
          renderOptionLabel = this.renderOptionLabel;
      var dataSource = [];

      if (props.search) {
        dataSource = props.search(value, optionsData);
      } else {
        var searchFields = props.searchFields || [];
        dataSource = optionsData.filter(function (p) {
          if (!value) return true;
          var is = false;

          if (Object.prototype.toString.call(p) === "[object Object]") {
            var searchFieldList = [lF].concat((0, _toConsumableArray2.default)(searchFields));

            for (var i = 0; i < searchFieldList.length; i++) {
              var key = searchFieldList[i];
              var optionValue = p[key] || p;

              if (value) {
                if (optionValue.toString().toLowerCase().indexOf(value.toLowerCase()) >= 0) {
                  is = true;
                  break;
                }
              }
            }
          } else if (p.toString().toLowerCase().indexOf(value.toLowerCase()) >= 0) {
            is = true;
          }

          return is;
        });
      }

      var selectOptions = dataSource.map(function (p) {
        return h("a-select-option", {
          "key": p[vF] || p
        }, [renderOptionLabel && renderOptionLabel(p) || p[lF] || p]);
      });
      return {
        selectOptions: selectOptions,
        dataSource: dataSource
      };
    }
  },
  render: function render(h) {
    var _this2 = this;

    var componentProps = this.componentProps,
        value = this.value,
        $slots = this.$slots;

    var _this$autoCompleteFil = this.autoCompleteFilterRender(componentProps),
        selectOptions = _this$autoCompleteFil.selectOptions,
        dataSource = _this$autoCompleteFil.dataSource;

    var slots = Object.keys($slots).map(function (name) {
      return h("template", {
        "slot": name
      }, [$slots[name]]);
    });

    if (!$slots.dataSource) {
      slots.push(h("template", {
        "slot": "dataSource"
      }, [selectOptions]));
    }

    return h("a-auto-complete", {
      ref: "inputComponent",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, componentProps.props), {}, {
        value: value
      }),
      on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, componentProps.on), {}, {
        focus: function focus() {
          _this2.focus();

          if (componentProps.on && componentProps.on.focus) {
            componentProps.on.focus();
          }
        },
        select: function select(value) {
          if (componentProps.on && componentProps.on.select) {
            var vF = _this2.vF;
            var row = dataSource.find(function (p) {
              if (Object.prototype.toString.call(p) === "[object Object]") {
                return p[vF] == value;
              } else {
                p == value;
              }
            });
            componentProps.on.select(value, row);
          }
        }
      })
    }, [slots]);
  }
};
exports.default = _default;