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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _typeof from '@babel/runtime/helpers/typeof';
import { defineComponent, ref, createVNode, mergeProps, h } from 'vue';
import { FilterIcon } from '@ldesign/icons-vue-next';
import isEmpty from 'lodash/isEmpty';
import isFunction from 'lodash/isFunction';
import { Popup } from '../popup/index.js';
import { CheckboxGroup } from '../checkbox/index.js';
import { RadioGroup } from '../radio/index.js';
import { Input } from '../input/index.js';
import { Button } from '../button/index.js';
import { useTNodeDefault } from '../hooks/tnode.js';
import { useConfig } from '../config-provider/useConfig.js';
import { useGlobalIcon } from '../hooks/useGlobalIcon.js';
import '../utils/withInstall.js';
import '../popup/popup.js';
import '@babel/runtime/helpers/slicedToArray';
import '@popperjs/core';
import 'lodash/isObject';
import 'lodash/isString';
import '../hooks/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-c9c789a8.js';
import 'dayjs';
import '../hooks/useVModel.js';
import 'lodash/kebabCase';
import '../utils/dom.js';
import '../utils/easing.js';
import '../_common/js/utils/set-style.js';
import '../popup/container.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../hooks/useResizeObserver.js';
import '../popup/props.js';
import 'lodash/camelCase';
import '../utils/render-tnode.js';
import '../checkbox/checkbox.js';
import '../form/hooks.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../checkbox/props.js';
import '../checkbox/constants.js';
import '../checkbox/group.js';
import 'lodash/intersection';
import 'lodash/isUndefined';
import '../hooks/slot.js';
import '../checkbox/checkbox-group-props.js';
import '../radio/radio.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/isNull';
import 'lodash/isNumber';
import '../radio/constants.js';
import '../radio/props.js';
import '../radio/group.js';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/regenerator';
import 'lodash/isNil';
import '../watermark/hooks.js';
import '../radio/radio-group-props.js';
import '../radio/useKeyboard.js';
import '../radio/radio-button.js';
import '../input/input.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../input/useLengthLimit.js';
import '../_common/js/log/log.js';
import '../_common/js/utils/helper.js';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../loading/loading.js';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../button/props.js';

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(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; }
var TableFilterController = defineComponent({
  name: "TableFilterController",
  props: {
    column: Object,
    colIndex: Number,
    tFilterValue: Object,
    innerFilterValue: Object,
    tableFilterClasses: Object,
    isFocusClass: String,
    primaryTableElement: {},
    popupProps: Object,
    attach: [String, Function],
    onVisibleChange: Function
  },
  emits: ["inner-filter-change", "reset", "confirm"],
  setup: function setup(props, context) {
    var triggerElementRef = ref(null);
    var renderTNode = useTNodeDefault();
    var _useConfig = useConfig("table"),
      t = _useConfig.t,
      globalConfig = _useConfig.globalConfig;
    var _useGlobalIcon = useGlobalIcon({
        FilterIcon: FilterIcon
      }),
      FilterIcon$1 = _useGlobalIcon.FilterIcon;
    var filterPopupVisible = ref(false);
    var onFilterPopupVisibleChange = function onFilterPopupVisibleChange(visible) {
      var _props$onVisibleChang;
      filterPopupVisible.value = visible;
      (_props$onVisibleChang = props.onVisibleChange) === null || _props$onVisibleChang === void 0 ? void 0 : _props$onVisibleChang.call(props, visible);
    };
    var renderComponent = function renderComponent(column, filterComponentProps, component) {
      var _props$innerFilterVal;
      if (!component) return null;
      var isVueComponent = !!component.setup;
      if (isFunction(column.filter.component) && !isVueComponent) {
        return column.filter.component(function (v, b) {
          var tProps = _typeof(b) === "object" && "attrs" in b ? b.attrs : {};
          return h(v, {
            props: _objectSpread(_objectSpread({}, filterComponentProps), tProps)
          });
        });
      }
      return createVNode(component, mergeProps({
        "value": (_props$innerFilterVal = props.innerFilterValue) === null || _props$innerFilterVal === void 0 ? void 0 : _props$innerFilterVal[column.colKey]
      }, filterComponentProps), null);
    };
    var getFilterContent = function getFilterContent(column) {
      var _column$filter, _column$filter2, _props$innerFilterVal2;
      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;
      }
      var component = {
        single: RadioGroup,
        multiple: CheckboxGroup,
        input: Input
      }[column.filter.type] || column.filter.component;
      if (!component && !column.filter.component) return;
      var filterComponentProps = _objectSpread(_objectSpread({
        options: ["single", "multiple"].includes(column.filter.type) ? (_column$filter = column.filter) === null || _column$filter === void 0 ? void 0 : _column$filter.list : void 0
      }, ((_column$filter2 = column.filter) === null || _column$filter2 === void 0 ? void 0 : _column$filter2.props) || {}), {}, {
        value: (_props$innerFilterVal2 = props.innerFilterValue) === null || _props$innerFilterVal2 === void 0 ? void 0 : _props$innerFilterVal2[column.colKey],
        onChange: function onChange(val, ctx) {
          var _column$filter$props, _column$filter$props$, _column$filter$props2;
          context.emit("inner-filter-change", val, column);
          if ((_column$filter$props = column.filter.props) !== null && _column$filter$props !== void 0 && _column$filter$props.onChange) (_column$filter$props$ = (_column$filter$props2 = column.filter.props).onChange) === null || _column$filter$props$ === void 0 ? void 0 : _column$filter$props$.call(_column$filter$props2, val, ctx);
        }
      });
      if (column.filter.confirmEvents) {
        column.filter.confirmEvents.forEach(function (event) {
          filterComponentProps[event] = function () {
            context.emit("confirm", column);
            filterPopupVisible.value = false;
          };
        });
      }
      return createVNode("div", {
        "class": props.tableFilterClasses.contentInner
      }, [renderComponent(column, filterComponentProps, component)]);
    };
    var getBottomButtons = function getBottomButtons(column) {
      if (!column.filter.showConfirmAndReset) return;
      return createVNode("div", {
        "class": props.tableFilterClasses.bottomButtons
      }, [createVNode(Button, {
        "theme": "default",
        "size": "small",
        "onClick": function onClick() {
          context.emit("reset", column);
          filterPopupVisible.value = false;
        }
      }, {
        "default": function _default() {
          return [globalConfig.value.resetText];
        }
      }), createVNode(Button, {
        "theme": "primary",
        "size": "small",
        "onClick": function onClick() {
          context.emit("confirm", column);
          filterPopupVisible.value = false;
        }
      }, {
        "default": function _default() {
          return [globalConfig.value.confirmText];
        }
      })]);
    };
    var getContent = function getContent() {
      return createVNode("div", {
        "class": props.tableFilterClasses.popupContent
      }, [getFilterContent(props.column), getBottomButtons(props.column)]);
    };
    return {
      t: t,
      globalConfig: globalConfig,
      FilterIcon: FilterIcon$1,
      filterPopupVisible: filterPopupVisible,
      triggerElementRef: triggerElementRef,
      renderTNode: renderTNode,
      getContent: getContent,
      onFilterPopupVisibleChange: onFilterPopupVisibleChange
    };
  },
  render: function render() {
    var _this$tFilterValue,
      _this = this;
    var column = this.column,
      popupProps = this.popupProps,
      FilterIcon = this.FilterIcon;
    if (!column.filter || column.filter && !Object.keys(column.filter).length) return null;
    var defaultFilterIcon = this.t(this.globalConfig.filterIcon) || createVNode(FilterIcon, null, null);
    var filterValue = (_this$tFilterValue = this.tFilterValue) === null || _this$tFilterValue === void 0 ? void 0 : _this$tFilterValue[column.colKey];
    var isObjectTrue = _typeof(filterValue) === "object" && !isEmpty(filterValue);
    var isValueTrue = filterValue && _typeof(filterValue) !== "object";
    return createVNode(Popup, mergeProps({
      "attach": this.attach || (this.primaryTableElement ? function () {
        return _this.primaryTableElement;
      } : void 0),
      "visible": this.filterPopupVisible,
      "destroyOnClose": true,
      "trigger": "click",
      "placement": "bottom-right",
      "showArrow": true,
      "overlayClassName": this.tableFilterClasses.popup,
      "onVisibleChange": function onVisibleChange(val) {
        return _this.onFilterPopupVisibleChange(val);
      },
      "class": [this.tableFilterClasses.icon, _defineProperty({}, this.isFocusClass, isObjectTrue || isValueTrue)],
      "content": this.getContent
    }, popupProps), {
      "default": function _default() {
        return [createVNode("div", {
          "ref": "triggerElementRef"
        }, [_this.renderTNode("filterIcon", {
          defaultNode: defaultFilterIcon,
          params: {
            col: column,
            colIndex: _this.colIndex
          }
        })])];
      }
    });
  }
});

export { TableFilterController as default };
//# sourceMappingURL=filter-controller.js.map
