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

import { _ as _toConsumableArray } from '../../_chunks/dep-bf1e33c8.mjs';
import { _ as _typeof } from '../../_chunks/dep-255303ba.mjs';
import { _ as _defineProperty } from '../../_chunks/dep-2db7b3ec.mjs';
import { _ as _slicedToArray } from '../../_chunks/dep-84acf3c2.mjs';
import { toRefs, ref, computed, watch, createVNode, mergeProps } from 'vue';
import { i as intersection_1 } from '../../_chunks/dep-85ed244c.mjs';
import { g as get_1 } from '../../_chunks/dep-c81c0ee4.mjs';
import { i as isFunction_1 } from '../../_chunks/dep-5717e263.mjs';
import useDefaultValue from '../../hooks/useDefaultValue.mjs';
import { isRowSelectedDisabled } from '../../_common/js/table/utils.mjs';
import { Checkbox } from '../../checkbox/index.mjs';
import { Radio } from '../../radio/index.mjs';
import '../../_common/js/log/index.mjs';
import log from '../../_common/js/log/log.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-2e7f59b1.mjs';
import '../../_chunks/dep-a8bcacea.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 '../../_chunks/dep-0d3dd4b5.mjs';
import '../../_chunks/dep-9a85dac1.mjs';
import '../../_chunks/dep-1372b2d3.mjs';
import '../../_chunks/dep-3f21269a.mjs';
import '../../utils/withInstall.mjs';
import '../../checkbox/checkbox.mjs';
import '../../hooks/useVModel.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 '../../hooks/useConfig.mjs';
import '../../config-provider/useConfig.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 '../../radio/radio.mjs';
import '../../utils/helper.mjs';
import '../../_chunks/dep-60fae668.mjs';
import '../../_chunks/dep-ed5ebb35.mjs';
import '../../radio/constants.mjs';
import '../../radio/props.mjs';
import '../../radio/group.mjs';
import '../../_chunks/dep-38ee6a8f.mjs';
import '../../_chunks/dep-01e3f446.mjs';
import '../../radio/radio-group-props.mjs';
import '../../radio/useKeyboard.mjs';
import '../../utils/dom.mjs';
import '../../utils/easing.mjs';
import '../../radio/radio-button.mjs';
import '../../radio/style/css.mjs';
import '../../radio/type.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 useRowSelect(props, tableSelectedClasses) {
  var _toRefs = toRefs(props),
    selectedRowKeys = _toRefs.selectedRowKeys,
    columns = _toRefs.columns,
    rowKey = _toRefs.rowKey,
    data = _toRefs.data,
    pagination = _toRefs.pagination,
    reserveSelectedRowOnPaginate = _toRefs.reserveSelectedRowOnPaginate;
  var currentPaginateData = ref(data.value);
  var selectedRowClassNames = ref();
  var _useDefaultValue = useDefaultValue(selectedRowKeys, props.defaultSelectedRowKeys || [], props.onSelectChange, "selectedRowKeys"),
    _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 () {
    var currentData = reserveSelectedRowOnPaginate.value ? data.value : currentPaginateData.value;
    return currentData.filter(function (row, rowIndex) {
      return !isDisabled(row, rowIndex);
    });
  });
  var intersectionKeys = computed(function () {
    return intersection_1(tSelectedRowKeys.value, canSelectedRows.value.map(function (t) {
      return get_1(t, props.rowKey || "id");
    }));
  });
  watch([data, pagination, reserveSelectedRowOnPaginate], function () {
    if (reserveSelectedRowOnPaginate.value) return;
    var _pagination$value = pagination.value,
      pageSize = _pagination$value.pageSize,
      current = _pagination$value.current,
      defaultPageSize = _pagination$value.defaultPageSize,
      defaultCurrent = _pagination$value.defaultCurrent;
    var tPageSize = pageSize || defaultPageSize;
    var tCurrent = current || defaultCurrent;
    currentPaginateData.value = data.value.slice(tPageSize * (tCurrent - 1), tPageSize * tCurrent);
  }, {
    immediate: true
  });
  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_1(row, props.rowKey || "id");
      return selected.has(rowId) ? tableSelectedClasses.selected : "";
    };
    var selectedRowClass = selected.size ? selectedRowClassFunc : void 0;
    selectedRowClassNames.value = [disabledRowClass, selectedRowClass];
  }, {
    immediate: true
  });
  function isDisabled(row, rowIndex) {
    return isRowSelectedDisabled(selectColumn.value, row, rowIndex);
  }
  function getSelectedHeader() {
    return function () {
      var isIndeterminate = intersectionKeys.value.length > 0 && intersectionKeys.value.length < canSelectedRows.value.length;
      var isChecked = intersectionKeys.value.length !== 0 && canSelectedRows.value.length !== 0 && intersectionKeys.value.length === canSelectedRows.value.length;
      return createVNode(Checkbox, {
        "checked": isChecked,
        "indeterminate": isIndeterminate,
        "disabled": !canSelectedRows.value.length,
        "onChange": handleSelectAll
      }, null);
    };
  }
  function getRowSelectDisabledData(p) {
    var col = p.col,
      row = p.row,
      rowIndex = p.rowIndex;
    var disabled = typeof col.disabled === "function" ? col.disabled({
      row: row,
      rowIndex: rowIndex
    }) : col.disabled;
    var checkProps = isFunction_1(col.checkProps) ? col.checkProps({
      row: row,
      rowIndex: rowIndex
    }) : col.checkProps;
    return {
      disabled: disabled || (checkProps === null || checkProps === void 0 ? void 0 : checkProps.disabled),
      checkProps: checkProps
    };
  }
  function renderSelectCell(p) {
    var column = p.col,
      _p$row = p.row,
      row = _p$row === void 0 ? {} : _p$row;
    var checked = tSelectedRowKeys.value.includes(get_1(row, props.rowKey || "id"));
    var _getRowSelectDisabled = getRowSelectDisabledData(p),
      disabled = _getRowSelectDisabled.disabled,
      checkProps = _getRowSelectDisabled.checkProps;
    var selectBoxProps = _objectSpread(_objectSpread({
      checked: checked,
      disabled: disabled
    }, checkProps), {}, {
      onClick: function onClick(e) {
        var _e$e;
        if (_typeof(e) === "object" && "e" in e) (_e$e = e.e) === null || _e$e === void 0 ? void 0 : _e$e.stopPropagation();else e === null || e === void 0 ? void 0 : e.stopPropagation();
      },
      onChange: function onChange() {
        return handleSelectChange(row);
      }
    });
    if (column.type === "single") return createVNode(Radio, selectBoxProps, null);
    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_1(row, props.rowKey)) : false;
      return createVNode(Checkbox, mergeProps({
        "indeterminate": isIndeterminate
      }, selectBoxProps), null);
    }
    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_1(row, reRowKey);
    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_1(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(_, p) {
        return renderSelectCell(p);
      },
      title: col.type === "multiple" ? getSelectedHeader() : col.title
    });
  }
  var onInnerSelectRowClick = function onInnerSelectRowClick(_ref3) {
    var row = _ref3.row,
      index = _ref3.index;
    var selectedColIndex = props.columns.findIndex(function (item) {
      return item.colKey === "row-select";
    });
    if (selectedColIndex === -1) return;
    var _getRowSelectDisabled2 = getRowSelectDisabledData({
        row: row,
        rowIndex: index,
        col: props.columns[selectedColIndex],
        colIndex: selectedColIndex
      }),
      disabled = _getRowSelectDisabled2.disabled;
    if (disabled) return;
    handleSelectChange(row);
  };
  watch(function () {
    return [_toConsumableArray(data.value), rowKey];
  }, function () {
    for (var i = 0, len = data.value.length; i < len; i++) selectedRowDataMap.value.set(get_1(data.value[i], rowKey.value || "id"), data.value[i]);
  }, {
    immediate: true
  });
  return {
    selectedRowClassNames: selectedRowClassNames,
    currentPaginateData: currentPaginateData,
    setTSelectedRowKeys: setTSelectedRowKeys,
    formatToRowSelectColumn: formatToRowSelectColumn,
    onInnerSelectRowClick: onInnerSelectRowClick
  };
}

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