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

'use strict';

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

var helper = require('../../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var intersection = require('lodash/intersection');
var get = require('lodash/get');
var isFunction = require('lodash/isFunction');
var log = require('../../../chunks/log.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
var components_table_utils = require('../utils.js');
var components_checkbox_index = require('../../checkbox/index.js');
var components_radio_index = require('../../radio/index.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('lodash/isObject');
require('../../../utils/map-props.js');
require('vue');
require('../../../utils/with-install.js');
require('lodash');
require('../../../config.js');
require('../../checkbox/group.js');
require('../../../utils/event.js');
require('../../../utils/helper.js');
require('lodash/camelCase');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../checkbox/checkbox.js');
require('../../../utils/render-node.js');
require('../../checkbox/props.js');
require('../../radio/radio.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('lodash/isString');
require('lodash/isNumber');
require('../../radio/radio-button.js');

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

var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

function useRowSelect(props, tableSelectedClasses) {
  const {
    selectedRowKeys,
    columns,
    data,
    rowKey
  } = VueCompositionAPI.toRefs(props);
  const selectedRowClassNames = VueCompositionAPI.ref();
  const [tSelectedRowKeys, setTSelectedRowKeys] = hooks_useDefaultValue["default"](selectedRowKeys, props.defaultSelectedRowKeys || [], props.onSelectChange, "selectedRowKeys", "select-change");
  const selectedRowDataMap = VueCompositionAPI.ref( /* @__PURE__ */new Map());
  const selectColumn = VueCompositionAPI.computed(() => props.columns.find(_ref => {
    let {
      type
    } = _ref;
    return ["multiple", "single"].includes(type);
  }));
  const canSelectedRows = VueCompositionAPI.computed(() => data.value.filter((row, rowIndex) => !isDisabled(row, rowIndex)));
  const intersectionKeys = VueCompositionAPI.computed(() => intersection__default["default"](tSelectedRowKeys.value, canSelectedRows.value.map(t => get__default["default"](t, props.rowKey || "id"))));
  VueCompositionAPI.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__default["default"](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 components_table_utils.isRowSelectedDisabled(selectColumn.value, row, rowIndex);
  }
  function getSelectedHeader(h2) {
    return () => VueCompositionAPI.h(components_checkbox_index.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__default["default"](row, props.rowKey || "id"));
    const disabled = typeof column.disabled === "function" ? column.disabled({
      row,
      rowIndex
    }) : column.disabled;
    const checkProps = isFunction__default["default"](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 VueCompositionAPI.h(components_radio_index.Radio, helper.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__default["default"](row, props.rowKey)) : false;
      return VueCompositionAPI.h(components_checkbox_index.Checkbox, helper.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__default["default"](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.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__default["default"](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
    };
  }
  VueCompositionAPI.watch([data, rowKey], _ref3 => {
    let [data2, rowKey2] = _ref3;
    for (let i = 0, len = data2.length; i < len; i++) selectedRowDataMap.value.set(get__default["default"](data2[i], rowKey2 || "id"), data2[i]);
  }, {
    immediate: true
  });
  return {
    selectedRowClassNames,
    formatToRowSelectColumn
  };
}

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