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

'use strict';

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

var helper = require('../../_chunks/dep-33ee4bce.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var VueCompositionAPI = require('@vue/composition-api');
var tdesignIconsVue = require('tdesign-icons-vue');
var intersection = require('lodash/intersection');
var checkbox_index = require('../../checkbox/index.js');
var dialog_plugin = require('../../dialog/plugin.js');
var hooks_tnode = require('../../hooks/tnode.js');
var table_hooks_useTableHeader = require('./useTableHeader.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../checkbox/group.js');
require('@babel/runtime/helpers/typeof');
require('vue');
require('../../config.js');
require('../../checkbox/checkbox.js');
require('../../utils/render-tnode.js');
require('lodash/isObject');
require('../../utils/classnames.js');
require('../../checkbox/props.js');
require('../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../../utils/event.js');
require('../../checkbox/checkbox-group-props.js');
require('../../utils/map-props.js');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../dialog/dialog.js');
require('lodash/throttle');
require('../../button/index.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/set-style.js');
require('../../_common/js/utils/helper.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/plugin.js');
require('../../button/props.js');
require('../../utils/ripple.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('../../utils/mixins.js');
require('../../dialog/actions.js');
require('../../dialog/props.js');
require('../../hooks/useDestroyOnClose.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/kebabCase');
require('../../hooks/render-tnode.js');
require('../../_common/js/log/log.js');
require('./useMultiHeader.js');
require('./useClassName.js');
require('lodash/cloneDeep');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection);

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; }
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__default["default"](keys)));
    }
  }

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

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

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

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

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

    return new Set(arr);
  });

  var keys = _toConsumableArray__default["default"](new Set(getColumnKeys(columns.value)));

  var displayColumnKeys = VueCompositionAPI.ref(keys);
  var columnCheckboxKeys = VueCompositionAPI.ref(keys);
  var checkboxOptions = VueCompositionAPI.computed(function () {
    return getCheckboxOptions(columns.value);
  });
  var intersectionChecked = VueCompositionAPI.computed(function () {
    return intersection__default["default"](columnCheckboxKeys.value, _toConsumableArray__default["default"](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 table_hooks_useTableHeader.renderTitle(VueCompositionAPI.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);
    }

    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 = dialog_plugin.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 = VueCompositionAPI.h("div", {
          "class": ["".concat(prefix, "-table__column-controller"), "".concat(prefix, "-table__column-controller--").concat(widthMode)]
        }, [VueCompositionAPI.h("div", {
          "class": "".concat(prefix, "-table__column-controller-body")
        }, [VueCompositionAPI.h("p", {
          "class": "".concat(prefix, "-table__column-controller-desc")
        }, ["\u8BF7\u9009\u62E9\u9700\u8981\u5728\u8868\u683C\u4E2D\u663E\u793A\u7684\u6570\u636E\u5217"]), VueCompositionAPI.h("div", {
          "class": "".concat(prefix, "-table__column-controller-block")
        }, [VueCompositionAPI.h(checkbox_index.Checkbox, {
          "attrs": {
            "indeterminate": isIndeterminate,
            "checked": isCheckedAll
          },
          "on": {
            "change": handleClickAllShowColumns
          }
        }, ["\u5168\u9009"])]), VueCompositionAPI.h("div", {
          "class": "".concat(prefix, "-table__column-controller-block")
        }, [VueCompositionAPI.h(checkbox_index.CheckboxGroup, helper.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__default["default"](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 VueCompositionAPI.h("div", {
      "class": "".concat(classPrefix.value, "-table__column-controller")
    }, [VueCompositionAPI.h("t-button", {
      "attrs": {
        "theme": "default",
        "variant": "outline"
      },
      "on": {
        "click": handleToggleColumnController
      }
    }, [VueCompositionAPI.h(tdesignIconsVue.SettingIcon, {
      "slot": "icon"
    }), "\u5217\u914D\u7F6E"])]);
  };

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

exports["default"] = useColumnController;
exports.getColumnKeys = getColumnKeys;
//# sourceMappingURL=useColumnController.js.map
