"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.selectProProps = exports.default = void 0;
var _vue = require("vue");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _construct2 = _interopRequireDefault(require("@babel/runtime/helpers/construct"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _request = _interopRequireDefault(require("../_util/request"));
var _select = _interopRequireWildcard(require("../select"));
var _treeSelect = _interopRequireWildcard(require("../tree-select"));
var _omit = _interopRequireDefault(require("../_util/omit"));
var _type = require("../_util/type");
var _pinyinPro = require("pinyin-pro");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var StructureTypeEnum;
(function (StructureTypeEnum) {
  StructureTypeEnum["List"] = "2008-01";
  StructureTypeEnum["Tree"] = "2008-02";
})(StructureTypeEnum || (StructureTypeEnum = {}));
var getCofig = function getCofig(params) {
  return (0, _request.default)('/develop/module/sink/render.open', {
    params: params
  });
};
var _selectProps = (0, _objectSpread2.default)({}, (0, _omit.default)((0, _select.selectProps)(), ['options']));
var _treeSelectProps = (0, _objectSpread2.default)({}, (0, _omit.default)((0, _treeSelect.treeSelectProps)(), ['treeData']));
var selectProProps = function selectProProps() {
  return (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, _treeSelectProps), _selectProps), {}, {
    treeDefaultExpandAll: {
      type: Boolean,
      default: true
    },
    value: [Array, Object, String, Number],
    showSearch: {
      type: Boolean,
      default: true
    },
    defaultValue: [Array, Object, String, Number],
    code: String,
    hisUuid: String,
    domain: String,
    config: Object,
    surname: Boolean,
    onConfigChange: Function,
    'onUpdate:configValue': Function
  });
};
exports.selectProProps = selectProProps;
var selectSlots = ['notFoundContent', 'suffixIcon', 'itemIcon', 'removeIcon', 'clearIcon', 'dropdownRender', 'option', 'placeholder', 'tagRender', 'maxTagPlaceholder', 'optionLabel' // donot use, maybe remove it
];

var treeSelectSlots = ['title', 'titleRender', 'placeholder', 'maxTagPlaceholder', 'treeIcon', 'switcherIcon', 'notFoundContent'];
var SelectPro = (0, _vue.defineComponent)({
  name: 'PfSelectPro',
  inheritAttrs: false,
  props: selectProProps(),
  slots: (0, _toConsumableArray2.default)((0, _construct2.default)(Set, selectSlots.concat(treeSelectSlots))),
  setup: function setup(props, _ref) {
    var attrs = _ref.attrs,
      slots = _ref.slots,
      emit = _ref.emit;
    var configLoading = (0, _vue.ref)(false);
    var config = (0, _vue.ref)();
    var selectMergedProps = (0, _vue.computed)(function () {
      var res = {};
      Object.keys(_selectProps).forEach(function (key) {
        if (props.hasOwnProperty(key) && props[key] !== undefined) {
          res[key] = props[key];
        }
      });
      return res;
    });
    var treeSelectMergedProps = (0, _vue.computed)(function () {
      var res = {};
      Object.keys(_treeSelectProps).forEach(function (key) {
        if (props.hasOwnProperty(key) && props[key] !== undefined) {
          res[key] = props[key];
        }
      });
      return res;
    });
    var handleConfig = function handleConfig(value) {
      var _props$onConfigChange;
      config.value = value;
      (_props$onConfigChange = props.onConfigChange) === null || _props$onConfigChange === void 0 ? void 0 : _props$onConfigChange.call(props, value);
      emit('update:configValue', value);
    };
    (0, _vue.watch)([function () {
      return props.code;
    }, function () {
      return props.hisUuid;
    }, function () {
      return props.domain;
    }, function () {
      return props.config;
    }], function (_ref2, _ref3) {
      var _ref4 = (0, _slicedToArray2.default)(_ref2, 4),
        curCode = _ref4[0],
        curHisUuid = _ref4[1],
        curDomain = _ref4[2],
        curConfig = _ref4[3];
      var _ref5 = (0, _slicedToArray2.default)(_ref3, 4),
        preCode = _ref5[0],
        preHisUuid = _ref5[1],
        preDomain = _ref5[2],
        preConfig = _ref5[3];
      if (curCode && curCode !== preCode || curHisUuid && curHisUuid !== preHisUuid || (curCode || curHisUuid) && curDomain !== preDomain) {
        configLoading.value = true;
        getCofig({
          moduleCode: curCode || '',
          hisUuid: curHisUuid && !curCode ? curHisUuid : ''
        }).then(function (res) {
          handleConfig(res);
        }).finally(function () {
          configLoading.value = false;
        });
      } else if (curConfig && curConfig !== preConfig) {
        Promise.resolve().then(function () {
          handleConfig(curConfig);
        });
      }
    }, {
      immediate: true
    });
    var filterInput = function filterInput(inputValue, label) {
      if (label !== null && label !== void 0 && label.includes(inputValue)) {
        return true;
      } else {
        var zhInitials = (0, _pinyinPro.pinyin)(label, {
          pattern: 'first',
          toneType: 'none',
          separator: '',
          // removeNonZh: true,
          mode: props.surname ? 'surname' : 'normal'
        });
        return zhInitials === null || zhInitials === void 0 ? void 0 : zhInitials.includes(inputValue);
      }
    };
    var filterSelect = function filterSelect(inputValue, option) {
      if (inputValue) {
        var _ref6 = option || {},
          label = _ref6.label;
        return filterInput(inputValue, label);
      } else {
        return true;
      }
    };
    var renderSelect = function renderSelect() {
      var _config$value;
      return (0, _vue.createVNode)(_select.default, (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
        "placeholder": "请选择",
        "filterOption": filterSelect
      }, attrs), selectMergedProps.value), {}, {
        "options": (_config$value = config.value) === null || _config$value === void 0 ? void 0 : _config$value.datas
      }), (0, _objectSpread2.default)({}, slots));
    };
    var filterTreeSelect = function filterTreeSelect(inputValue, node) {
      if (inputValue) {
        var _ref7 = node || {},
          label = _ref7.label;
        return filterInput(inputValue, label);
      } else {
        return true;
      }
    };
    var renderTreeSelect = function renderTreeSelect() {
      var _config$value2;
      return (0, _vue.createVNode)(_treeSelect.default, (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
        "placeholder": "请选择",
        "filterTreeNode": filterTreeSelect
      }, attrs), treeSelectMergedProps.value), {}, {
        "treeData": (_config$value2 = config.value) === null || _config$value2 === void 0 ? void 0 : _config$value2.datas
      }), (0, _objectSpread2.default)({}, slots));
    };
    return function () {
      var _config$value3, _config$value4;
      if (((_config$value3 = config.value) === null || _config$value3 === void 0 ? void 0 : _config$value3.frameType) === StructureTypeEnum.List) {
        return renderSelect();
      } else if (((_config$value4 = config.value) === null || _config$value4 === void 0 ? void 0 : _config$value4.frameType) === StructureTypeEnum.Tree) {
        return renderTreeSelect();
      } else {
        return null;
      }
    };
  }
});
var _default = (0, _type.withInstall)(SelectPro);
exports.default = _default;