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

import { h as helper } from '../_chunks/dep-b6ac6e96.js';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref } from '@vue/composition-api';
import { FilterIcon } from '@ldesign/icons-vue';
import isEmpty from 'lodash/isEmpty';
import lowerFirst from 'lodash/lowerFirst';
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 { useGlobalIcon } from '../hooks/useGlobalIcon.js';
import { useConfig } from '../config-provider/useConfig.js';
import log from '../_common/js/log/log.js';
import '../utils/map-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'vue';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../popup/popup.js';
import '@popperjs/core';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/helper.js';
import '@babel/runtime/helpers/slicedToArray';
import '../_common/js/utils/set-style.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-2cb61c79.js';
import 'dayjs';
import '../utils/mixins.js';
import '../utils/event.js';
import '../popup/props.js';
import '../popup/container.js';
import 'lodash/isFunction';
import '../popup/utils.js';
import '../popup/trigger.js';
import '../popup/plugin.js';
import '../checkbox/group.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/checkbox-group-props.js';
import '../radio/radio.js';
import '../radio/props.js';
import '../radio/group.js';
import 'lodash/isNumber';
import '../radio/radio-group-props.js';
import '../radio/radio-button.js';
import '../input/input.js';
import '../_common/js/utils/helper.js';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isArray';
import '../input/props.js';
import '../input/input-group.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import 'lodash/isObject';
import 'lodash/mapKeys';
import 'lodash/isEqual';
import '../loading/plugin.js';
import '../config.js';
import '../loading/loading.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../utils/ripple.js';
import '../button/props.js';
import '../hooks/render-tnode.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]
  },
  setup: function setup(props, _ref) {
    var emit = _ref.emit;
    var triggerElementRef = ref(null);
    var renderTNode = useTNodeDefault();
    var _useConfig = useConfig("table"),
      t = _useConfig.t,
      global = _useConfig.global;
    var _useGlobalIcon = useGlobalIcon({
        FilterIcon: FilterIcon
      }),
      FilterIcon$1 = _useGlobalIcon.FilterIcon;
    var filterPopupVisible = ref(false);
    var onFilterPopupVisibleChange = function onFilterPopupVisibleChange(visible) {
      filterPopupVisible.value = visible;
      emit("visible-change", visible);
    };
    return {
      t: t,
      global: global,
      FilterIcon: FilterIcon$1,
      filterPopupVisible: filterPopupVisible,
      triggerElementRef: triggerElementRef,
      renderTNode: renderTNode,
      onFilterPopupVisibleChange: onFilterPopupVisibleChange
    };
  },
  render: function render(h) {
    var _this = this,
      _this$tFilterValue;
    var getFilterContent = function getFilterContent(h2, column2) {
      var _column2$filter, _column2$filter2, _this$innerFilterValu;
      var types = ["single", "multiple", "input"];
      if (column2.type && !types.includes(column2.filter.type)) {
        log.error("Table", "filter.type must be the following: ".concat(JSON.stringify(types)));
        return;
      }
      if (!column2.filter.type && !column2.filter.component) {
        log.error("Table", "both filter.type and filter.component can not be empty.");
        return;
      }
      var component = {
        single: RadioGroup,
        multiple: CheckboxGroup,
        input: Input
      }[column2.filter.type] || column2.filter.component;
      if (!component && !column2.filter.component) return;
      var filterComponentProps = _objectSpread(_objectSpread({
        options: ["single", "multiple"].includes(column2.filter.type) ? (_column2$filter = column2.filter) === null || _column2$filter === void 0 ? void 0 : _column2$filter.list : void 0
      }, ((_column2$filter2 = column2.filter) === null || _column2$filter2 === void 0 ? void 0 : _column2$filter2.props) || {}), {}, {
        value: (_this$innerFilterValu = _this.innerFilterValue) === null || _this$innerFilterValu === void 0 ? void 0 : _this$innerFilterValu[column2.colKey]
      });
      if (column2.filter.type === "single") {
        filterComponentProps.onChange = function (val) {
          _this.$emit("inner-filter-change", val, column2);
        };
      }
      var on = {
        change: function change(val) {
          _this.$emit("inner-filter-change", val, column2);
        }
      };
      if (column2.filter.confirmEvents) {
        column2.filter.confirmEvents.forEach(function (event) {
          var pureEvent = lowerFirst(event.replace("on", ""));
          on[pureEvent] = function () {
            _this.$emit("confirm", column2);
            _this.filterPopupVisible = false;
          };
        });
      }
      var wrapperListeners = {};
      if (column2.filter.showConfirmAndReset) wrapperListeners.click = function (e) {
        return e.stopPropagation();
      };
      var renderComponent = function renderComponent() {
        var _this$innerFilterValu2;
        if (!component) return null;
        var isVueComponent = component.install && component.component;
        if (typeof component === "function" && !isVueComponent) {
          return component(function (v, b) {
            var tProps = _typeof(b) === "object" && "attrs" in b ? b.attrs : {};
            return h2(v, {
              props: _objectSpread(_objectSpread({}, filterComponentProps), tProps),
              on: on
            });
          });
        }
        return h(component, helper([{
          "attrs": {
            "value": (_this$innerFilterValu2 = _this.innerFilterValue) === null || _this$innerFilterValu2 === void 0 ? void 0 : _this$innerFilterValu2[column2.colKey]
          }
        }, {
          "props": _objectSpread({}, filterComponentProps)
        }, {}, {
          "on": _objectSpread({}, on)
        }]));
      };
      return h("div", helper([{
        "class": _this.tableFilterClasses.contentInner
      }, {
        "on": wrapperListeners
      }]), [renderComponent()]);
    };
    var getBottomButtons = function getBottomButtons(h2, column2) {
      if (!column2.filter.showConfirmAndReset) return;
      return h("div", {
        "class": _this.tableFilterClasses.bottomButtons
      }, [h(Button, {
        "attrs": {
          "theme": "default",
          "size": "small"
        },
        "on": {
          "click": function click() {
            _this.$emit("reset", column2);
            _this.filterPopupVisible = false;
          }
        }
      }, [_this.global.resetText]), h(Button, {
        "attrs": {
          "theme": "primary",
          "size": "small"
        },
        "on": {
          "click": function click() {
            _this.$emit("confirm", column2);
            _this.filterPopupVisible = false;
          }
        }
      }, [_this.global.confirmText])]);
    };
    var column = this.column,
      FilterIcon = this.FilterIcon,
      popupProps = this.popupProps;
    if (!column.filter || column.filter && !Object.keys(column.filter).length) return null;
    var defaultFilterIcon = this.t(this.global.filterIcon) || h(FilterIcon);
    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 h(Popup, helper([{
      "attrs": {
        "attach": this.attach || (this.primaryTableElement ? function () {
          return _this.primaryTableElement;
        } : void 0),
        "visible": this.filterPopupVisible,
        "destroyOnClose": true,
        "trigger": "click",
        "placement": "bottom",
        "showArrow": true,
        "overlayClassName": this.tableFilterClasses.popup
      }
    }, {
      "on": {
        "visible-change": function visibleChange(val) {
          return _this.onFilterPopupVisibleChange(val);
        }
      }
    }, {
      "class": [this.tableFilterClasses.icon, _defineProperty({}, this.isFocusClass, isObjectTrue || isValueTrue)],
      "attrs": {
        "content": function content() {
          return h("div", {
            "class": _this.tableFilterClasses.popupContent
          }, [getFilterContent(h, column), getBottomButtons(h, column)]);
        }
      }
    }, {
      "props": popupProps
    }]), [h("div", {
      "ref": "triggerElementRef"
    }, [this.renderTNode("filterIcon", {
      defaultNode: defaultFilterIcon,
      params: {
        col: column,
        colIndex: this.colIndex
      }
    })])]);
  }
});

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