/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../../../_chunks/dep-44e6bdf6.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _typeof = require('@babel/runtime/helpers/typeof');
var Vue = require('vue');
var isFunction = require('lodash/isFunction');
var tdesignIconsVue = require('tdesign-icons-vue');
var table_primaryTableProps = require('../../primary-table-props.js');
var table_baseTableProps = require('../../base-table-props.js');
var config = require('../../../config.js');
var checkbox_index = require('../../../checkbox/index.js');
var radio_index = require('../../../radio/index.js');
var input_index = require('../../../input/index.js');
var popup_index = require('../../../popup/index.js');
var button_index = require('../../../button/index.js');
var table_util_common = require('../../util/common.js');
var utils_event = require('../../../utils/event.js');
var utils_renderTnode = require('../../../utils/render-tnode.js');
require('../../../checkbox/group.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/intersection');
require('../../../checkbox/checkbox.js');
require('../../../utils/classnames.js');
require('../../../checkbox/props.js');
require('../../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../../../checkbox/checkbox-group-props.js');
require('../../../utils/map-props.js');
require('../../../utils/withInstall.js');
require('lodash/capitalize');
require('../../../radio/radio.js');
require('../../../radio/props.js');
require('../../../radio/group.js');
require('lodash/isString');
require('lodash/isNumber');
require('../../../radio/radio-group-props.js');
require('../../../radio/radio-button.js');
require('../../../input/addon.js');
require('../../../input/input.js');
require('../../../config-provider/config-receiver.js');
require('../../../config-provider/zh_CN_config.js');
require('../../../utils/mixins.js');
require('../../../input/props.js');
require('../../../input/input-group.js');
require('../../../popup/popup.js');
require('@popperjs/core');
require('../../../_chunks/dep-939cdc39.js');
require('../../../utils/dom.js');
require('raf');
require('../../../utils/easing.js');
require('../../../popup/props.js');
require('../../../utils/set-style.js');
require('../../../button/button.js');
require('../../../loading/index.js');
require('../../../loading/loading.js');
require('../../../loading/icon/gradient.js');
require('../../../utils/transfer-dom.js');
require('../../../loading/props.js');
require('../../../loading/plugin.js');
require('../../../button/props.js');
require('../../../utils/ripple.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function filterEmptyData(data) {
  var newFilterValue = {};
  Object.keys(data).forEach(function (key) {
    var item = data[key];
    var isArrayTrue = item instanceof Array && item.length;
    var isObject = _typeof__default["default"](item) === "object" && !(item instanceof Array);
    var isObjectTrue = isObject && Object.keys(item).length;

    if (isArrayTrue || isObjectTrue || !["null", "", "undefined"].includes(String(item))) {
      newFilterValue[key] = item;
    }
  });
  return newFilterValue;
}

