/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { h as helper } from '../../_chunks/dep-a4bd011a.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, ref, computed, watch, h } from '@vue/composition-api';
import intersection from 'lodash/intersection';
import get from 'lodash/get';
import isFunction from 'lodash/isFunction';
import useDefaultValue from '../../hooks/useDefaultValue.js';
import { isRowSelectedDisabled } from '../utils.js';
import { Checkbox } from '../../checkbox/index.js';
import { Radio } from '../../radio/index.js';
import '../../_common/js/log/index.js';
import log from '../../_common/js/log/log.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/isObject';
import '../../checkbox/group.js';
import '../../checkbox/checkbox.js';
import 'vue';
import '../../utils/render-tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../checkbox/props.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../../utils/event.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 '../../checkbox/checkbox-group-props.js';
import '../../utils/map-props.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../../checkbox/type.js';
import '../../radio/radio.js';
import '../../radio/props.js';
import '../../radio/group.js';
import 'lodash/isString';
import 'lodash/isNumber';
import '../../radio/radio-group-props.js';
import '../../radio/radio-button.js';
import '../../radio/type.js';

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 useRowSelect(props, tableSelectedClasses) {
  var _toRefs = toRefs(props),
      selectedRowKeys = _toRefs.selectedRowKeys,
      columns = _toRefs.columns,
      data = _toRefs.data,
      rowKey = _toRefs.rowKey;

  var selectedRowClassNames = ref();

  var _useDefaultValue = useDefaultValue(selectedRowKeys, props.defaultSelectedRowKeys || [], props.onSelectChange, "selectedRowKeys", "select-change"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
      tSelectedRowKeys = _useDefaultValue2[0],
      setTSelectedRowKeys = _useDefaultValue2[1];

  var selectedRowDataMap = ref( /* @__PURE__ */new Map());
  var selectColumn = computed(function () {
    return props.columns.find(function (_ref) {
      var type = _ref.type;
      return ["multiple", "single"].includes(type);
    });
  });
  var canSelectedRows = computed(function () {
    return data.value.filter(function (row, rowIndex) {
      return !isDisabled(row, rowIndex);
    });
  });
  var intersectionKeys = computed(function () {
    return intersection(tSelectedRowKeys.value, canSelectedRows.value.map(function (t) {
      return get(t, props.rowKey || "id");
    }));
  });
  watch([data, columns, tSelectedRowKeys, selectColumn, rowKey], function () {
    var _selectColumn$value;

    var disabledRowFunc = function disabledRowFunc(p) {
      return selectColumn.value.disabled(p) ? tableSelectedClasses.disabled : "";
    };

    var disabledRowClass = (_selectColumn$value = selectColumn.value) !== null && _selectColumn$value !== void 0 && _selectColumn$value.disabled ? disabledRowFunc : void 0;
    var selected = new Set(tSelectedRowKeys.value);

    var selectedRowClassFunc = function selectedRowClassFunc(_ref2) {
      var row = _ref2.row;
      var rowId = get(row, props.rowKey || "id");
      return selected.has(rowId) ? tableSelectedClasses.selected : "";
    };

    var selectedRowClass = selected.size ? selectedRowClassFunc : void 0;
    selectedRowClassNames.value = [disabledRowClass, selectedRowClass].filter(function (v) {
      return v;
    });
  }, {
    immediate: true
  });

  function isDisabled(row, rowIndex) {
    return isRowSelectedDisabled(selectColumn.value, row, rowIndex);
  }

  function getSelectedHeader(h2) {
    return function () {
      return h(Checkbox, {
        "attrs": {
          "checked": intersectionKeys.value.length !== 0 && canSelectedRows.value.length !== 0 && intersectionKeys.value.length === canSelectedRows.value.length,
          "indeterminate": intersectionKeys.value.length > 0 && intersectionKeys.value.length < canSelectedRows.value.length,
          "disabled": !canSelectedRows.value.length
        },
        "on": _objectSpread({}, {
          change: handleSelectAll
        })
      });
    };
  }

  function renderSelectCell(h2, p) {
    var column = p.col,
        _p$row = p.row,
        row = _p$row === void 0 ? {} : _p$row,
        rowIndex = p.rowIndex;
    var checked = tSelectedRowKeys.value.includes(get(row, props.rowKey || "id"));
    var disabled = typeof column.disabled === "function" ? column.disabled({
      row: row,
      rowIndex: rowIndex
    }) : column.disabled;
    var checkProps = isFunction(column.checkProps) ? column.checkProps({
      row: row,
      rowIndex: rowIndex
    }) : column.checkProps;
    var selectBoxProps = {
      props: _objectSpread({
        checked: checked,
        disabled: disabled
      }, checkProps),
      on: {
        click: function click(e) {
          e === null || e === void 0 ? void 0 : e.stopPropagation();
        },
        change: function change() {
          return handleSelectChange(row);
        }
      }
    };
    if (column.type === "single") return h(Radio, helper([{}, selectBoxProps]));

    if (column.type === "multiple") {
      var _props$indeterminateS;

      var isIndeterminate = (_props$indeterminateS = props.indeterminateSelectedRowKeys) !== null && _props$indeterminateS !== void 0 && _props$indeterminateS.length ? props.indeterminateSelectedRowKeys.includes(get(row, props.rowKey)) : false;
      return h(Checkbox, helper([{
        "attrs": {
          "indeterminate": isIndeterminate
        }
      }, selectBoxProps]));
    }

    return null;
  }

  function handleSelectChange() {
    var row = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var selectedRowKeys2 = _toConsumableArray(tSelectedRowKeys.value);

    var reRowKey = props.rowKey || "id";
    var id = get(row, reRowKey);
    selectedRowDataMap.value.set(id, row);
    var selectedRowIndex = selectedRowKeys2.indexOf(id);
    var isExisted = selectedRowIndex !== -1;

    if (selectColumn.value.type === "multiple") {
      isExisted ? selectedRowKeys2.splice(selectedRowIndex, 1) : selectedRowKeys2.push(id);
    } else if (selectColumn.value.type === "single") {
      selectedRowKeys2 = !isExisted ? [id] : [];
    } else {
      log.warn("Table", "`column.type` must be one of `multiple` and `single`");
      return;
    }

    setTSelectedRowKeys(selectedRowKeys2, {
      selectedRowData: selectedRowKeys2.map(function (t) {
        return selectedRowDataMap.value.get(t);
      }),
      currentRowKey: id,
      currentRowData: row,
      type: isExisted ? "uncheck" : "check"
    });
  }

  function handleSelectAll(checked) {
    var _selectedRowKeys$valu;

    var reRowKey = props.rowKey || "id";
    var canSelectedRowKeys = canSelectedRows.value.map(function (record) {
      return get(record, reRowKey);
    });
    var disabledSelectedRowKeys = ((_selectedRowKeys$valu = selectedRowKeys.value) === null || _selectedRowKeys$valu === void 0 ? void 0 : _selectedRowKeys$valu.filter(function (id) {
      return !canSelectedRowKeys.includes(id);
    })) || [];
    var allIds = checked ? [].concat(_toConsumableArray(disabledSelectedRowKeys), _toConsumableArray(canSelectedRowKeys)) : _toConsumableArray(disabledSelectedRowKeys);
    setTSelectedRowKeys(allIds, {
      selectedRowData: checked ? allIds.map(function (t) {
        return selectedRowDataMap.value.get(t);
      }) : [],
      type: checked ? "check" : "uncheck",
      currentRowKey: "CHECK_ALL_BOX"
    });
  }

  function formatToRowSelectColumn(col) {
    var isSelection = ["multiple", "single"].includes(col.type);
    if (!isSelection) return col;
    return _objectSpread(_objectSpread({}, col), {}, {
      width: col.width || 64,
      className: [tableSelectedClasses.checkCell, col.className],
      cell: function cell(h2, p) {
        return renderSelectCell(h2, p);
      },
      title: col.type === "multiple" ? getSelectedHeader(h) : ""
    });
  }

  watch([data, rowKey], function (_ref3) {
    var _ref4 = _slicedToArray(_ref3, 2),
        data2 = _ref4[0],
        rowKey2 = _ref4[1];

    for (var i = 0, len = data2.length; i < len; i++) {
      selectedRowDataMap.value.set(get(data2[i], rowKey2 || "id"), data2[i]);
    }
  }, {
    immediate: true
  });
  return {
    selectedRowClassNames: selectedRowClassNames,
    formatToRowSelectColumn: formatToRowSelectColumn
  };
}

export { useRowSelect as default };
//# sourceMappingURL=useRowSelect.js.map
