/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import { h as helper } from '../../_chunks/dep-1ec74b8b.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { toRefs, computed, ref, h } from '@vue/composition-api';
import { SettingIcon } from 'tdesign-icons-vue';
import intersection from 'lodash/intersection';
import { Checkbox, CheckboxGroup } from '../../checkbox/index.js';
import { DialogPlugin } from '../../dialog/plugin.js';
import { useTNodeDefault } from '../../hooks/tnode.js';
import { renderTitle } from './useTableHeader.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../checkbox/group.js';
import '@babel/runtime/helpers/typeof';
import 'vue';
import '../../config.js';
import '../../checkbox/checkbox.js';
import '../../utils/render-tnode.js';
import 'lodash/isObject';
import '../../utils/classnames.js';
import '../../checkbox/props.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../../utils/event.js';
import '../../checkbox/checkbox-group-props.js';
import '../../utils/map-props.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../../checkbox/type.js';
import '../../dialog/dialog.js';
import 'lodash/throttle';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../loading/icon/gradient.js';
import '../../_common/js/loading/circle-adapter.js';
import '../../_common/js/utils/set-style.js';
import '../../_common/js/utils/helper.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/props.js';
import '../../utils/ripple.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 '../../config-provider/type.js';
import '../../utils/mixins.js';
import '../../button/type.js';
import '../../dialog/actions.js';
import '../../dialog/props.js';
import '../../hooks/useDestroyOnClose.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/kebabCase';
import '../../hooks/render-tnode.js';
import '../../_common/js/log/index.js';
import '../../_common/js/log/log.js';
import './useMultiHeader.js';
import './useClassName.js';
import 'lodash/cloneDeep';

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] : [];

  for (var i = 0, len = columns.length; i < len; i++) {
    var _col$children;

    var col = columns[i];
    col.colKey && keys.push(col.colKey);

    if ((_col$children = col.children) !== null && _col$children !== void 0 && _col$children.length) {
      keys = keys.concat(getColumnKeys(col.children, _toConsumableArray(keys)));
    }
  }

  return keys;
}
function useColumnController(props, context) {
  var renderTNode = useTNodeDefault();

  var _useConfig = useConfig(),
      classPrefix = _useConfig.classPrefix;

  var _toRefs = toRefs(props),
      columns = _toRefs.columns,
      columnController = _toRefs.columnController;

  var enabledColKeys = computed(function () {
    var _columnController$val;

    var arr = (((_columnController$val = columnController.value) === null || _columnController$val === void 0 ? void 0 : _columnController$val.fields) || _toConsumableArray(new Set(getColumnKeys(columns.value)))).filter(function (v) {
      return v;
    });

    return new Set(arr);
  });

  var keys = _toConsumableArray(new Set(getColumnKeys(columns.value)));

  var displayColumnKeys = ref(keys);
  var columnCheckboxKeys = ref(keys);
  var checkboxOptions = computed(function () {
    return getCheckboxOptions(columns.value);
  });
  var intersectionChecked = computed(function () {
    return intersection(columnCheckboxKeys.value, _toConsumableArray(enabledColKeys.value));
  });

  function getCheckboxOptions(columns2) {
    var arr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

    var _loop = function _loop(i, len) {
      var _item$children;

      var item = columns2[i];

      if (item.colKey) {
        arr.push({
          label: function label() {
            return renderTitle(h, 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, len);
    }

    return arr;
  }

  var handleCheckChange = function handleCheckChange(val) {
    var _props$onColumnChange;

    columnCheckboxKeys.value = val;
    var params = {
      columns: val
    };
    (_props$onColumnChange = props.onColumnChange) === null || _props$onColumnChange === void 0 ? void 0 : _props$onColumnChange.call(props, params);
    context.emit("column-change", params);
  };

  var handleClickAllShowColumns = function handleClickAllShowColumns(checked) {
    if (checked) {
      var _columns$value, _props$onColumnChange2;

      var newData = ((_columns$value = columns.value) === null || _columns$value === void 0 ? void 0 : _columns$value.map(function (t) {
        return t.colKey;
      })) || [];
      columnCheckboxKeys.value = newData;
      (_props$onColumnChange2 = props.onColumnChange) === null || _props$onColumnChange2 === void 0 ? void 0 : _props$onColumnChange2.call(props, {
        type: "check",
        columns: newData
      });
      context.emit("column-change", {
        type: "check",
        columns: newData
      });
    } 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
      });
      context.emit("column-change", {
        type: "uncheck",
        columns: disabledColKeys
      });
    }
  };

  var handleToggleColumnController = function handleToggleColumnController() {
    var _columnController$val4;

    var dialogInstance = DialogPlugin.confirm(_objectSpread({
      header: "\u8868\u683C\u5217\u914D\u7F6E",
      body: function body(h2) {
        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 prefix = classPrefix.value;
        var defaultNode = h("div", {
          "class": ["".concat(prefix, "-table__column-controller"), "".concat(prefix, "-table__column-controller--").concat(widthMode)]
        }, [h("div", {
          "class": "".concat(prefix, "-table__column-controller-body")
        }, [h("p", {
          "class": "".concat(prefix, "-table__column-controller-desc")
        }, ["\u8BF7\u9009\u62E9\u9700\u8981\u5728\u8868\u683C\u4E2D\u663E\u793A\u7684\u6570\u636E\u5217"]), h("div", {
          "class": "".concat(prefix, "-table__column-controller-block")
        }, [h(Checkbox, {
          "attrs": {
            "indeterminate": isIndeterminate,
            "checked": isCheckedAll
          },
          "on": {
            "change": handleClickAllShowColumns
          }
        }, ["\u5168\u9009"])]), h("div", {
          "class": "".concat(prefix, "-table__column-controller-block")
        }, [h(CheckboxGroup, helper([{
          "attrs": {
            "options": checkboxOptions.value
          }
        }, {
          "props": (_columnController$val3 = columnController.value) === null || _columnController$val3 === void 0 ? void 0 : _columnController$val3.checkboxProps
        }, {
          "attrs": {
            "value": columnCheckboxKeys.value
          },
          "on": {
            "change": handleCheckChange
          }
        }]))])])]);
        return renderTNode("columnControllerContent", defaultNode);
      },
      confirmBtn: "\u786E\u8BA4",
      cancelBtn: "\u53D6\u6D88",
      width: 612,
      onConfirm: function onConfirm() {
        displayColumnKeys.value = _toConsumableArray(columnCheckboxKeys.value);
        dialogInstance.hide();
      },
      onClose: function onClose() {
        dialogInstance.hide();
      }
    }, ((_columnController$val4 = columnController.value) === null || _columnController$val4 === void 0 ? void 0 : _columnController$val4.dialogProps) || {}));
  };

  var renderColumnController = function renderColumnController(h2) {
    return h("div", {
      "class": "".concat(classPrefix.value, "-table__column-controller")
    }, [h("t-button", {
      "attrs": {
        "theme": "default",
        "variant": "outline"
      },
      "on": {
        "click": handleToggleColumnController
      }
    }, [h(SettingIcon, {
      "slot": "icon"
    }), "\u5217\u914D\u7F6E"])]);
  };

  return {
    displayColumnKeys: displayColumnKeys,
    columnCheckboxKeys: columnCheckboxKeys,
    checkboxOptions: checkboxOptions,
    renderColumnController: renderColumnController
  };
}

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