/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var components_button_index = require('../../button/index.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
var hooks_lnode = require('../../../hooks/lnode.js');
var components_table_core_filterController = require('../core/filter-controller.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
var components_table_hooks_useClassName = require('./useClassName.js');
require('../../../utils/with-install.js');
require('vue');
require('lodash');
require('../../../config.js');
require('../../button/button.js');
require('@ldesign/icons-vue');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/easing.js');
require('../../../utils/render-node.js');
require('../../../utils/transfer-dom.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/set-style.js');
require('../../../chunks/helper2.js');
require('../../loading/plugin.js');
require('../../../utils/ripple.js');
require('../../button/props.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../hooks/render-lnode.js');
require('lodash/lowerFirst');
require('../../popup/index.js');
require('../../../utils/map-props.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../../utils/helper.js');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../../utils/event.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../radio/index.js');
require('../../radio/radio.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('lodash/isNumber');
require('../../radio/radio-button.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../../hooks/use-global-icon.js');

function isFilterValueExist(value) {
  const isArrayTrue = Array.isArray(value) && value.length;
  const isObject = typeof value === "object" && !Array.isArray(value);
  const isObjectTrue = isObject && Object.keys(value).length;
  return isArrayTrue || isObjectTrue || !["null", "", "undefined"].includes(String(value));
}
function filterEmptyData(data) {
  const newFilterValue = {};
  Object.keys(data).forEach(key => {
    const item = data[key];
    if (isFilterValueExist(item)) newFilterValue[key] = item;
  });
  return newFilterValue;
}
function useFilter(props, context) {
  const primaryTableRef = VueCompositionAPI.ref(null);
  const {
    t,
    global
  } = components_configProvider_useConfig.useConfig("table");
  const renderTNode = hooks_lnode.useLNodeDefault();
  const {
    filterValue
  } = VueCompositionAPI.toRefs(props);
  const {
    tableFilterClasses,
    isFocusClass
  } = components_table_hooks_useClassName["default"]();
  const isTableOverflowHidden = VueCompositionAPI.ref();
  const [tFilterValue, setTFilterValue] = hooks_useDefaultValue["default"](filterValue, props.defaultFilterValue, props.onFilterChange, "filterValue", "filter-change");
  const innerFilterValue = VueCompositionAPI.ref(tFilterValue.value);
  const hasEmptyCondition = VueCompositionAPI.computed(() => {
    const filterEmpty = filterEmptyData(tFilterValue.value || {});
    return !tFilterValue.value || !Object.keys(filterEmpty).length;
  });
  VueCompositionAPI.watch([tFilterValue], _ref => {
    let [val] = _ref;
    innerFilterValue.value = val;
  });
  function renderFirstFilterRow(h) {
    var _props$pagination, _props$data;
    if (hasEmptyCondition.value) return null;
    const 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(components_button_index.Button, {
      "attrs": {
        "theme": "primary",
        "variant": "text"
      },
      "on": {
        "click": onResetAll
      }
    }, [global.value.clearFilterResultButtonText])]);
    const filterContent = renderTNode("filterRow");
    if (props.filterRow && !filterContent) return null;
    return h("div", {
      "class": tableFilterClasses.inner
    }, [filterContent || defaultNode]);
  }
  function getFilterResultContent() {
    const arr = [];
    props.columns.filter(col => col.filter).forEach(col => {
      let value = tFilterValue.value[col.colKey];
      if (col.filter.list && !["null", "", "undefined"].includes(String(value))) {
        const formattedValue = Array.isArray(value) ? value : [value];
        const label = [];
        col.filter.list.forEach(option => {
          if (formattedValue.includes(option.value)) label.push(option.label);
        });
        value = label.join();
      }
      if (value) arr.push(`${col.title}\uFF1A${value}`);
    });
    return arr.join("\uFF1B");
  }
  function onInnerFilterChange(val, column) {
    const filterValue2 = {
      ...innerFilterValue.value,
      [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) {
    const filterValue2 = {
      ...tFilterValue.value,
      [column.colKey]: column.filter.resetValue ?? {
        single: "",
        multiple: [],
        input: ""
      }[column.filter.type] ?? ""
    };
    emitFilterChange(filterValue2, column);
  }
  function onResetAll() {
    emitFilterChange({}, void 0);
  }
  function onConfirm(column) {
    emitFilterChange(innerFilterValue.value, column);
  }
  function renderFilterIcon(h, _ref2) {
    var _primaryTableRef$valu;
    let {
      col
    } = _ref2;
    return h(components_table_core_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,
    isTableOverflowHidden,
    renderFilterIcon,
    renderFirstFilterRow,
    setFilterPrimaryTableRef
  };
}

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