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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var iconsVue = require('@ldesign/icons-vue');
var get = require('lodash/get');
var hooks_lnode = require('../../../hooks/lnode.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
var hooks_useGlobalIcon = require('../../../hooks/use-global-icon.js');
var components_table_hooks_useClassName = require('./useClassName.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../hooks/render-lnode.js');
require('lodash');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');

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

var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

function useRowExpand(props, context) {
  const {
    expandedRowKeys
  } = VueCompositionAPI.toRefs(props);
  const renderTNode = hooks_lnode.useLNodeJSX();
  const {
    t,
    global
  } = components_configProvider_useConfig.useConfig("table");
  const {
    ChevronRightCircleIcon
  } = hooks_useGlobalIcon.useGlobalIcon({
    ChevronRightCircleIcon: iconsVue.ChevronRightCircleIcon
  });
  const {
    tableExpandClasses,
    positiveRotate90,
    tableFullRowClasses
  } = components_table_hooks_useClassName["default"]();
  const [tExpandedRowKeys, setTExpandedRowKeys] = hooks_useDefaultValue["default"](expandedRowKeys, props.defaultExpandedRowKeys || [], props.onExpandChange, "expandedRowKeys", "expand-change");
  const showExpandedRow = VueCompositionAPI.computed(() => Boolean(props.expandedRow || context.slots.expandedRow || context.slots["expanded-row"]));
  const showExpandIconColumn = VueCompositionAPI.computed(() => props.expandIcon !== false && showExpandedRow.value);
  const isFirstColumnFixed = VueCompositionAPI.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__default["default"](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__default["default"](t2, props.rowKey || "id"))),
      currentRowData: row
    });
  };
  const renderExpandIcon = (h, p) => {
    const {
      row,
      rowIndex
    } = p;
    const currentId = get__default["default"](row, props.rowKey || "id");
    const expanded = tExpandedRowKeys.value.includes(currentId);
    const icon = renderTNode("expandIcon", {
      defaultNode: t(global.value.expandIcon) || h(ChevronRightCircleIcon),
      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__default["default"](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
  };
}

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