/**
 * 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 iconsVue = require('@ldesign/icons-vue');
var isEmpty = require('lodash/isEmpty');
var lowerFirst = require('lodash/lowerFirst');
var log = require('../../../chunks/log.js');
var components_popup_index = require('../../popup/index.js');
var components_checkbox_index = require('../../checkbox/index.js');
var components_radio_index = require('../../radio/index.js');
var components_input_index = require('../../input/index.js');
var components_button_index = require('../../button/index.js');
var hooks_lnode = require('../../../hooks/lnode.js');
var hooks_useGlobalIcon = require('../../../hooks/use-global-icon.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/map-props.js');
require('vue');
require('../../../utils/with-install.js');
require('lodash');
require('../../../config.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../../chunks/set-style.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../utils/easing.js');
require('../../../utils/render-node.js');
require('../../../utils/helper.js');
require('lodash/camelCase');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../../utils/event.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../radio/radio.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('lodash/isNumber');
require('../../radio/radio-button.js');
require('../../input/input.js');
require('lodash/kebabCase');
require('../../../chunks/helper2.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../loading/plugin.js');
require('../../../utils/ripple.js');
require('../../button/props.js');
require('lodash/isFunction');
require('../../../hooks/render-lnode.js');

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

var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);
var lowerFirst__default = /*#__PURE__*/_interopDefaultLegacy(lowerFirst);

var TableFilterController = VueCompositionAPI.defineComponent({
  name: "TableFilterController",
  props: {
    column: Object,
    tFilterValue: Object,
    innerFilterValue: Object,
    tableFilterClasses: Object,
    isFocusClass: String,
    primaryTableElement: {}
  },
  setup(props, _ref) {
    let {
      emit
    } = _ref;
    const triggerElementRef = VueCompositionAPI.ref(null);
    const renderLNode = hooks_lnode.useLNodeDefault();
    const {
      t,
      global
    } = components_configProvider_useConfig.useConfig("table");
    const {
      FilterIcon
    } = hooks_useGlobalIcon.useGlobalIcon({
      FilterIcon: iconsVue.FilterIcon
    });
    const filterPopupVisible = VueCompositionAPI.ref(false);
    const onFilterPopupVisibleChange = visible => {
      filterPopupVisible.value = visible;
      emit("visible-change", visible);
    };
    return {
      t,
      global,
      FilterIcon,
      filterPopupVisible,
      triggerElementRef,
      renderLNode,
      onFilterPopupVisibleChange
    };
  },
  render(h) {
    var _this$tFilterValue;
    const getFilterContent = (h2, column2) => {
      var _column2$filter, _column2$filter2, _this$innerFilterValu;
      const types = ["single", "multiple", "input"];
      if (column2.type && !types.includes(column2.filter.type)) {
        log.log.error("Table", `filter.type must be the following: ${JSON.stringify(types)}`);
        return;
      }
      if (!column2.filter.type && !column2.filter.component) {
        log.log.error("Table", "both filter.type and filter.component can not be empty.");
        return;
      }
      const component = {
        single: components_radio_index.RadioGroup,
        multiple: components_checkbox_index.CheckboxGroup,
        input: components_input_index.Input
      }[column2.filter.type] || column2.filter.component;
      if (!component && !column2.filter.component) return;
      const filterComponentProps = {
        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 = val => {
          this.$emit("inner-filter-change", val, column2);
        };
      }
      const on = {
        change: val => {
          this.$emit("inner-filter-change", val, column2);
        }
      };
      if (column2.filter.confirmEvents) {
        column2.filter.confirmEvents.forEach(event => {
          const pureEvent = lowerFirst__default["default"](event.replace("on", ""));
          on[pureEvent] = () => {
            this.$emit("confirm", column2);
            this.filterPopupVisible = false;
          };
        });
      }
      const wrapperListeners = {};
      if (column2.filter.showConfirmAndReset) wrapperListeners.click = e => e.stopPropagation();
      const renderComponent = () => {
        var _this$innerFilterValu2;
        if (!component) return null;
        const isVueComponent = component.install && component.component;
        if (typeof component === "function" && !isVueComponent) {
          return component((v, b) => {
            const tProps = typeof b === "object" && "attrs" in b ? b.attrs : {};
            return h2(v, {
              props: {
                ...filterComponentProps,
                ...tProps
              },
              on
            });
          });
        }
        return h(component, helper.helper([{
          "attrs": {
            "value": (_this$innerFilterValu2 = this.innerFilterValue) === null || _this$innerFilterValu2 === void 0 ? void 0 : _this$innerFilterValu2[column2.colKey]
          }
        }, {
          "props": {
            ...filterComponentProps
          }
        }, {}, {
          "on": {
            ...on
          }
        }]));
      };
      return h("div", helper.helper([{
        "class": this.tableFilterClasses.contentInner
      }, {
        "on": wrapperListeners
      }]), [renderComponent()]);
    };
    const getBottomButtons = (h2, column2) => {
      if (!column2.filter.showConfirmAndReset) return;
      return h("div", {
        "class": this.tableFilterClasses.bottomButtons
      }, [h(components_button_index.Button, {
        "attrs": {
          "theme": "default",
          "size": "small"
        },
        "on": {
          "click": () => {
            this.$emit("reset", column2);
            this.filterPopupVisible = false;
          }
        }
      }, [this.global.resetText]), h(components_button_index.Button, {
        "attrs": {
          "theme": "primary",
          "size": "small"
        },
        "on": {
          "click": () => {
            this.$emit("confirm", column2);
            this.filterPopupVisible = false;
          }
        }
      }, [this.global.confirmText])]);
    };
    const {
      column,
      FilterIcon
    } = this;
    if (!column.filter || column.filter && !Object.keys(column.filter).length) return null;
    const defaultFilterIcon = this.t(this.global.filterIcon) || h(FilterIcon);
    const filterValue = (_this$tFilterValue = this.tFilterValue) === null || _this$tFilterValue === void 0 ? void 0 : _this$tFilterValue[column.colKey];
    const isObjectTrue = typeof filterValue === "object" && !isEmpty__default["default"](filterValue);
    const isValueTrue = filterValue && typeof filterValue !== "object";
    return h(components_popup_index.Popup, helper.helper([{
      "attrs": {
        "attach": this.primaryTableElement ? () => this.primaryTableElement : void 0,
        "visible": this.filterPopupVisible,
        "destroyOnClose": true,
        "trigger": "click",
        "placement": "bottom",
        "showArrow": true,
        "overlayClassName": this.tableFilterClasses.popup
      }
    }, {
      "on": {
        "visible-change": val => this.onFilterPopupVisibleChange(val)
      }
    }, {
      "class": [this.tableFilterClasses.icon, {
        [this.isFocusClass]: isObjectTrue || isValueTrue
      }],
      "attrs": {
        "content": () => h("div", {
          "class": this.tableFilterClasses.popupContent
        }, [getFilterContent(h, column), getBottomButtons(h, column)])
      }
    }]), [h("div", {
      "ref": "triggerElementRef"
    }, [this.renderLNode("filterIcon", defaultFilterIcon)])]);
  }
});

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