/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _defineProperty } from '../../_chunks/dep-2db7b3ec.mjs';
import { _ as _toConsumableArray } from '../../_chunks/dep-bf1e33c8.mjs';
import { _ as _slicedToArray } from '../../_chunks/dep-84acf3c2.mjs';
import { toRefs, computed, createVNode } from 'vue';
import { ChevronRightCircleIcon } from '@ldesign/icons-vue-next';
import { g as get_1 } from '../../_chunks/dep-c81c0ee4.mjs';
import { useTNodeJSX } from '../../hooks/tnode.mjs';
import useDefaultValue from '../../hooks/useDefaultValue.mjs';
import '../../hooks/useConfig.mjs';
import { useGlobalIcon } from '../../hooks/useGlobalIcon.mjs';
import useClassName from './useClassName.mjs';
import { useConfig } from '../../config-provider/useConfig.mjs';
import '../../_chunks/dep-255303ba.mjs';
import '../../_chunks/dep-b8d7992c.mjs';
import '../../_chunks/dep-eba0ea6e.mjs';
import '../../_chunks/dep-0d3dd4b5.mjs';
import '../../_chunks/dep-b57180d0.mjs';
import '../../_chunks/dep-c8d7b6b4.mjs';
import '../../_chunks/dep-2bc37d52.mjs';
import '../../_chunks/dep-469b4ae0.mjs';
import '../../_chunks/dep-3da99438.mjs';
import '../../_chunks/dep-bc1d737a.mjs';
import '../../_chunks/dep-4d7a675b.mjs';
import '../../_chunks/dep-5717e263.mjs';
import '../../_chunks/dep-a8bcacea.mjs';
import '../../_chunks/dep-2e7f59b1.mjs';
import '../../_chunks/dep-855db8bc.mjs';
import '../../_chunks/dep-42cc3015.mjs';
import '../../_chunks/dep-406cd044.mjs';
import '../../_chunks/dep-327559e8.mjs';
import '../../_chunks/dep-b2235f26.mjs';
import '../../_chunks/dep-8b989474.mjs';
import '../../_chunks/dep-1372b2d3.mjs';
import '../../_chunks/dep-9a85dac1.mjs';
import '../../utils/render-tnode.mjs';
import '../../_chunks/dep-888e80c4.mjs';
import '../../_chunks/dep-334a4f6b.mjs';
import '../../_chunks/dep-e5eb9f23.mjs';
import '../../_chunks/dep-9fe2ef12.mjs';
import '../../_chunks/dep-672e7d1a.mjs';
import '../../_chunks/dep-1434aa15.mjs';
import '../../_chunks/dep-9262ee2f.mjs';
import '../../_chunks/dep-876fb3ec.mjs';
import '../../_chunks/dep-095107f7.mjs';
import '../../_chunks/dep-c989dff3.mjs';
import '../../_chunks/dep-9cf86cf1.mjs';
import '../../_chunks/dep-a87f3e4c.mjs';
import '../../_chunks/dep-1e566bb5.mjs';
import '../../_chunks/dep-42a19209.mjs';
import '../../_chunks/dep-8d3e9aa3.mjs';
import '../../_chunks/dep-40d69a9c.mjs';
import '../../_chunks/dep-dcc83161.mjs';
import '../../_chunks/dep-0fc02184.mjs';
import '../../_chunks/dep-f1994936.mjs';
import '../../_chunks/dep-345e1be3.mjs';
import '../../_common/js/global-config/default-config.mjs';
import '../../_common/js/global-config/locale/zh_CN.mjs';
import '../../_chunks/dep-9888611c.mjs';
import '../../_chunks/dep-697a1365.mjs';
import '../../config-provider/type.mjs';

