/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../../_chunks/dep-29915b8e.js';
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 { l as log } from '../../../_chunks/dep-70c789e5.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { isRowSelectedDisabled } from '../utils.js';
import { Checkbox } from '../../checkbox/index.js';
import { Radio } from '../../radio/index.js';
import '../../../_chunks/dep-5210251b.js';
import 'lodash/isObject';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/with-install.js';
import 'lodash';
import '../../../config.js';
import '../../checkbox/group.js';
import '../../../utils/event.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../_chunks/dep-def4c2ce.js';
import '../../../utils/mixins.js';
import '../../checkbox/checkbox.js';
import '../../../utils/render-node.js';
import '../../checkbox/props.js';
import '../../radio/radio.js';
import '../../radio/props.js';
import '../../radio/group.js';
import 'lodash/isString';
import 'lodash/isNumber';
import '../../radio/radio-button.js';

function useRowSelect(props, tableSelectedClasses) {
  const {
    selectedRowKeys,
    columns,
    data,
    rowKey
  } = toRefs(props);
  const selectedRowClassNames = ref();
  const [tSelectedRowKeys, setTSelectedRowKeys] = useDefaultValue(selectedRowKeys, props.defaultSelectedRowKeys || [], props.onSelectChange, "selectedRowKeys", "select-change");
  const selectedRowDataMap = ref( /* @__PURE__ */new Map());
  const selectColumn = computed(() => props.columns.find(_ref => {
    let {
      type
    } = _ref;
    return ["multiple", "single"].includes(type);
  }));
  const canSelectedRows = computed(() => data.value.filter((row, rowIndex) => !isDisabled(row, rowIndex)));
  const intersectionKeys = computed(() => intersection(tSelectedRowKeys.value, canSelectedRows.value.map(t => get(t, props.rowKey || "id"))));
  watch([data, columns, tSelectedRowKeys, selectColumn, rowKey], () => {
    var _selectColumn$value;
    const disabledRowFunc = p => selectColumn.value.disabled(p) ? tableSelectedClasses.disabled : "";
    const disabledRowClass = (_selectColumn$value = selectColumn.value) !== null && _selectColumn$value !== void 0 && _selectColumn$value.disabled ? disabledRowFunc : void 0;
    const selected = new Set(tSelectedRowKeys.value);
    const selectedRowClassFunc = _ref2 => {
      let {
        row
      } = _ref2;
      const rowId = get(row, props.rowKey || "id");
      return selected.has(rowId) ? tableSelectedClasses.selected : "";
    };
    const selectedRowClass = selected.size ? selectedRowClassFunc : void 0;
    selectedRowClassNames.value = [disabledRowClass, selectedRowClass].filter(v => v);
  }, {
    immediate: true
  });
  function isDisabled(row, rowIndex) {
    return isRowSelectedDisabled(selectColumn.value, row, rowIndex);
  }
  function getSelectedHeader(h2) {
    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": {
        ...{
          change: handleSelectAll
        }
      }
    });
  }
  function renderSelectCell(h2, p) {
    const {
      col: column,
      row = {},
      rowIndex
    } = p;
    const checked = tSelectedRowKeys.value.includes(get(row, props.rowKey || "id"));
    const disabled = typeof column.disabled === "function" ? column.disabled({
      row,
      rowIndex
    }) : column.disabled;
    const checkProps = isFunction(column.checkProps) ? column.checkProps({
      row,
      rowIndex
    }) : column.checkProps;
    const selectBoxProps = {
      props: {
        checked,
        disabled,
        ...checkProps
      },
      on: {
        click: e => {
          e === null || e === void 0 ? void 0 : e.stopPropagation();
        },
        change: () => handleSelectChange(row)
      }
    };
    if (column.type === "single") return h(Radio, helper([{}, selectBoxProps]));
    if (column.type === "multiple") {
      var _props$indeterminateS;
      const 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() {
    let row = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    let selectedRowKeys2 = [...tSelectedRowKeys.value];
    const reRowKey = props.rowKey || "id";
    const id = get(row, reRowKey);
    const selectedRowIndex = selectedRowKeys2.indexOf(id);
    const 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(t => selectedRowDataMap.value.get(t)),
      currentRowKey: id,
      currentRowData: row,
      type: isExisted ? "uncheck" : "check"
    });
  }
  function handleSelectAll(checked) {
    var _selectedRowKeys$valu;
    const reRowKey = props.rowKey || "id";
    const canSelectedRowKeys = canSelectedRows.value.map(record => get(record, reRowKey));
    const disabledSelectedRowKeys = ((_selectedRowKeys$valu = selectedRowKeys.value) === null || _selectedRowKeys$valu === void 0 ? void 0 : _selectedRowKeys$valu.filter(id => !canSelectedRowKeys.includes(id))) || [];
    const allIds = checked ? [...disabledSelectedRowKeys, ...canSelectedRowKeys] : [...disabledSelectedRowKeys];
    setTSelectedRowKeys(allIds, {
      selectedRowData: checked ? allIds.map(t => selectedRowDataMap.value.get(t)) : [],
      type: checked ? "check" : "uncheck",
      currentRowKey: "CHECK_ALL_BOX"
    });
  }
  function formatToRowSelectColumn(col) {
    const isSelection = ["multiple", "single"].includes(col.type);
    if (!isSelection) return col;
    return {
      ...col,
      width: col.width || 64,
      className: [tableSelectedClasses.checkCell, col.className],
      cell: (h2, p) => renderSelectCell(h2, p),
      title: col.type === "multiple" ? getSelectedHeader() : col.title
    };
  }
  watch([data, rowKey], _ref3 => {
    let [data2, rowKey2] = _ref3;
    for (let i = 0, len = data2.length; i < len; i++) selectedRowDataMap.value.set(get(data2[i], rowKey2 || "id"), data2[i]);
  }, {
    immediate: true
  });
  return {
    selectedRowClassNames,
    formatToRowSelectColumn
  };
}

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