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

import { _ as _defineProperty } from '../../_chunks/dep-2db7b3ec.mjs';
import { _ as _slicedToArray } from '../../_chunks/dep-84acf3c2.mjs';
import { _ as _toConsumableArray } from '../../_chunks/dep-bf1e33c8.mjs';
import { toRefs, ref, computed, watch, createVNode, mergeProps } from 'vue';
import { SettingIcon } from '@ldesign/icons-vue-next';
import { i as intersection_1 } from '../../_chunks/dep-85ed244c.mjs';
import { Checkbox, CheckboxGroup } from '../../checkbox/index.mjs';
import { DialogPlugin } from '../../dialog/plugin.mjs';
import '../../hooks/useConfig.mjs';
import { useGlobalIcon } from '../../hooks/useGlobalIcon.mjs';
import useDefaultValue from '../../hooks/useDefaultValue.mjs';
import { getCurrentRowByKey } from '../utils.mjs';
import { Button } from '../../button/index.mjs';
import { renderTitle } from './useTableHeader.mjs';
import { useConfig } from '../../config-provider/useConfig.mjs';
import '../../_chunks/dep-255303ba.mjs';
import '../../_chunks/dep-b8d7992c.mjs';
import '../../_chunks/dep-eba0ea6e.mjs';
import '../../_chunks/dep-42cc3015.mjs';
import '../../_chunks/dep-469b4ae0.mjs';
import '../../_chunks/dep-b57180d0.mjs';
import '../../_chunks/dep-c8d7b6b4.mjs';
import '../../_chunks/dep-2bc37d52.mjs';
import '../../_chunks/dep-3da99438.mjs';
import '../../_chunks/dep-c42916c7.mjs';
import '../../_chunks/dep-bc1d737a.mjs';
import '../../_chunks/dep-4d7a675b.mjs';
import '../../_chunks/dep-5717e263.mjs';
import '../../_chunks/dep-a8bcacea.mjs';
import '../../_chunks/dep-2e7f59b1.mjs';
import '../../_chunks/dep-855db8bc.mjs';
import '../../_chunks/dep-672e7d1a.mjs';
import '../../_chunks/dep-dcc83161.mjs';
import '../../_chunks/dep-9262ee2f.mjs';
import '../../_chunks/dep-9fe2ef12.mjs';
import '../../_chunks/dep-0fc02184.mjs';
import '../../_chunks/dep-1e566bb5.mjs';
import '../../utils/withInstall.mjs';
import '../../checkbox/checkbox.mjs';
import '../../hooks/useVModel.mjs';
import '../../_chunks/dep-9a85dac1.mjs';
import '../../_chunks/dep-1372b2d3.mjs';
import '../../form/hooks.mjs';
import '../../hooks/useRipple.mjs';
import '../../utils/set-style.mjs';
import '../../hooks/useKeepAnimation.mjs';
import '../../_chunks/dep-40d69a9c.mjs';
import '../../_chunks/dep-9cf86cf1.mjs';
import '../../_chunks/dep-a87f3e4c.mjs';
import '../../_chunks/dep-1434aa15.mjs';
import '../../_chunks/dep-e5eb9f23.mjs';
import '../../_chunks/dep-42a19209.mjs';
import '../../_chunks/dep-f1994936.mjs';
import '../../_chunks/dep-345e1be3.mjs';
import '../../_common/js/global-config/default-config.mjs';
import '../../_common/js/global-config/locale/zh_CN.mjs';
import '../../_chunks/dep-9888611c.mjs';
import '../../_chunks/dep-697a1365.mjs';
import '../../_chunks/dep-095107f7.mjs';
import '../../_chunks/dep-c989dff3.mjs';
import '../../_chunks/dep-334a4f6b.mjs';
import '../../_chunks/dep-8d3e9aa3.mjs';
import '../../_chunks/dep-876fb3ec.mjs';
import '../../config-provider/type.mjs';
import '../../hooks/tnode.mjs';
import '../../_chunks/dep-406cd044.mjs';
import '../../_chunks/dep-327559e8.mjs';
import '../../_chunks/dep-b2235f26.mjs';
import '../../_chunks/dep-8b989474.mjs';
import '../../utils/render-tnode.mjs';
import '../../_chunks/dep-888e80c4.mjs';
import '../../checkbox/props.mjs';
import '../../checkbox/constants.mjs';
import '../../checkbox/group.mjs';
import '../../_chunks/dep-78847b2e.mjs';
import '../../hooks/slot.mjs';
import '../../checkbox/checkbox-group-props.mjs';
import '../../checkbox/style/css.mjs';
import '../../checkbox/type.mjs';
import '../../utils/dom.mjs';
import '../../utils/easing.mjs';
import '../../dialog/dialog.mjs';
import '../../hooks/useDestroyOnClose.mjs';
import '../../_common/js/utils/getScrollbarWidth.mjs';
import '../../_common/js/utils/helper.mjs';
import '../../_chunks/dep-60fae668.mjs';
import '../../_chunks/dep-ed5ebb35.mjs';
import '../../_chunks/dep-3f21269a.mjs';
import '../../hooks/useTeleport.mjs';
import '../../dialog/stack.mjs';
import '../../dialog/hooks.mjs';
import '../../_chunks/dep-350764bc.mjs';
import '../../_chunks/dep-0d3dd4b5.mjs';
import '../../_chunks/dep-6884627f.mjs';
import '../../dialog/props.mjs';
import '../../button/button.mjs';
import '../../loading/index.mjs';
import '../../loading/directive.mjs';
import '../../loading/plugin.mjs';
import '../../loading/loading.mjs';
import '../../loading/props.mjs';
import '../../loading/icon/gradient.mjs';
import '../../_common/js/loading/circle-adapter.mjs';
import '../../_common/js/utils/set-style.mjs';
import '../../loading/style/css.mjs';
import '../../loading/type.mjs';
import '../../button/props.mjs';
import '../../button/style/css.mjs';
import '../../button/type.mjs';
import '../../_chunks/dep-c81c0ee4.mjs';
import '../ellipsis.mjs';
import '../../_chunks/dep-9b6cf76d.mjs';
import '../../_chunks/dep-f709f709.mjs';
import '../../tooltip/index.mjs';
import '../../tooltip/tooltip.mjs';
import '../../popup/props.mjs';
import '../../popup/index.mjs';
import '../../popup/popup.mjs';
import '@popperjs/core';
import '../../popup/container.mjs';
import '../../hooks/useResizeObserver.mjs';
import '../../popup/style/css.mjs';
import '../../popup/type.mjs';
import '../../tooltip/util.mjs';
import '../../tooltip/props.mjs';
import '../../tooltip/style/css.mjs';
import '../../tooltip/type.mjs';
import './useClassName.mjs';
import './useMultiHeader.mjs';

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; }
function getColumnKeys(columns) {
  var keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : /* @__PURE__ */new Set();
  for (var i = 0, len = columns.length; i < len; i++) {
    var _col$children;
    var col = columns[i];
    col.colKey && keys.add(col.colKey);
    if ((_col$children = col.children) !== null && _col$children !== void 0 && _col$children.length) getColumnKeys(col.children, keys);
  }
  return keys;
}
function useColumnController(props, context) {
  var _useConfig = useConfig("table"),
    classPrefix = _useConfig.classPrefix,
    globalConfig = _useConfig.globalConfig;
  var _useGlobalIcon = useGlobalIcon({
      SettingIcon: SettingIcon
    }),
    SettingIcon$1 = _useGlobalIcon.SettingIcon;
  var _toRefs = toRefs(props),
    columns = _toRefs.columns,
    columnController = _toRefs.columnController,
    displayColumns = _toRefs.displayColumns,
    columnControllerVisible = _toRefs.columnControllerVisible;
  var dialogInstance = ref(null);
  var enabledColKeys = computed(function () {
    var _columnController$val;
    var arr = (((_columnController$val = columnController.value) === null || _columnController$val === void 0 ? void 0 : _columnController$val.fields) || _toConsumableArray(getColumnKeys(columns.value))).filter(function (v) {
      return v;
    });
    return new Set(arr);
  });
  var keys = _toConsumableArray(getColumnKeys(columns.value));
  var _useDefaultValue = useDefaultValue(displayColumns, props.defaultDisplayColumns || keys, props.onDisplayColumnsChange, "displayColumns"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    tDisplayColumns = _useDefaultValue2[0],
    setTDisplayColumns = _useDefaultValue2[1];
  var columnCheckboxKeys = ref(displayColumns.value || props.defaultDisplayColumns || keys);
  var checkboxOptions = computed(function () {
    return getCheckboxOptions(columns.value);
  });
  var intersectionChecked = computed(function () {
    return intersection_1(columnCheckboxKeys.value, _toConsumableArray(enabledColKeys.value));
  });
  watch([displayColumns], function (_ref) {
    var _ref2 = _slicedToArray(_ref, 1),
      val = _ref2[0];
    columnCheckboxKeys.value = val || props.defaultDisplayColumns || keys;
  });
  function getCheckboxOptions(columns2) {
    var arr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
    var _loop = function _loop(i) {
      var _item$children;
      var item = columns2[i];
      if (item.colKey) {
        arr.push({
          label: function label() {
            return renderTitle(context.slots, item, i);
          },
          value: item.colKey,
          disabled: !enabledColKeys.value.has(item.colKey)
        });
      }
      if ((_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length) getCheckboxOptions(item.children, arr);
    };
    for (var i = 0, len = columns2.length; i < len; i++) {
      _loop(i);
    }
    return arr;
  }
  var handleCheckChange = function handleCheckChange(val, ctx) {
    var _props$onColumnChange;
    columnCheckboxKeys.value = val;
    var params = {
      columns: val,
      type: ctx.type,
      currentColumn: getCurrentRowByKey(columns.value, String(ctx.current)),
      e: ctx.e
    };
    (_props$onColumnChange = props.onColumnChange) === null || _props$onColumnChange === void 0 ? void 0 : _props$onColumnChange.call(props, params);
  };
  var handleClickAllShowColumns = function handleClickAllShowColumns(checked, ctx) {
    if (checked) {
      var _checkboxOptions$valu, _props$onColumnChange2;
      var newData = ((_checkboxOptions$valu = checkboxOptions.value) === null || _checkboxOptions$valu === void 0 ? void 0 : _checkboxOptions$valu.map(function (t) {
        return t.value;
      })) || [];
      columnCheckboxKeys.value = newData;
      (_props$onColumnChange2 = props.onColumnChange) === null || _props$onColumnChange2 === void 0 ? void 0 : _props$onColumnChange2.call(props, {
        type: "check",
        columns: newData,
        e: ctx.e
      });
    } else {
      var _props$onColumnChange3;
      var disabledColKeys = checkboxOptions.value.filter(function (t) {
        return t.disabled;
      }).map(function (t) {
        return t.value;
      });
      columnCheckboxKeys.value = disabledColKeys;
      (_props$onColumnChange3 = props.onColumnChange) === null || _props$onColumnChange3 === void 0 ? void 0 : _props$onColumnChange3.call(props, {
        type: "uncheck",
        columns: disabledColKeys,
        e: ctx.e
      });
    }
  };
  var handleToggleColumnController = function handleToggleColumnController() {
    var _columnController$val4;
    dialogInstance.value = DialogPlugin.confirm(_objectSpread({
      header: globalConfig.value.columnConfigTitleText,
      body: function body() {
        var _columnController$val2, _columnController$val3;
        var widthMode = ((_columnController$val2 = columnController.value) === null || _columnController$val2 === void 0 ? void 0 : _columnController$val2.displayType) === "fixed-width" ? "fixed" : "auto";
        var checkedLength = intersectionChecked.value.length;
        var isCheckedAll = checkedLength === enabledColKeys.value.size;
        var isIndeterminate = checkedLength > 0 && checkedLength < enabledColKeys.value.size;
        var defaultNode = createVNode("div", {
          "class": ["".concat(classPrefix.value, "-table__column-controller"), "".concat(classPrefix.value, "-table__column-controller--").concat(widthMode)]
        }, [createVNode("div", {
          "class": "".concat(classPrefix.value, "-table__column-controller-body")
        }, [createVNode("p", {
          "class": "".concat(classPrefix.value, "-table__column-controller-desc")
        }, [globalConfig.value.columnConfigDescriptionText]), createVNode("div", {
          "class": "".concat(classPrefix.value, "-table__column-controller-block")
        }, [createVNode(Checkbox, {
          "indeterminate": isIndeterminate,
          "checked": isCheckedAll,
          "onChange": handleClickAllShowColumns
        }, {
          "default": function _default() {
            return [globalConfig.value.selectAllText];
          }
        })]), createVNode("div", {
          "class": "".concat(classPrefix.value, "-table__column-controller-block")
        }, [createVNode(CheckboxGroup, mergeProps({
          "options": checkboxOptions.value
        }, ((_columnController$val3 = columnController.value) === null || _columnController$val3 === void 0 ? void 0 : _columnController$val3.checkboxProps) || {}, {
          "value": columnCheckboxKeys.value,
          "onChange": handleCheckChange
        }), null)])])]);
        return defaultNode;
      },
      confirmBtn: globalConfig.value.confirmText,
      cancelBtn: globalConfig.value.cancelText,
      width: 612,
      onConfirm: function onConfirm() {
        setTDisplayColumns(_toConsumableArray(columnCheckboxKeys.value));
        if (columnControllerVisible.value === void 0) {
          dialogInstance.value.hide();
        } else {
          var _props$onColumnContro;
          (_props$onColumnContro = props.onColumnControllerVisibleChange) === null || _props$onColumnContro === void 0 ? void 0 : _props$onColumnContro.call(props, false, {
            trigger: "cancel"
          });
          context.emit("update:columnControllerVisible", false);
        }
      },
      onClose: function onClose() {
        if (columnControllerVisible.value === void 0) {
          dialogInstance.value.hide();
        } else {
          var _props$onColumnContro2;
          (_props$onColumnContro2 = props.onColumnControllerVisibleChange) === null || _props$onColumnContro2 === void 0 ? void 0 : _props$onColumnContro2.call(props, false, {
            trigger: "confirm"
          });
          context.emit("update:columnControllerVisible", false);
        }
      }
    }, ((_columnController$val4 = columnController.value) === null || _columnController$val4 === void 0 ? void 0 : _columnController$val4.dialogProps) || {}));
  };
  watch([columnControllerVisible], function (_ref3) {
    var _ref4 = _slicedToArray(_ref3, 1),
      visible = _ref4[0];
    if (visible === void 0) return;
    if (dialogInstance.value) visible ? dialogInstance.value.show() : dialogInstance.value.hide();else visible && handleToggleColumnController();
  }, {
    immediate: true
  });
  var renderColumnController = function renderColumnController() {
    var _props$columnControll;
    var isColumnController = !!(columnController.value && Object.keys(columnController.value).length);
    var placement = isColumnController ? columnController.value.placement || "top-right" : "";
    if (isColumnController && columnController.value.hideTriggerButton) return null;
    var classes = ["".concat(classPrefix.value, "-table__column-controller-trigger"), _defineProperty({}, "".concat(classPrefix.value, "-align-").concat(placement), !!placement)];
    return createVNode("div", {
      "class": classes
    }, [createVNode(Button, mergeProps({
      "theme": "default",
      "variant": "outline",
      "onClick": handleToggleColumnController,
      "content": globalConfig.value.columnConfigButtonText
    }, (_props$columnControll = props.columnController) === null || _props$columnControll === void 0 ? void 0 : _props$columnControll.buttonProps), {
      icon: function icon() {
        return createVNode(SettingIcon$1, null, null);
      }
    })]);
  };
  return {
    tDisplayColumns: tDisplayColumns,
    columnCheckboxKeys: columnCheckboxKeys,
    checkboxOptions: checkboxOptions,
    renderColumnController: renderColumnController
  };
}

export { useColumnController as default, getColumnKeys };
//# sourceMappingURL=useColumnController.mjs.map
