"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _vueTypes = _interopRequireDefault(require("../../_util/vue-types"));

var _ExpandIcon = _interopRequireDefault(require("./ExpandIcon"));

var _BaseMixin = _interopRequireDefault(require("../../_util/BaseMixin"));

var _store = require("../../_util/store");

var _propsUtil = require("../../_util/props-util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var ExpandableRow = {
  mixins: [_BaseMixin.default],
  name: 'ExpandableRow',
  inheritAttrs: false,
  props: {
    prefixCls: _vueTypes.default.string.isRequired,
    rowKey: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number]).isRequired,
    fixed: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.bool]),
    record: _vueTypes.default.oneOfType([_vueTypes.default.object, _vueTypes.default.array]).isRequired,
    indentSize: _vueTypes.default.number,
    needIndentSpaced: _vueTypes.default.bool.isRequired,
    expandRowByClick: _vueTypes.default.bool,
    expanded: _vueTypes.default.bool.isRequired,
    expandIconAsCell: _vueTypes.default.bool,
    expandIconColumnIndex: _vueTypes.default.number,
    childrenColumnName: _vueTypes.default.string,
    expandedRowRender: _vueTypes.default.func,
    expandIcon: _vueTypes.default.func // onExpandedChange: PropTypes.func.isRequired,
    // onRowClick: PropTypes.func,
    // children: PropTypes.func.isRequired,

  },
  beforeUnmount: function beforeUnmount() {
    this.handleDestroy();
  },
  methods: {
    hasExpandIcon: function hasExpandIcon(columnIndex) {
      var _this$$props = this.$props,
          expandRowByClick = _this$$props.expandRowByClick,
          expandIcon = _this$$props.expandIcon;

      if (this.tempExpandIconAsCell || columnIndex !== this.tempExpandIconColumnIndex) {
        return false;
      }

      return !!expandIcon || !expandRowByClick;
    },
    handleExpandChange: function handleExpandChange(record, event) {
      var expanded = this.expanded,
          rowKey = this.rowKey;

      this.__emit('expandedChange', !expanded, record, event, rowKey);
    },
    handleDestroy: function handleDestroy() {
      var rowKey = this.rowKey,
          record = this.record;

      this.__emit('expandedChange', false, record, null, rowKey, true);
    },
    handleRowClick: function handleRowClick(record, index, event) {
      var expandRowByClick = this.expandRowByClick;

      if (expandRowByClick) {
        this.handleExpandChange(record, event);
      }

      this.__emit('rowClick', record, index, event);
    },
    renderExpandIcon: function renderExpandIcon() {
      var prefixCls = this.prefixCls,
          expanded = this.expanded,
          record = this.record,
          needIndentSpaced = this.needIndentSpaced,
          expandIcon = this.expandIcon;

      if (expandIcon) {
        return expandIcon({
          prefixCls: prefixCls,
          expanded: expanded,
          record: record,
          needIndentSpaced: needIndentSpaced,
          expandable: this.expandable,
          onExpand: this.handleExpandChange
        });
      }

      return _vue.createVNode(_ExpandIcon.default, {
        "expandable": this.expandable,
        "prefixCls": prefixCls,
        "onExpand": this.handleExpandChange,
        "needIndentSpaced": needIndentSpaced,
        "expanded": expanded,
        "record": record
      }, null);
    },
    renderExpandIconCell: function renderExpandIconCell(cells) {
      if (!this.tempExpandIconAsCell) {
        return;
      }

      var prefixCls = this.prefixCls;
      cells.push(_vue.createVNode("td", {
        "class": "".concat(prefixCls, "-expand-icon-cell"),
        "key": "rc-table-expand-icon-cell"
      }, [this.renderExpandIcon()]));
    }
  },
  render: function render() {
    var childrenColumnName = this.childrenColumnName,
        expandedRowRender = this.expandedRowRender,
        indentSize = this.indentSize,
        record = this.record,
        fixed = this.fixed,
        expanded = this.expanded;
    this.tempExpandIconAsCell = fixed !== 'right' ? this.expandIconAsCell : false;
    this.tempExpandIconColumnIndex = fixed !== 'right' ? this.expandIconColumnIndex : -1;
    var childrenData = record[childrenColumnName];
    this.expandable = !!(childrenData || expandedRowRender);
    var expandableRowProps = {
      indentSize: indentSize,
      expanded: expanded,
      // not used in TableRow, but it's required to re-render TableRow when `expanded` changes
      hasExpandIcon: this.hasExpandIcon,
      renderExpandIcon: this.renderExpandIcon,
      renderExpandIconCell: this.renderExpandIconCell,
      onRowClick: this.handleRowClick
    };
    return (0, _propsUtil.getSlot)(this, 'default', expandableRowProps);
  }
};

var _default = (0, _store.connect)(function (_ref, _ref2) {
  var expandedRowKeys = _ref.expandedRowKeys;
  var rowKey = _ref2.rowKey;
  return {
    expanded: expandedRowKeys.includes(rowKey)
  };
})(ExpandableRow);

exports.default = _default;