/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../../_chunks/dep-99705d51.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _typeof = require('@babel/runtime/helpers/typeof');
var VueCompositionAPI = require('@vue/composition-api');
var table_hooks_useClassName = require('./useClassName.js');
var button_index = require('../../button/index.js');
var hooks_useDefaultValue = require('../../hooks/useDefaultValue.js');
var hooks_tnode = require('../../hooks/tnode.js');
var table_filterController = require('../filter-controller.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('../../button/button.js');
require('../../config.js');
require('../../utils/classnames.js');
require('../../loading/index.js');
require('../../loading/loading.js');
require('vue');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/set-style.js');
require('../../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/render-tnode.js');
require('../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../loading/plugin.js');
require('../../button/props.js');
require('../../utils/ripple.js');
require('../../config-provider/config-receiver.js');
require('../../utils/mixins.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../hooks/render-tnode.js');
require('lodash/isObject');
require('tdesign-icons-vue');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../utils/helper.js');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../utils/map-props.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/intersection');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../utils/event.js');
require('../../checkbox/checkbox-group-props.js');
require('../../radio/index.js');
require('../../radio/radio.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('lodash/isNumber');
require('../../radio/radio-group-props.js');
require('../../radio/radio-button.js');
require('../../input/index.js');
require('../../input/addon.js');
require('../../input/input.js');
require('../../input/props.js');
require('../../input/input-group.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);

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

function useFilter(props, context) {
  var primaryTableRef = VueCompositionAPI.ref(null);

  var _useConfig = configProvider_useConfig.useConfig("table"),
      t = _useConfig.t,
      global = _useConfig.global;

  var renderTNode = hooks_tnode.useTNodeDefault();

  var _toRefs = VueCompositionAPI.toRefs(props),
      filterValue = _toRefs.filterValue;

  var _useClassName = table_hooks_useClassName["default"](),
      tableFilterClasses = _useClassName.tableFilterClasses,
      isFocusClass = _useClassName.isFocusClass;

  var isTableOverflowHidden = VueCompositionAPI.ref();

  var _useDefaultValue = hooks_useDefaultValue["default"](filterValue, props.defaultFilterValue, props.onFilterChange, "filterValue", "filter-change"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      tFilterValue = _useDefaultValue2[0],
      setTFilterValue = _useDefaultValue2[1];

  var innerFilterValue = VueCompositionAPI.ref(tFilterValue.value);
  var hasEmptyCondition = VueCompositionAPI.computed(function () {
    var filterEmpty = filterEmptyData(tFilterValue.value || {});
    return !tFilterValue.value || !Object.keys(filterEmpty).length;
  });
  VueCompositionAPI.watch([tFilterValue], function (_ref) {
    var _ref2 = _slicedToArray__default["default"](_ref, 1),
        val = _ref2[0];

    innerFilterValue.value = val;
  });

  function renderFirstFilterRow(h) {
    var _props$pagination, _props$data;

    if (hasEmptyCondition.value) return null;
    var defaultNode = h("div", {
      "class": tableFilterClasses.result
    }, [t(global.value.searchResultText, {
      result: getFilterResultContent(),
      count: ((_props$pagination = props.pagination) === null || _props$pagination === void 0 ? void 0 : _props$pagination.total) || ((_props$data = props.data) === null || _props$data === void 0 ? void 0 : _props$data.length)
    }), h(button_index.Button, {
      "attrs": {
        "theme": "primary",
        "variant": "text"
      },
      "on": {
        "click": onResetAll
      }
    }, [global.value.clearFilterResultButtonText])]);
    var filterContent = renderTNode("filterRow");
    if (props.filterRow && !filterContent) return null;
    return h("div", {
      "class": tableFilterClasses.inner
    }, [filterContent || defaultNode]);
  }

  function getFilterResultContent() {
    var arr = [];
    props.columns.filter(function (col) {
      return col.filter;
    }).forEach(function (col) {
      var value = tFilterValue.value[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");
  }

  function onInnerFilterChange(val, column) {
    var filterValue2 = _objectSpread(_objectSpread({}, innerFilterValue.value), {}, _defineProperty__default["default"]({}, column.colKey, val));

    innerFilterValue.value = filterValue2;

    if (!column.filter.showConfirmAndReset) {
      emitFilterChange(filterValue2, column);
    }
  }

  function emitFilterChange(filterValue2, column) {
    var _props$onChange;

    setTFilterValue(filterValue2, {
      col: column
    });
    (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, {
      filter: filterValue2
    }, {
      trigger: "filter"
    });
    context.emit("change", {
      filter: filterValue2
    }, {
      trigger: "filter"
    });
  }

  function onReset(column) {
    var filterValue2 = _objectSpread(_objectSpread({}, tFilterValue.value), {}, _defineProperty__default["default"]({}, column.colKey, {
      single: "",
      multiple: [],
      input: ""
    }[column.filter.type] || column.filter.resetValue || ""));

    emitFilterChange(filterValue2, column);
  }

  function onResetAll() {
    emitFilterChange({}, void 0);
  }

  function onConfirm(column) {
    emitFilterChange(innerFilterValue.value, column);
  }

  function renderFilterIcon(h, _ref3) {
    var _primaryTableRef$valu;

    var col = _ref3.col;
    return h(table_filterController["default"], helper.helper([{
      "scopedSlots": {
        filterIcon: context.slots.filterIcon
      },
      "attrs": {
        "column": col,
        "filterIcon": props.filterIcon,
        "tFilterValue": tFilterValue.value,
        "innerFilterValue": innerFilterValue.value,
        "tableFilterClasses": tableFilterClasses,
        "isFocusClass": isFocusClass,
        "primaryTableElement": (_primaryTableRef$valu = primaryTableRef.value) === null || _primaryTableRef$valu === void 0 ? void 0 : _primaryTableRef$valu.$el
      }
    }, {
      "on": {
        reset: onReset,
        confirm: onConfirm,
        "inner-filter-change": onInnerFilterChange,
        "visible-change": onPopupVisibleChange
      }
    }]));
  }

  function setFilterPrimaryTableRef(primaryTableElement) {
    primaryTableRef.value = primaryTableElement;
  }

  function onPopupVisibleChange(visible) {
    if (visible && !isTableOverflowHidden.value) {
      isTableOverflowHidden.value = !visible;
    }
  }

  return {
    hasEmptyCondition: hasEmptyCondition,
    isTableOverflowHidden: isTableOverflowHidden,
    renderFilterIcon: renderFilterIcon,
    renderFirstFilterRow: renderFirstFilterRow,
    setFilterPrimaryTableRef: setFilterPrimaryTableRef
  };
}

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