/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { toRefs, computed } from '@vue/composition-api';
import { ChevronRightCircleIcon } from '@ldesign/icons-vue';
import get from 'lodash/get';
import { useLNodeJSX } from '../../../hooks/lnode.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { useConfig } from '../../config-provider/useConfig.js';
import { useGlobalIcon } from '../../../hooks/use-global-icon.js';
import useClassName from './useClassName.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../hooks/render-lnode.js';
import 'lodash';
import '../../config-provider/context.js';
import '../../../_chunks/dep-def4c2ce.js';

function useRowExpand(props, context) {
  const {
    expandedRowKeys
  } = toRefs(props);
  const renderTNode = useLNodeJSX();
  const {
    t,
    global
  } = useConfig("table");
  const {
    ChevronRightCircleIcon: ChevronRightCircleIcon$1
  } = useGlobalIcon({
    ChevronRightCircleIcon: ChevronRightCircleIcon
  });
  const {
    tableExpandClasses,
    positiveRotate90,
    tableFullRowClasses
  } = useClassName();
  const [tExpandedRowKeys, setTExpandedRowKeys] = useDefaultValue(expandedRowKeys, props.defaultExpandedRowKeys || [], props.onExpandChange, "expandedRowKeys", "expand-change");
  const showExpandedRow = computed(() => Boolean(props.expandedRow || context.slots.expandedRow || context.slots["expanded-row"]));
  const showExpandIconColumn = computed(() => props.expandIcon !== false && showExpandedRow.value);
  const isFirstColumnFixed = computed(() => {
    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";
  });
  const onToggleExpand = (e, row) => {
    props.expandOnRowClick && e.stopPropagation();
    const currentId = get(row, props.rowKey || "id");
    const index = tExpandedRowKeys.value.indexOf(currentId);
    const newKeys = [...tExpandedRowKeys.value];
    index !== -1 ? newKeys.splice(index, 1) : newKeys.push(currentId);
    setTExpandedRowKeys(newKeys, {
      expandedRowData: props.data.filter(t2 => newKeys.includes(get(t2, props.rowKey || "id"))),
      currentRowData: row
    });
  };
  const renderExpandIcon = (h, p) => {
    const {
      row,
      rowIndex
    } = p;
    const currentId = get(row, props.rowKey || "id");
    const expanded = tExpandedRowKeys.value.includes(currentId);
    const icon = renderTNode("expandIcon", {
      defaultNode: t(global.value.expandIcon) || h(ChevronRightCircleIcon$1),
      params: {
        row,
        index: rowIndex
      }
    });
    if (!icon) return null;
    const classes = [tableExpandClasses.iconBox, tableExpandClasses[expanded ? "expanded" : "collapsed"], {
      [positiveRotate90]: expanded
    }];
    return h("span", {
      "class": classes,
      "on": {
        "click": e => onToggleExpand(e, row)
      }
    }, [icon]);
  };
  const getExpandColumn = h => {
    const expandCol = {
      colKey: "__EXPAND_ROW_ICON_COLUMN__",
      width: 64,
      className: tableExpandClasses.iconCell,
      fixed: isFirstColumnFixed.value ? "left" : void 0,
      cell: renderExpandIcon
    };
    return expandCol;
  };
  const renderExpandedRow = (h, p) => {
    const rowId = get(p.row, props.rowKey || "id");
    if (!tExpandedRowKeys.value || !tExpandedRowKeys.value.includes(rowId)) return null;
    const isFixedLeft = p.isWidthOverflow && props.columns.find(item => item.fixed === "left");
    return h("tr", {
      "key": `expand_${rowId}`,
      "class": [tableExpandClasses.row, {
        [tableFullRowClasses.base]: isFixedLeft
      }]
    }, [h("td", {
      "attrs": {
        "colspan": p.columns.length
      }
    }, [h("div", {
      "class": [tableExpandClasses.rowInner, {
        [tableFullRowClasses.innerFullRow]: isFixedLeft
      }],
      "style": isFixedLeft ? {
        width: `${p.tableWidth}px`
      } : {}
    }, [h("div", {
      "class": tableFullRowClasses.innerFullElement
    }, [renderTNode("expandedRow", {
      params: p
    })])])])]);
  };
  const onInnerExpandRowClick = p => {
    onToggleExpand(p.e, p.row);
  };
  return {
    showExpandedRow,
    showExpandIconColumn,
    getExpandColumn,
    renderExpandedRow,
    onInnerExpandRowClick
  };
}

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