function useRowExpand(props, context) {
  var _toRefs = toRefs(props),
    expandedRowKeys = _toRefs.expandedRowKeys;
  var renderTNode = useTNodeJSX();
  var _useConfig = useConfig("table"),
    t = _useConfig.t,
    globalConfig = _useConfig.globalConfig;
  var _useGlobalIcon = useGlobalIcon({
      ChevronRightCircleIcon: ChevronRightCircleIcon
    }),
    ChevronRightCircleIcon$1 = _useGlobalIcon.ChevronRightCircleIcon;
  var _useClassName = useClassName(),
    tableExpandClasses = _useClassName.tableExpandClasses,
    positiveRotate90 = _useClassName.positiveRotate90,
    tableFullRowClasses = _useClassName.tableFullRowClasses;
  var _useDefaultValue = useDefaultValue(expandedRowKeys, props.defaultExpandedRowKeys || [], props.onExpandChange, "expandedRowKeys"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    tExpandedRowKeys = _useDefaultValue2[0],
    setTExpandedRowKeys = _useDefaultValue2[1];
  var showExpandedRow = computed(function () {
    return Boolean(props.expandedRow || context.slots.expandedRow || context.slots["expanded-row"]);
  });
  var showExpandIconColumn = computed(function () {
    return props.expandIcon !== false && showExpandedRow.value;
  });
  var isFirstColumnFixed = 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_1(row, props.rowKey || "id");
    var index = tExpandedRowKeys.value.indexOf(currentId);
    var newKeys = _toConsumableArray(tExpandedRowKeys.value);
    index !== -1 ? newKeys.splice(index, 1) : newKeys.push(currentId);
    setTExpandedRowKeys(newKeys, {
      expandedRowData: props.data.filter(function (t2) {
        return newKeys.includes(get_1(t2, props.rowKey || "id"));
      }),
      currentRowData: row
    });
  };
  var renderExpandIcon = function renderExpandIcon(_, p) {
    var row = p.row,
      rowIndex = p.rowIndex;
    var currentId = get_1(row, props.rowKey || "id");
    var expanded = tExpandedRowKeys.value.includes(currentId);
    var icon = renderTNode("expandIcon", {
      defaultNode: t(globalConfig.value.expandIcon) || createVNode(ChevronRightCircleIcon$1, null, null),
      params: {
        row: row,
        index: rowIndex
      }
    });
    if (!icon) return null;
    var classes = [tableExpandClasses.iconBox, tableExpandClasses[expanded ? "expanded" : "collapsed"], _defineProperty({}, positiveRotate90, expanded)];
    return createVNode("span", {
      "class": classes,
      "onClick": function onClick(e) {
        return onToggleExpand(e, row);
      }
    }, [icon]);
  };
  var getExpandColumn = function getExpandColumn() {
    var expandCol = {
      colKey: "__EXPAND_ROW_ICON_COLUMN__",
      width: 46,
      className: tableExpandClasses.iconCell,
      fixed: isFirstColumnFixed.value ? "left" : void 0,
      cell: renderExpandIcon,
      stopPropagation: true
    };
    return expandCol;
  };
  var renderExpandedRow = function renderExpandedRow(p) {
    var rowId = get_1(p.row, props.rowKey || "id");
    if (!tExpandedRowKeys.value || !tExpandedRowKeys.value.includes(rowId)) return null;
    var isFixedLeft = p.isWidthOverflow && props.columns.find(function (item) {
      return item.fixed === "left";
    });
    return createVNode("tr", {
      "key": "expand_".concat(rowId),
      "class": [tableExpandClasses.row, _defineProperty({}, tableFullRowClasses.base, isFixedLeft)]
    }, [createVNode("td", {
      "colspan": p.columns.length
    }, [createVNode("div", {
      "class": [tableExpandClasses.rowInner, _defineProperty({}, tableFullRowClasses.innerFullRow, isFixedLeft)],
      "style": isFixedLeft ? {
        width: "".concat(p.tableWidth, "px")
      } : {}
    }, [createVNode("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
  };
}

export { useRowExpand as default };
//# sourceMappingURL=useRowExpand.mjs.map
