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

import { h as helper } from '../../../chunks/helper.js';
import { ref, toRefs, computed, watch } from '@vue/composition-api';
import { Button } from '../../button/index.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { useLNodeDefault } from '../../../hooks/lnode.js';
import TableFilterController from '../core/filter-controller.js';
import '../../../hooks/use-config.js';
import useClassName from './useClassName.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../chunks/_commonjsHelpers.js';
import '../../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../../config.js';
import '../../button/button.js';
import '@ldesign/icons-vue';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '../../../utils/transfer-dom.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style/css.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../../utils/ripple.js';
import '../../button/props.js';
import '../../button/style/css.js';
import '../../button/type.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../hooks/render-lnode.js';
import 'lodash/lowerFirst';
import '../../popup/index.js';
import '../../../utils/map-props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../utils/helper.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/style/css.js';
import '../../popup/type.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../../utils/event.js';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/style/css.js';
import '../../checkbox/type.js';
import '../../radio/index.js';
import '../../radio/radio.js';
import '../../radio/props.js';
import '../../radio/group.js';
import 'lodash/isNumber';
import '../../radio/radio-button.js';
import '../../radio/style/css.js';
import '../../radio/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style/css.js';
import '../../input/type.js';
import '../../../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 = ref(null);
  const {
    t,
    global
  } = useConfig("table");
  const renderTNode = useLNodeDefault();
  const {
    filterValue
  } = toRefs(props);
  const {
    tableFilterClasses,
    isFocusClass
  } = useClassName();
  const isTableOverflowHidden = ref();
  const [tFilterValue, setTFilterValue] = useDefaultValue(filterValue, props.defaultFilterValue, props.onFilterChange, "filterValue", "filter-change");
  const innerFilterValue = ref(tFilterValue.value);
  const hasEmptyCondition = computed(() => {
    const filterEmpty = filterEmptyData(tFilterValue.value || {});
    return !tFilterValue.value || !Object.keys(filterEmpty).length;
  });
  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(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(TableFilterController, 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
  };
}

export { useFilter as default };
//# sourceMappingURL=useFilter.js.map
