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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var helper = require('../../../_chunks/dep-44e6bdf6.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var get = require('lodash/get');
var table_primaryTableProps = require('../../primary-table-props.js');
var table_baseTableProps = require('../../base-table-props.js');
var config = require('../../../config.js');
var table_util_common = require('../../util/common.js');
var table_primaryTable_selectBox = require('../select-box.js');
var utils_event = require('../../../utils/event.js');
require('lodash/isFunction');
require('lodash/isString');
require('../../../checkbox/index.js');
require('../../../checkbox/group.js');
require('@babel/runtime/helpers/typeof');
require('lodash/intersection');
require('../../../checkbox/checkbox.js');
require('../../../utils/render-tnode.js');
require('../../../utils/classnames.js');
require('../../../checkbox/props.js');
require('../../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../../../checkbox/checkbox-group-props.js');
require('../../../utils/map-props.js');
require('../../../utils/withInstall.js');
require('lodash/capitalize');
require('../../../radio/index.js');
require('../../../radio/radio.js');
require('../../../radio/props.js');
require('../../../radio/group.js');
require('lodash/isNumber');
require('../../../radio/radio-group-props.js');
require('../../../radio/radio-button.js');

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

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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__default["default"](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__default["default"].extend({
  name: "".concat(config.prefix, "-primary-table-select"),
  props: {
    columns: table_primaryTableProps["default"].columns,
    data: table_baseTableProps["default"].data,
    rowKey: table_baseTableProps["default"].rowKey,
    selectedRowKeys: table_primaryTableProps["default"].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__default["default"](record, _this2.reRowKey));
      }));
    },
    isSelectedSome: function isSelectedSome() {
      var _this3 = this;

      return !this.isSelectedAll && this.canSelectedRows.some(function (record) {
        return _this3.selectedRowKeys.includes(get__default["default"](record, _this3.reRowKey));
      });
    }
  },
  methods: {
    isDisabled: function isDisabled(row, rowIndex) {
      return table_util_common.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(table_primaryTable_selectBox["default"], {
          "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__default["default"](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(table_primaryTable_selectBox["default"], helper.helper([{}, selectBoxProps]));
    },
    handleSelectChange: function handleSelectChange() {
      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var selectedRowKeys = _toConsumableArray__default["default"](this.selectedRowKeys);

      var reRowKey = this.reRowKey;
      var id = get__default["default"](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] : [];
      }

      utils_event.emitEvent(this, "select-change", selectedRowKeys, {
        selectedRowData: table_util_common.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__default["default"](record, reRowKey);
      });
      var disabledSelectedRowKeys = selectedRowKeys.filter(function (id) {
        return !canSelectedRowKeys.includes(id);
      });
      var allIds = this.isSelectedAll ? _toConsumableArray__default["default"](disabledSelectedRowKeys) : [].concat(_toConsumableArray__default["default"](disabledSelectedRowKeys), _toConsumableArray__default["default"](canSelectedRowKeys));
      utils_event.emitEvent(this, "select-change", allIds, {
        selectedRowData: table_util_common.filterDataByIds(this.data, allIds, reRowKey),
        type: this.isSelectedAll ? "uncheck" : "check",
        currentRowKey: "CHECK_ALL_BOX"
      });
    }
  }
});

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