"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;

require("regenerator-runtime/runtime.js");

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

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.number.constructor.js");

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

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

require("core-js/modules/web.dom-collections.for-each.js");

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

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

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

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

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

var _index = require("ant-design-vue/lib/select/index");

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,
      childrenField = _vm.childrenField,
      optionsFilter = _vm.optionsFilter,
      value = _vm.value;

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

    return cloneOptions.map(function (item) {
      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;
      }

      if (item[childrenField] && item[childrenField].length) {
        item[childrenField] = handleItemPropsOptions(item[childrenField], _vm, item.value);
      }

      return item;
    });
  }

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

  return options;
}

var _default2 = {
  name: "OpuSelect",
  components: {},
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _index.SelectProps), {}, {
    value: [Number, String, Object, Array],
    valueField: String,
    labelField: String,
    dataField: String,
    options: Array,
    searchFields: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    childrenField: String,
    renderOptionLabel: Function,
    optionsFilter: Function,
    searchTrigger: {
      type: String,
      //search || focus
      default: "focus"
    },
    searchApi: Function
  }),
  model: {
    prop: "value",
    event: "update"
  },
  data: function data() {
    return {
      optionsData: [],
      hasGroup: false,
      fetching: null
    };
  },
  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,
          optionsData = this.optionsData,
          value = this.value,
          searchFields = this.searchFields,
          vF = this.vF,
          lF = this.lF;
      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 currentValue = value;

      if (value) {
        if (propsData.labelInValue) {
          if (_utils.default.isArray(value)) {
            value.forEach(function (p) {
              if (!p.label) {
                p.label = p[lF];
              }

              if (!p.key) {
                p.key = p[vF];
              }
            });
          } else {
            if (!value.label) {
              value.label = value[lF];
            }

            if (!value.key) {
              value.key = value[vF];
            }
          }

          currentValue = value;
        } else if (_utils.default.isNumber(value)) {
          currentValue = value + "";
        } else if (_utils.default.isArray(value)) {
          currentValue = value.map(function (p) {
            return p + "";
          });
        }
      }

      var props = {
        props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, propsData), {}, {
          value: currentValue,
          options: null // getPopupContainer: triggerNode => triggerNode.parentNode

        }),
        on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
          change: this.updateValue
        })
      };

      if (props.props.showSearch && !props.props.filterOption) {
        props.props.filterOption = function (input, option) {
          var value = option.componentOptions.propsData.value;
          var objIndex = optionsData.findIndex(function (item) {
            return item[vF].toString() === value;
          });
          var obj = optionsData[objIndex];
          var is = false;
          var searchFieldList = [lF].concat((0, _toConsumableArray2.default)(searchFields)); // vF,

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

            if (obj[key]) {
              if (obj[key].toString().toLowerCase().indexOf(input.toLowerCase()) >= 0) {
                is = true;
                break;
              }
            }
          }

          return is;
        };
      }

      props.props.options = optionsData;
      return props;
    }
  },
  watch: {
    options: function options() {
      this.init();
    }
  },
  created: function created() {
    this.init();
  },
  methods: {
    init: function init() {
      var options = this.options;

      if (options) {
        this.setOptionsData(options);
      }
    },
    updateValue: function updateValue(e) {
      var vF = this.vF,
          lF = this.lF,
          optionsData = this.optionsData,
          childrenField = this.childrenField,
          labelInValue = this.labelInValue;

      var optionsList = _utils.default.treeTransArray(optionsData, childrenField);

      if (e instanceof Array) {
        var rows = [];

        for (var i = 0; i < e.length; i++) {
          var item = e[i];

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

          for (var j = 0; j < optionsList.length; j++) {
            if (optionsList[j][vF] == item) {
              rows.push(optionsList[j]);
              break;
            }
          }
        }

        this.$emit("update", e, rows);
        this.$emit("change", e, rows);
      } else {
        var value = e;

        if (e && labelInValue) {
          e[vF] = e.key;
          e[lF] = e.label;
          value = e.key;
        }

        var row = optionsList.find(function (p) {
          return p[vF] == value;
        });
        var pRow;

        if (row && row._pValue) {
          pRow = optionsList.find(function (p) {
            return p[vF] == row._pValue;
          });
        }

        this.$emit("update", e, row, pRow);
        this.$emit("change", e, row, pRow);
      }
    },
    setOptionsData: function setOptionsData(data) {
      if (this.optionsData !== data) {
        var options = handleItemPropsOptions(data, this);
        this.optionsData = options;
        this.fetching = null;
        return options;
      } else {
        return data;
      }
    },
    getOptionsData: function getOptionsData() {
      return this.optionsData;
    },
    focus: function focus() {
      var input = this.$refs.inputComponent;

      if (input) {
        input.focus && input.focus();
        var el = input.$el;

        if (el) {
          // select获得输入焦点，弹出下拉面板
          var box = el.getElementsByClassName("ant-select-selection__rendered");

          if (box && box.length) {
            box[0].click();
          }
        }
      }
    },
    onSearch: function onSearch(e, type) {
      var _this2 = this;

      return new Promise(function (resolve, reject) {
        if (_this2.fetching === null && _this2.searchTrigger == type && _this2.searchApi && _this2.optionsData.length == 0) {
          _this2.fetching = true;

          _this2.searchApi(e).then(function (res) {
            _this2.fetching = false;

            var data = _utils.default.getObjData(_this2.dataField, res);

            _this2.optionsData = handleItemPropsOptions(data, _this2);
            resolve(data);
          }).catch(function () {
            _this2.fetching = false;
            reject();
          });
        } else {
          resolve(_this2.optionsData);
        }
      });
    },
    renderSelectChildren: function renderSelectChildren(h) {
      if (this.fetching) {
        return [h("a-spin", {
          "slot": "notFoundContent",
          "attrs": {
            "size": "small"
          }
        })];
      } else {
        var renderOptGroup = this.renderOptGroup,
            renderOptionItems = this.renderOptionItems;
        var optGroup = renderOptGroup(h);
        var optionItems = "";

        if (!optGroup) {
          optionItems = renderOptionItems(h);
        }

        return [optGroup, optionItems];
      }
    },
    renderOptGroup: function renderOptGroup(h) {
      var childrenField = this.childrenField,
          optionsData = this.optionsData,
          renderOptionText = this.renderOptionText;

      if (this.childrenField) {
        return optionsData.map(function (group) {
          var options = "";

          if (group[childrenField] && group[childrenField].length) {
            options = group[childrenField].map(function (p) {
              var label = renderOptionText(p);
              return h("a-select-option", {
                props: {
                  value: p.value,
                  disabled: p.disabled
                }
              }, [label]);
            });
            return h("a-select-opt-group", {
              props: {
                label: group.label,
                key: group.value,
                disabled: group.disabled
              }
            }, [options]);
          }

          return h("a-select-option", {
            props: {
              value: group.value,
              disabled: group.disabled
            }
          }, [group.label]);
        });
      }

      return "";
    },
    renderOptionItems: function renderOptionItems(h) {
      var optionsData = this.optionsData,
          renderOptionText = this.renderOptionText;
      return optionsData.map(function (item) {
        var label = renderOptionText(item);
        return h("a-select-option", {
          props: {
            value: item.value,
            disabled: item.disabled
          }
        }, [label]);
      });
    },
    renderOptionText: function renderOptionText(item) {
      if (this.renderOptionLabel) {
        return this.renderOptionLabel(item);
      } else {
        return item[this.lF];
      }
    }
  },
  render: function render(h) {
    var _this3 = this;

    var componentProps = this.componentProps;
    return h("a-select", {
      ref: "inputComponent",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, componentProps.props), {}, {
        options: null,
        search: function () {
          var _search = (0, _asyncToGenerator2.default)( /*#__PURE__*/regeneratorRuntime.mark(function _callee(e) {
            return regeneratorRuntime.wrap(function _callee$(_context) {
              while (1) {
                switch (_context.prev = _context.next) {
                  case 0:
                    _context.next = 2;
                    return _this3.onSearch(e, "search");

                  case 2:
                    componentProps.props.search && componentProps.props.search(e, _this3.optionsData);

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

          function search(_x) {
            return _search.apply(this, arguments);
          }

          return search;
        }()
      }),
      on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, componentProps.on), {}, {
        focus: function () {
          var _focus = (0, _asyncToGenerator2.default)( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(e) {
            return regeneratorRuntime.wrap(function _callee2$(_context2) {
              while (1) {
                switch (_context2.prev = _context2.next) {
                  case 0:
                    _context2.next = 2;
                    return _this3.onSearch(e, "focus");

                  case 2:
                    componentProps.on.focus && componentProps.on.focus(e, _this3.optionsData);

                  case 3:
                  case "end":
                    return _context2.stop();
                }
              }
            }, _callee2);
          }));

          function focus(_x2) {
            return _focus.apply(this, arguments);
          }

          return focus;
        }()
      })
    }, this.renderSelectChildren(h));
  }
};
exports.default = _default2;