/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _typeof = require('@babel/runtime/helpers/typeof');
var vue = require('vue');
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');
var table_hooks_useClassName = require('./useClassName.js');
require('../../utils/withInstall.js');
require('../../button/button.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../../loading/index.js');
require('../../loading/directive.js');
require('../../loading/plugin.js');
require('../../utils/dom.js');
require('../../_chunks/dep-7f0559c3.js');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isArray');
require('../../utils/easing.js');
require('../../hooks/useConfig.js');
require('lodash/cloneDeep');
require('../../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('dayjs');
require('../../loading/loading.js');
require('../../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../hooks/useTeleport.js');
require('../../loading/props.js');
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('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('../../hooks/useRipple.js');
require('../../utils/set-style.js');
require('../../hooks/useKeepAnimation.js');
require('../../form/hooks.js');
require('../../button/props.js');
require('@ldesign/icons-vue-next');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../hooks/useVModel.js');
require('../../popup/container.js');
require('../../hooks/useResizeObserver.js');
require('../../popup/props.js');
require('../../checkbox/index.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/constants.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../hooks/slot.js');
require('../../checkbox/checkbox-group-props.js');
require('../../radio/index.js');
require('../../radio/radio.js');
require('../../utils/helper.js');
require('../../radio/constants.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/regenerator');
require('lodash/isNil');
require('../../watermark/hooks.js');
require('../../radio/radio-group-props.js');
require('../../radio/useKeyboard.js');
require('../../radio/radio-button.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../hooks/useGlobalIcon.js');
require('../../input/props.js');
require('../../input/useInput.js');
require('../../form/const.js');
require('../../input/useLengthLimit.js');
require('../../_common/js/log/log.js');
require('../../input/useInputEventHandler.js');
require('../../input/useInputWidth.js');
require('../../input/input-group.js');
require('../../input/input-group-props.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 isFilterValueExist(value) {
  var isArrayTrue = Array.isArray(value) && value.length;
  var isObject = _typeof__default["default"](value) === "object" && !Array.isArray(value);
  var isObjectTrue = isObject && Object.keys(value).length;
  return isArrayTrue || isObjectTrue || !["null", "", "undefined"].includes(String(value));
}
function filterEmptyData(data) {
  var newFilterValue = {};
  Object.keys(data).forEach(function (key) {
    var item = data[key];
    if (isFilterValueExist(item)) newFilterValue[key] = item;
  });
  return newFilterValue;
}
function useFilter(props, context) {
  var primaryTableRef = vue.ref(null);
  var _useConfig = configProvider_useConfig.useConfig("table"),
    t = _useConfig.t,
    globalConfig = _useConfig.globalConfig;
  var renderTNode = hooks_tnode.useTNodeDefault();
  var _toRefs = vue.toRefs(props),
    filterValue = _toRefs.filterValue;
  var _useClassName = table_hooks_useClassName["default"](),
    tableFilterClasses = _useClassName.tableFilterClasses,
    isFocusClass = _useClassName.isFocusClass;
  var isTableOverflowHidden = vue.ref();
  var _useDefaultValue = hooks_useDefaultValue["default"](filterValue, props.defaultFilterValue, props.onFilterChange, "filterValue"),
    _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
    tFilterValue = _useDefaultValue2[0],
    setTFilterValue = _useDefaultValue2[1];
  var innerFilterValue = vue.ref(tFilterValue.value);
  var hasEmptyCondition = vue.computed(function () {
    var filterEmpty = filterEmptyData(tFilterValue.value || {});
    return !tFilterValue.value || !Object.keys(filterEmpty).length;
  });
  vue.watch([tFilterValue], function (_ref) {
    var _ref2 = _slicedToArray__default["default"](_ref, 1),
      val = _ref2[0];
    innerFilterValue.value = val;
  });
  function renderFirstFilterRow() {
    var _props$pagination, _props$data;
    if (hasEmptyCondition.value) return null;
    var defaultNode = vue.createVNode("div", {
      "class": tableFilterClasses.result
    }, [vue.createVNode("span", null, [t(globalConfig.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)
    })]), vue.createVNode(button_index.Button, {
      "theme": "primary",
      "variant": "text",
      "onClick": onResetAll
    }, {
      "default": function _default() {
        return [globalConfig.value.clearFilterResultButtonText];
      }
    })]);
    var filterContent = renderTNode("filterRow");
    if (props.filterRow && !filterContent || props.filterRow === null) return null;
    return vue.createVNode("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 = Array.isArray(value) ? value : [value];
        var label = [];
        col.filter.list.forEach(function (option) {
          if (formattedValue.includes(option.value)) label.push(option.label);
        });
        value = label.join();
      }
      if (isFilterValueExist(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"
    });
  }
  function onReset(column) {
    var _ref3, _column$filter$resetV;
    var filterValue2 = _objectSpread(_objectSpread({}, tFilterValue.value), {}, _defineProperty__default["default"]({}, column.colKey, (_ref3 = (_column$filter$resetV = column.filter.resetValue) !== null && _column$filter$resetV !== void 0 ? _column$filter$resetV : {
      single: "",
      multiple: [],
      input: ""
    }[column.filter.type]) !== null && _ref3 !== void 0 ? _ref3 : ""));
    emitFilterChange(filterValue2, column);
  }
  function onResetAll() {
    emitFilterChange({}, void 0);
  }
  function onConfirm(column) {
    emitFilterChange(innerFilterValue.value, column);
  }
  function renderFilterIcon(_ref4) {
    var _primaryTableRef$valu;
    var col = _ref4.col,
      colIndex = _ref4.colIndex;
    return vue.createVNode(table_filterController["default"], {
      "column": col,
      "colIndex": colIndex,
      "filterIcon": props.filterIcon,
      "tFilterValue": tFilterValue.value,
      "innerFilterValue": innerFilterValue.value,
      "tableFilterClasses": tableFilterClasses,
      "isFocusClass": isFocusClass,
      "popupProps": col.filter.popupProps,
      "attach": props.attach,
      "onReset": onReset,
      "onConfirm": onConfirm,
      "onInnerFilterChange": onInnerFilterChange,
      "primaryTableElement": (_primaryTableRef$valu = primaryTableRef.value) === null || _primaryTableRef$valu === void 0 ? void 0 : _primaryTableRef$valu.$el,
      "onVisibleChange": onPopupVisibleChange
    }, {
      filterIcon: context.slots.filterIcon
    });
  }
  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
