/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { h as helper } from '../../../_chunks/dep-42d5c87e.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import get from 'lodash/get';
import primaryTableProps from '../../primary-table-props.js';
import baseTableProps from '../../base-table-props.js';
import { prefix } from '../../../config.js';
import { isRowSelectedDisabled, filterDataByIds } from '../../util/common.js';
import SelectBox from '../select-box.js';
import { emitEvent } from '../../../utils/event.js';
import 'lodash/isFunction';
import 'lodash/isString';
import '../../../checkbox/index.js';
import '../../../checkbox/group.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/intersection';
import '../../../checkbox/checkbox.js';
import '../../../utils/render-tnode.js';
import '../../../utils/classnames.js';
import '../../../checkbox/props.js';
import '../../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../../../checkbox/checkbox-group-props.js';
import '../../../utils/map-props.js';
import '../../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../../../checkbox/type.js';
import '../../../radio/index.js';
import '../../../radio/radio.js';
import '../../../radio/props.js';
import '../../../radio/group.js';
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; }
var select = Vue.extend({
  name: "".concat(prefix, "-primary-table-select"),
  props: {
    columns: primaryTableProps.columns,
    data: baseTableProps.data,
    rowKey: baseTableProps.rowKey,
    selectedRowKeys: primaryTableProps.selectedRowKeys
  },
  computed: {
    reRowKey: function reRowKey() {
      return this.rowKey || "id";
    },
    selectColumn: function selectColumn() {
      return this.columns.find(function (_ref) {
        var type = _ref.type;
        return ["multiple", "single"].includes(type);
      }) || {};
    },
    canSelectedRows: function canSelectedRows() {
      var _this = this;

      return this.data.filter(function (row, rowIndex) {
        return !_this.isDisabled(row, rowIndex);
      });
    },
    isSelectedAll: function isSelectedAll() {
      var _this2 = this;

      return !!(this.canSelectedRows.length && this.canSelectedRows.every(function (record) {
        return _this2.selectedRowKeys.includes(get(record, _this2.reRowKey));
      }));
    },
    isSelectedSome: function isSelectedSome() {
      var _this3 = this;

      return !this.isSelectedAll && this.canSelectedRows.some(function (record) {
        return _this3.selectedRowKeys.includes(get(record, _this3.reRowKey));
      });
    }
  },
  methods: {
    isDisabled: function isDisabled(row, rowIndex) {
      return isRowSelectedDisabled(this.selectColumn, row, rowIndex);
    },
    getSelectColumns: function getSelectColumns(columns) {
      var _this4 = this;

      return columns.map(function (c) {
        var isSelection = ["multiple", "single"].includes(c.type);
        var isMultiple = c.type === "multiple";
        var title = isMultiple ? _this4.getSelectedHeader() : "";
        return _objectSpread(_objectSpread(_objectSpread({}, c), isSelection ? {
          render: function render(h, slotProps) {
            return _this4.renderSelectCell(_objectSpread({
              column: c
            }, slotProps));
          }
        } : {}), isSelection ? {
          title: title
        } : {});
      });
    },
    getSelectedHeader: function getSelectedHeader() {
      var _this5 = this;

      var h = this.$createElement;
      return function () {
        return h(SelectBox, {
          "attrs": {
            "checked": _this5.isSelectedAll,
            "indeterminate": _this5.isSelectedSome,
            "disabled": !_this5.canSelectedRows.length
          },
          "on": _objectSpread({}, {
            change: _this5.handleSelectAll
          })
        });
      };
    },
    renderSelectCell: function renderSelectCell(_ref2) {
      var _this6 = this;

      var _ref2$column = _ref2.column,
          column = _ref2$column === void 0 ? {} : _ref2$column,
          _ref2$row = _ref2.row,
          row = _ref2$row === void 0 ? {} : _ref2$row,
          rowIndex = _ref2.rowIndex;
      var h = this.$createElement;
      var selectBoxProps = {
        props: _objectSpread(_objectSpread({
          checked: this.selectedRowKeys.includes(get(row, this.reRowKey))
        }, column), {}, {
          type: column.type,
          checkProps: typeof column.checkProps === "function" ? column.checkProps({
            row: row,
            rowIndex: rowIndex
          }) : column.checkProps,
          disabled: typeof column.disabled === "function" ? column.disabled({
            row: row,
            rowIndex: rowIndex
          }) : column.disabled,
          rowIndex: rowIndex
        }),
        on: {
          click: function click(e) {
            e.stopPropagation();
          },
          change: function change() {
            return _this6.handleSelectChange(row);
          }
        }
      };
      return h(SelectBox, helper([{}, selectBoxProps]));
    },
    handleSelectChange: function handleSelectChange() {
      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var selectedRowKeys = _toConsumableArray(this.selectedRowKeys);

      var reRowKey = this.reRowKey;
      var id = get(record, reRowKey);
      var selectedRowIndex = selectedRowKeys.indexOf(id);
      var isSelected = selectedRowIndex !== -1;

      if (this.selectColumn.type === "multiple") {
        isSelected ? selectedRowKeys.splice(selectedRowIndex, 1) : selectedRowKeys.push(id);
      }

      if (this.selectColumn.type === "single") {
        selectedRowKeys = !isSelected ? [id] : [];
      }

      emitEvent(this, "select-change", selectedRowKeys, {
        selectedRowData: filterDataByIds(this.data, selectedRowKeys, reRowKey),
        currentRowKey: id,
        currentRowData: record,
        type: isSelected ? "uncheck" : "check"
      });
    },
    handleSelectAll: function handleSelectAll() {
      var selectedRowKeys = this.selectedRowKeys,
          canSelectedRows = this.canSelectedRows,
          reRowKey = this.reRowKey;
      var canSelectedRowKeys = canSelectedRows.map(function (record) {
        return get(record, reRowKey);
      });
      var disabledSelectedRowKeys = selectedRowKeys.filter(function (id) {
        return !canSelectedRowKeys.includes(id);
      });
      var allIds = this.isSelectedAll ? _toConsumableArray(disabledSelectedRowKeys) : [].concat(_toConsumableArray(disabledSelectedRowKeys), _toConsumableArray(canSelectedRowKeys));
      emitEvent(this, "select-change", allIds, {
        selectedRowData: filterDataByIds(this.data, allIds, reRowKey),
        type: this.isSelectedAll ? "uncheck" : "check",
        currentRowKey: "CHECK_ALL_BOX"
      });
    }
  }
});

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