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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _typeof = require('@babel/runtime/helpers/typeof');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var isEmpty = require('lodash/isEmpty');
var isFunction = require('lodash/isFunction');
var popup_index = require('../popup/index.js');
var checkbox_index = require('../checkbox/index.js');
var radio_index = require('../radio/index.js');
var input_index = require('../input/index.js');
var button_index = require('../button/index.js');
var hooks_tnode = require('../hooks/tnode.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
require('../utils/withInstall.js');
require('../popup/popup.js');
require('@babel/runtime/helpers/slicedToArray');
require('@popperjs/core');
require('lodash/isString');
require('lodash/isObject');
require('../hooks/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('../utils/dom.js');
require('../utils/easing.js');
require('../utils/render-tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../_common/js/utils/set-style.js');
require('../hooks/useVModel.js');
require('../popup/props.js');
require('../popup/container.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/isUndefined');
require('../hooks/useResizeObserver.js');
require('../checkbox/checkbox.js');
require('../form/hooks.js');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.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/radio.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isNumber');
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('../radio/radio-group-props.js');
require('../radio/useKeyboard.js');
require('../radio/radio-button.js');
require('../input/input.js');
require('../input/props.js');
require('../input/useInput.js');
require('../form/const.js');
require('../input/useLengthLimit.js');
require('../_common/js/log/log.js');
require('../_common/js/utils/helper.js');
require('../input/useInputEventHandler.js');
require('../input/useInputWidth.js');
require('../input/input-group.js');
require('../input/input-group-props.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../hooks/useTeleport.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../button/props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

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; }
var TableFilterController = vue.defineComponent({
  name: "TableFilterController",
  props: {
    column: Object,
    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 = vue.ref(null);
    var renderTNode = hooks_tnode.useTNodeDefault();
    var _useConfig = configProvider_useConfig.useConfig("table"),
      t = _useConfig.t,
      globalConfig = _useConfig.globalConfig;
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        FilterIcon: iconsVueNext.FilterIcon
      }),
      FilterIcon = _useGlobalIcon.FilterIcon;
    var filterPopupVisible = vue.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__default["default"](column.filter.component) && !isVueComponent) {
        return column.filter.component(function (v, b) {
          var tProps = _typeof__default["default"](b) === "object" && "attrs" in b ? b.attrs : {};
          return vue.h(v, {
            props: _objectSpread(_objectSpread({}, filterComponentProps), tProps)
          });
        });
      }
      return vue.createVNode(component, vue.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: radio_index.RadioGroup,
        multiple: checkbox_index.CheckboxGroup,
        input: input_index.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) {
          context.emit("inner-filter-change", val, column);
        }
      });
      if (column.filter.confirmEvents) {
        column.filter.confirmEvents.forEach(function (event) {
          filterComponentProps[event] = function () {
            context.emit("confirm", column);
            filterPopupVisible.value = false;
          };
        });
      }
      return vue.createVNode("div", {
        "class": props.tableFilterClasses.contentInner
      }, [renderComponent(column, filterComponentProps, component)]);
    };
    var getBottomButtons = function getBottomButtons(column) {
      if (!column.filter.showConfirmAndReset) return;
      return vue.createVNode("div", {
        "class": props.tableFilterClasses.bottomButtons
      }, [vue.createVNode(button_index.Button, {
        "theme": "default",
        "size": "small",
        "onClick": function onClick() {
          context.emit("reset", column);
          filterPopupVisible.value = false;
        }
      }, {
        "default": function _default() {
          return [globalConfig.value.resetText];
        }
      }), vue.createVNode(button_index.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 vue.createVNode("div", {
        "class": props.tableFilterClasses.popupContent
      }, [getFilterContent(props.column), getBottomButtons(props.column)]);
    };
    return {
      t: t,
      globalConfig: globalConfig,
      FilterIcon: FilterIcon,
      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) || vue.createVNode(FilterIcon, null, null);
    var filterValue = (_this$tFilterValue = this.tFilterValue) === null || _this$tFilterValue === void 0 ? void 0 : _this$tFilterValue[column.colKey];
    var isObjectTrue = _typeof__default["default"](filterValue) === "object" && !isEmpty__default["default"](filterValue);
    var isValueTrue = filterValue && _typeof__default["default"](filterValue) !== "object";
    return vue.createVNode(popup_index.Popup, vue.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__default["default"]({}, this.isFocusClass, isObjectTrue || isValueTrue)],
      "content": this.getContent
    }, popupProps), {
      "default": function _default() {
        return [vue.createVNode("div", {
          "ref": "triggerElementRef"
        }, [_this.renderTNode("filterIcon", defaultFilterIcon)])];
      }
    });
  }
});

exports["default"] = TableFilterController;
//# sourceMappingURL=filter-controller.js.map