var filter = Vue__default["default"].extend({
  name: "".concat(config.prefix, "-primary-table-filter"),
  data: function data() {
    return {
      tableWidth: 0,
      innerFilterValue: this.filterValue,
      filterPopupVisible: {}
    };
  },
  props: {
    columns: table_primaryTableProps["default"].columns,
    pagination: table_baseTableProps["default"].pagination,
    filterValue: table_primaryTableProps["default"].filterValue,
    filterIcon: table_primaryTableProps["default"].filterIcon,
    data: table_baseTableProps["default"].data,
    filterRow: Function
  },
  computed: {
    fixedLeftColumn: function fixedLeftColumn() {
      var _this$columns;

      return !!((_this$columns = this.columns) !== null && _this$columns !== void 0 && _this$columns.filter(function (col) {
        return col.fixed === "left";
      }).length);
    },
    hasFilterCondition: function hasFilterCondition() {
      var _this$columns2;

      return !!((_this$columns2 = this.columns) !== null && _this$columns2 !== void 0 && _this$columns2.filter(function (col) {
        return col.filter;
      }).length);
    }
  },
  watch: {
    filterValue: function filterValue(val) {
      this.innerFilterValue = _objectSpread({}, val);
    }
  },
  mounted: function mounted() {
    var _this = this;

    var timer = setTimeout(function () {
      _this.updateTableWidth();

      clearTimeout(timer);
      timer = null;
    }, 0);
  },
  methods: {
    updateTableWidth: function updateTableWidth() {
      var tbody = this.$el.querySelector(".".concat(config.prefix, "-table__body"));

      if (tbody) {
        this.tableWidth = tbody.clientWidth;
      } else {
        var el = this.$el.querySelector(".".concat(config.prefix, "-table-content"));
        el && (this.tableWidth = el.clientWidth);
      }
    },
    onFilterPopupVisibleChange: function onFilterPopupVisibleChange(visible, colKey) {
      this.$set(this.filterPopupVisible, colKey, visible);
    },
    renderFirstFilterRow: function renderFirstFilterRow() {
      var _this$pagination, _this$data;

      var h = this.$createElement;
      var filterEmpty = filterEmptyData(this.filterValue);
      if (!this.filterValue || !Object.keys(filterEmpty).length) return null;
      var defaultNode = h("div", {
        "class": "".concat(config.prefix, "-table__filter-result")
      }, [h("span", ["\u641C\u7D22 \u201C", this.getFilterResultContent(), "\u201D\uFF0C"]), h("span", ["\u627E\u5230 ", ((_this$pagination = this.pagination) === null || _this$pagination === void 0 ? void 0 : _this$pagination.total) || ((_this$data = this.data) === null || _this$data === void 0 ? void 0 : _this$data.length), " \u6761\u7ED3\u679C"]), h(button_index.Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "on": {
          "click": this.onResetAll
        }
      }, ["\u6E05\u7A7A\u7B5B\u9009"])]);
      var filterContent = utils_renderTnode.renderTNodeJSXDefault(this, "filterRow", {
        defaultNode: defaultNode
      });
      return h("div", {
        "style": {
          width: this.fixedLeftColumn ? "".concat(this.tableWidth, "px") : void 0
        },
        "class": "".concat(config.prefix, "-table__row-filter-inner")
      }, [filterContent]);
    },
    getFilterResultContent: function getFilterResultContent() {
      var _this2 = this;

      var arr = [];
      this.columns.filter(function (col) {
        return col.filter;
      }).forEach(function (col) {
        var value = _this2.filterValue[col.colKey];

        if (col.filter.list && !["null", "", "undefined"].includes(String(value))) {
          var formattedValue = value instanceof Array ? value : [value];
          var label = [];
          col.filter.list.forEach(function (option) {
            if (formattedValue.includes(option.value)) {
              label.push(option.label);
            }
          });
          value = label.join();
        }

        if (value) {
          arr.push("".concat(col.title, "\uFF1A").concat(value));
        }
      });
      return arr.join("\uFF1B");
    },
    onInnerFilterChange: function onInnerFilterChange(val, column) {
      var filterValue = _objectSpread(_objectSpread({}, this.innerFilterValue), {}, _defineProperty__default["default"]({}, column.colKey, val));

      this.innerFilterValue = filterValue;

      if (!column.filter.showConfirmAndReset) {
        this.emitFilterChange(filterValue, column);
      }
    },
    emitFilterChange: function emitFilterChange(filterValue, column) {
      utils_event.emitEvent(this, "filter-change", filterValue, {
        col: column
      });
      utils_event.emitEvent(this, "change", {
        filter: filterValue
      }, {
        trigger: "filter"
      });
    },
    onReset: function onReset(column) {
      var filterValue = _objectSpread(_objectSpread({}, this.innerFilterValue), {}, _defineProperty__default["default"]({}, column.colKey, {
        single: "",
        multiple: [],
        input: ""
      }[column.filter.type] || column.filter.resetValue || ""));

      this.innerFilterValue = filterValue;
      this.emitFilterChange(filterValue, column);
      this.$set(this.filterPopupVisible, column.colKey, false);
    },
    onResetAll: function onResetAll() {
      this.innerFilterValue = {};
      this.emitFilterChange({});
      this.filterPopupVisible = {};
    },
    onConfirm: function onConfirm(column) {
      this.emitFilterChange(this.innerFilterValue, column);
      this.$set(this.filterPopupVisible, column.colKey, false);
    },
    getBottomButtons: function getBottomButtons(column) {
      var _this3 = this;

      var h = this.$createElement;
      if (!column.filter.showConfirmAndReset) return;
      return h("div", {
        "class": "".concat(config.prefix, "-table__filter--bottom-buttons")
      }, [h(button_index.Button, {
        "attrs": {
          "theme": "default",
          "size": "small"
        },
        "on": {
          "click": function click() {
            return _this3.onReset(column);
          }
        }
      }, ["\u91CD\u7F6E"]), h(button_index.Button, {
        "attrs": {
          "theme": "primary",
          "size": "small"
        },
        "on": {
          "click": function click() {
            return _this3.onConfirm(column);
          }
        }
      }, ["\u786E\u8BA4"])]);
    },
    getFilterContent: function getFilterContent(column) {
      var _column$filter,
          _column$filter2,
          _column$filter3,
          _column$filter4,
          _column$filter5,
          _this4 = this,
          _column$filter6,
          _column$filter7;

      var h = this.$createElement;
      var types = ["single", "multiple", "input"];

      if (column.type && !types.includes(column.filter.type)) {
        console.error("TDesign Table Error: column.filter.type must be the following: ".concat(JSON.stringify(types)));
        return;
      }

      if (column !== null && column !== void 0 && (_column$filter = column.filter) !== null && _column$filter !== void 0 && _column$filter.component && typeof (column === null || column === void 0 ? void 0 : (_column$filter2 = column.filter) === null || _column$filter2 === void 0 ? void 0 : _column$filter2.component) !== "function") {
        console.error("TDesign Table Error: column.filter.component must be a function");
        return;
      }

      var component = {
        single: radio_index.RadioGroup,
        multiple: checkbox_index.CheckboxGroup,
        input: input_index.Input
      }[column.filter.type];
      if (!component && !(column !== null && column !== void 0 && (_column$filter3 = column.filter) !== null && _column$filter3 !== void 0 && _column$filter3.component)) return;

      var props = _objectSpread(_objectSpread({
        options: ["single", "multiple"].includes(column.filter.type) ? (_column$filter4 = column.filter) === null || _column$filter4 === void 0 ? void 0 : _column$filter4.list : void 0
      }, ((_column$filter5 = column.filter) === null || _column$filter5 === void 0 ? void 0 : _column$filter5.props) || {}), {}, {
        value: this.innerFilterValue[column.colKey]
      });

      var on = {
        change: function change(val) {
          return _this4.onInnerFilterChange(val, column);
        }
      };
      return h("div", {
        "class": "".concat(config.prefix, "-table__filter-pop-content-inner")
      }, [column !== null && column !== void 0 && (_column$filter6 = column.filter) !== null && _column$filter6 !== void 0 && _column$filter6.component ? column === null || column === void 0 ? void 0 : (_column$filter7 = column.filter) === null || _column$filter7 === void 0 ? void 0 : _column$filter7.component(function (v, b) {
        var tProps = _typeof__default["default"](b) === "object" && "attrs" in b ? b.attrs : {};
        return _this4.$createElement(v, {
          props: _objectSpread(_objectSpread({}, props), tProps),
          on: on
        });
      }) : h(component, helper.helper([{
        "attrs": {
          "value": this.innerFilterValue[column.colKey]
        }
      }, {
        "props": _objectSpread({}, props)
      }, {}, {
        "on": _objectSpread({}, on)
      }]))]);
    },
    getFilterColumns: function getFilterColumns(columns) {
      var _this5 = this;

      var h = this.$createElement;
      return columns.map(function (item, index) {
        var column = _objectSpread({}, item);

        if (column.filter) {
          var title = table_util_common.getTitle(_this5, column, index);

          column.title = function () {
            return h("div", {
              "class": "".concat(config.prefix, "-table__cell--title")
            }, [h("div", [title]), h("div", {
              "class": "".concat(config.prefix, "-table__cell--filter")
            }, [h(popup_index.Popup, helper.helper([{
              "attrs": {
                "visible": _this5.filterPopupVisible[column.colKey],
                "trigger": "click",
                "placement": "bottom",
                "showArrow": true,
                "overlayClassName": "".concat(config.prefix, "-table__filter-pop")
              }
            }, {
              "on": {
                "visible-change": function visibleChange(val) {
                  return _this5.onFilterPopupVisibleChange(val, column.colKey);
                }
              }
            }]), [isFunction__default["default"](_this5.filterIcon) ? _this5.filterIcon(_this5.$createElement) : h(tdesignIconsVue.FilterIcon, {
              "attrs": {
                "name": "filter"
              },
              "class": "".concat(config.prefix, "-table__filter-icon")
            }), h("template", {
              "slot": "content"
            }, [h("div", {
              "class": "".concat(config.prefix, "-table__filter-pop-content")
            }, [_this5.getFilterContent(column), _this5.getBottomButtons(column)])])])])]);
          };
        }

        return column;
      });
    }
  }
});

exports["default"] = filter;
//# sourceMappingURL=filter.js.map
