import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _construct from "@babel/runtime/helpers/esm/construct";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createVNode as _createVNode, resolveDirective as _resolveDirective } from "vue";
import request from '../_util/request';
import { computed, defineComponent, ref, watch } from 'vue';
import { default as PfSelect, selectProps } from '../select';
import { default as PfTreeSelect, treeSelectProps } from '../tree-select';
import omit from '../_util/omit';
import { withInstall } from '../_util/type';
import { pinyin } from 'pinyin-pro';
var StructureTypeEnum;
(function (StructureTypeEnum) {
  StructureTypeEnum["List"] = "2008-01";
  StructureTypeEnum["Tree"] = "2008-02";
})(StructureTypeEnum || (StructureTypeEnum = {}));
var getCofig = function getCofig(params) {
  return request('/develop/module/sink/render.open', {
    params: params
  });
};
var _selectProps = _objectSpread({}, omit(selectProps(), ['options']));
var _treeSelectProps = _objectSpread({}, omit(treeSelectProps(), ['treeData']));
export var selectProProps = function selectProProps() {
  return _objectSpread(_objectSpread(_objectSpread({}, _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
  });
};
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 = defineComponent({
  name: 'PfSelectPro',
  inheritAttrs: false,
  props: selectProProps(),
  slots: _toConsumableArray(_construct(Set, selectSlots.concat(treeSelectSlots))),
  setup: function setup(props, _ref) {
    var attrs = _ref.attrs,
      slots = _ref.slots,
      emit = _ref.emit;
    var configLoading = ref(false);
    var config = ref();
    var selectMergedProps = 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 = 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);
    };
    watch([function () {
      return props.code;
    }, function () {
      return props.hisUuid;
    }, function () {
      return props.domain;
    }, function () {
      return props.config;
    }], function (_ref2, _ref3) {
      var _ref4 = _slicedToArray(_ref2, 4),
        curCode = _ref4[0],
        curHisUuid = _ref4[1],
        curDomain = _ref4[2],
        curConfig = _ref4[3];
      var _ref5 = _slicedToArray(_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 = 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 _createVNode(PfSelect, _objectSpread(_objectSpread(_objectSpread({
        "placeholder": "请选择",
        "filterOption": filterSelect
      }, attrs), selectMergedProps.value), {}, {
        "options": (_config$value = config.value) === null || _config$value === void 0 ? void 0 : _config$value.datas
      }), _objectSpread({}, 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 _createVNode(PfTreeSelect, _objectSpread(_objectSpread(_objectSpread({
        "placeholder": "请选择",
        "filterTreeNode": filterTreeSelect
      }, attrs), treeSelectMergedProps.value), {}, {
        "treeData": (_config$value2 = config.value) === null || _config$value2 === void 0 ? void 0 : _config$value2.datas
      }), _objectSpread({}, 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;
      }
    };
  }
});
export default withInstall(SelectPro);