"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const aria = require("../../../constants/aria.js");
const event = require("../../../utils/dom/event.js");
const types = require("../../../utils/types.js");
const index$5 = require("../../../hooks/use-locale/index.js");
const index$6 = require("../../../hooks/use-namespace/index.js");
const index$1 = require("../../icon/index.js");
const content = require("../../tooltip/src/content.js");
const index$2 = require("../../tooltip/index.js");
const index$3 = require("../../scrollbar/index.js");
const index$4 = require("../../checkbox/index.js");
const index = require("../../../../../@element-plus/icons-vue/dist/index.js");
const vue = require("vue");
var filter_panel_vue_vue_type_script_lang_default = vue.defineComponent({
  name: "ElTableFilterPanel",
  components: {
    ElCheckbox: index$4.ElCheckbox,
    ElCheckboxGroup: index$4.ElCheckboxGroup,
    ElScrollbar: index$3.ElScrollbar,
    ElTooltip: index$2.ElTooltip,
    ElIcon: index$1.ElIcon,
    ArrowDown: index.ArrowDown,
    ArrowUp: index.ArrowUp
  },
  props: {
    placement: {
      type: String,
      default: "bottom-start"
    },
    store: { type: Object },
    column: { type: Object },
    upDataColumn: { type: Function },
    appendTo: content.useTooltipContentProps.appendTo
  },
  setup(props) {
    const instance = vue.getCurrentInstance();
    const { t } = index$5.useLocale();
    const ns = index$6.useNamespace("table-filter");
    const parent = instance == null ? void 0 : instance.parent;
    if (props.column && !parent.filterPanels.value[props.column.id]) parent.filterPanels.value[props.column.id] = instance;
    const tooltipRef = vue.ref(null);
    const rootRef = vue.ref(null);
    const checkedIndex = vue.ref(0);
    const filters = vue.computed(() => {
      return props.column && props.column.filters;
    });
    const filterClassName = vue.computed(() => {
      if (props.column && props.column.filterClassName) return `${ns.b()} ${props.column.filterClassName}`;
      return ns.b();
    });
    const filterValue = vue.computed({
      get: () => {
        var _a;
        return (((_a = props.column) == null ? void 0 : _a.filteredValue) || [])[0];
      },
      set: (value) => {
        if (filteredValue.value) if (!types.isPropAbsent(value)) filteredValue.value.splice(0, 1, value);
        else filteredValue.value.splice(0, 1);
      }
    });
    const filteredValue = vue.computed({
      get() {
        if (props.column) return props.column.filteredValue || [];
        return [];
      },
      set(value) {
        var _a;
        if (props.column) (_a = props.upDataColumn) == null ? void 0 : _a.call(props, "filteredValue", value);
      }
    });
    const multiple = vue.computed(() => {
      if (props.column) return props.column.filterMultiple;
      return true;
    });
    const isActive = (filter) => {
      return filter.value === filterValue.value;
    };
    const hidden = () => {
      var _a;
      (_a = tooltipRef.value) == null ? void 0 : _a.onClose();
    };
    const handleConfirm = () => {
      confirmFilter(filteredValue.value);
      hidden();
    };
    const handleReset = () => {
      filteredValue.value = [];
      confirmFilter(filteredValue.value);
      hidden();
    };
    const handleSelect = (_filterValue, index2) => {
      filterValue.value = _filterValue;
      checkedIndex.value = index2;
      if (!types.isPropAbsent(_filterValue)) confirmFilter(filteredValue.value);
      else confirmFilter([]);
      hidden();
    };
    const confirmFilter = (filteredValue2) => {
      var _a, _b;
      (_a = props.store) == null ? void 0 : _a.commit("filterChange", {
        column: props.column,
        values: filteredValue2
      });
      (_b = props.store) == null ? void 0 : _b.updateAllSelected();
    };
    const handleShowTooltip = () => {
      var _a, _b;
      (_a = rootRef.value) == null ? void 0 : _a.focus();
      !multiple.value && initCheckedIndex();
      if (props.column) (_b = props.upDataColumn) == null ? void 0 : _b.call(props, "filterOpened", true);
    };
    const handleHideTooltip = () => {
      var _a;
      if (props.column) (_a = props.upDataColumn) == null ? void 0 : _a.call(props, "filterOpened", false);
    };
    const initCheckedIndex = () => {
      if (types.isPropAbsent(filterValue)) {
        checkedIndex.value = 0;
        return;
      }
      const idx = (filters.value || []).findIndex((item) => {
        return item.value === filterValue.value;
      });
      checkedIndex.value = idx >= 0 ? idx + 1 : 0;
    };
    const handleKeydown = (event$1) => {
      var _a, _b;
      const code = event.getEventCode(event$1);
      const len = (filters.value ? filters.value.length : 0) + 1;
      let index2 = checkedIndex.value;
      let isPreventDefault = true;
      switch (code) {
        case aria.EVENT_CODE.down:
        case aria.EVENT_CODE.right:
          index2 = (index2 + 1) % len;
          break;
        case aria.EVENT_CODE.up:
        case aria.EVENT_CODE.left:
          index2 = (index2 - 1 + len) % len;
          break;
        case aria.EVENT_CODE.tab:
          hidden();
          isPreventDefault = false;
          break;
        case aria.EVENT_CODE.enter:
        case aria.EVENT_CODE.space:
          if (index2 === 0) handleSelect(null, 0);
          else {
            const item = (filters.value || [])[index2 - 1];
            item.value && handleSelect(item.value, index2);
          }
          break;
        default:
          isPreventDefault = false;
          break;
      }
      isPreventDefault && event$1.preventDefault();
      checkedIndex.value = index2;
      (_b = (_a = rootRef.value) == null ? void 0 : _a.querySelector(`.${ns.e("list-item")}:nth-child(${index2 + 1})`)) == null ? void 0 : _b.focus();
    };
    return {
      multiple,
      filterClassName,
      filteredValue,
      filterValue,
      filters,
      handleConfirm,
      handleReset,
      handleSelect,
      isPropAbsent: types.isPropAbsent,
      isActive,
      t,
      ns,
      tooltipRef,
      rootRef,
      checkedIndex,
      handleShowTooltip,
      handleHideTooltip,
      handleKeydown
    };
  }
});
exports.default = filter_panel_vue_vue_type_script_lang_default;
