/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var tdesignIconsVue = require('tdesign-icons-vue');
var get = require('lodash/get');
var table_hooks_useClassName = require('./useClassName.js');
var hooks_tnode = require('../../hooks/tnode.js');
var hooks_useDefaultValue = require('../../hooks/useDefaultValue.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../hooks/render-tnode.js');
require('lodash/isString');
require('lodash/isObject');

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

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

function useRowExpand(props, context) {
  var _toRefs = VueCompositionAPI.toRefs(props),
      expandedRowKeys = _toRefs.expandedRowKeys;

  var renderTNode = hooks_tnode.useTNodeJSX();

  var _useConfig = configProvider_useConfig.useConfig("table"),
      t = _useConfig.t,
      global = _useConfig.global;

  var _useClassName = table_hooks_useClassName["default"](),
      tableExpandClasses = _useClassName.tableExpandClasses,
      positiveRotate90 = _useClassName.positiveRotate90,
      tableFullRowClasses = _useClassName.tableFullRowClasses;

  var _useDefaultValue = hooks_useDefaultValue["default"](expandedRowKeys, props.defaultExpandedRowKeys, props.onExpandChange, "expandedRowKeys", "expand-change"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      tExpandedRowKeys = _useDefaultValue2[0],
      setTExpandedRowKeys = _useDefaultValue2[1];

  var showExpandedRow = VueCompositionAPI.computed(function () {
    return Boolean(props.expandedRow || context.slots.expandedRow || context.slots["expanded-row"]);
  });
  var showExpandIconColumn = VueCompositionAPI.computed(function () {
    return props.expandIcon !== false && showExpandedRow.value;
  });
  var isFirstColumnFixed = VueCompositionAPI.computed(function () {
    var _props$columns, _props$columns$;

    return ((_props$columns = props.columns) === null || _props$columns === void 0 ? void 0 : (_props$columns$ = _props$columns[0]) === null || _props$columns$ === void 0 ? void 0 : _props$columns$.fixed) === "left";
  });

  var onToggleExpand = function onToggleExpand(e, row) {
    props.expandOnRowClick && e.stopPropagation();
    var currentId = get__default["default"](row, props.rowKey || "id");
    var index = tExpandedRowKeys.value.indexOf(currentId);

    var newKeys = _toConsumableArray__default["default"](tExpandedRowKeys.value);

    index !== -1 ? newKeys.splice(index, 1) : newKeys.push(currentId);
    setTExpandedRowKeys(newKeys, {
      expandedRowData: props.data.filter(function (t2) {
        return newKeys.includes(get__default["default"](t2, props.rowKey || "id"));
      })
    });
  };

  var renderExpandIcon = function renderExpandIcon(h, p) {
    var row = p.row,
        rowIndex = p.rowIndex;
    var currentId = get__default["default"](row, props.rowKey || "id");
    var expanded = tExpandedRowKeys.value.includes(currentId);
    var icon = renderTNode("expandIcon", {
      defaultNode: t(global.value.expandIcon) || h(tdesignIconsVue.ChevronRightCircleIcon),
      params: {
        row: row,
        index: rowIndex
      }
    });
    if (!icon) return null;
    var classes = [tableExpandClasses.iconBox, tableExpandClasses[expanded ? "expanded" : "collapsed"], _defineProperty__default["default"]({}, positiveRotate90, expanded)];
    return h("span", {
      "class": classes,
      "on": {
        "click": function click(e) {
          return onToggleExpand(e, row);
        }
      }
    }, [icon]);
  };

  var getExpandColumn = function getExpandColumn(h) {
    var expandCol = {
      colKey: "__EXPAND_ROW_ICON_COLUMN__",
      width: 64,
      className: tableExpandClasses.iconCell,
      fixed: isFirstColumnFixed.value ? "left" : void 0,
      cell: renderExpandIcon
    };
    return expandCol;
  };

  var renderExpandedRow = function renderExpandedRow(h, p) {
    var rowId = get__default["default"](p.row, props.rowKey || "id");
    if (!tExpandedRowKeys.value.includes(rowId)) return null;
    var isFixedLeft = p.isWidthOverflow && props.columns.find(function (item) {
      return item.fixed === "left";
    });
    return h("tr", {
      "key": "expand_".concat(rowId),
      "class": [tableExpandClasses.row, _defineProperty__default["default"]({}, tableFullRowClasses.base, isFixedLeft)]
    }, [h("td", {
      "attrs": {
        "colspan": p.columns.length
      }
    }, [h("div", {
      "class": [tableExpandClasses.rowInner, _defineProperty__default["default"]({}, tableFullRowClasses.innerFullRow, isFixedLeft)],
      "style": isFixedLeft ? {
        width: "".concat(p.tableWidth, "px")
      } : {}
    }, [h("div", {
      "class": tableFullRowClasses.innerFullElement
    }, [renderTNode("expandedRow", {
      params: p
    })])])])]);
  };

  var onInnerExpandRowClick = function onInnerExpandRowClick(p) {
    onToggleExpand(p.e, p.row);
  };

  return {
    showExpandedRow: showExpandedRow,
    showExpandIconColumn: showExpandIconColumn,
    getExpandColumn: getExpandColumn,
    renderExpandedRow: renderExpandedRow,
    onInnerExpandRowClick: onInnerExpandRowClick
  };
}

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