/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, ref, computed, watch, onUnmounted } from '@vue/composition-api';
import { MinusRectangleIcon, AddRectangleIcon } from 'tdesign-icons-vue';
import cloneDeep from 'lodash/cloneDeep';
import get from 'lodash/get';
import TableTreeStore from './tree-store.js';
import useClassName from './useClassName.js';
import { renderCell } from '../tr.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../utils.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/isFunction';
import '../../_common/js/log/index.js';
import '../../_common/js/log/log.js';
import '../../config-provider/useConfig.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '../../_chunks/dep-1ec74b8b.js';
import 'lodash/upperFirst';
import 'lodash/isString';
import 'lodash/pick';
import './useFixed.js';
import '../../_common/js/utils/getScrollbarWidth.js';
import '../../utils/dom.js';
import 'vue';
import 'raf';
import '../../utils/easing.js';
import '../ellipsis.js';
import '../../utils/render-tnode.js';
import 'lodash/isObject';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../config.js';
import '../../utils/classnames.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';
import '../../_common/js/utils/set-style.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../utils/map-props.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../../popup/type.js';
import '../base-table-props.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function useTreeData(props, context) {
  var _toRefs = toRefs(props),
      data = _toRefs.data,
      columns = _toRefs.columns;

  var store = ref(new TableTreeStore());
  var treeNodeCol = ref();
  var dataSource = ref([]);

  var _useClassName = useClassName(),
      tableTreeClasses = _useClassName.tableTreeClasses;

  var rowDataKeys = computed(function () {
    var _props$tree;

    return {
      rowKey: props.rowKey || "id",
      childrenKey: ((_props$tree = props.tree) === null || _props$tree === void 0 ? void 0 : _props$tree.childrenKey) || "children"
    };
  });
  watch([data], function (_ref) {
    var _ref2 = _slicedToArray(_ref, 1),
        val = _ref2[0];

    if (!val) return [];

    if (!props.tree || !Object.keys(props.tree).length) {
      dataSource.value = val;
      return;
    }

    var newVal = cloneDeep(val);
    dataSource.value = newVal;
    store.value.initialTreeStore(newVal, props.columns, rowDataKeys.value);
  }, {
    immediate: true
  });
  onUnmounted(function () {
    var _store$value$treeData;

    if (!props.tree || !Object.keys(props.tree).length) return;
    (_store$value$treeData = store.value.treeDataMap) === null || _store$value$treeData === void 0 ? void 0 : _store$value$treeData.clear();
    store.value = null;
  });
  watch([columns], function () {
    treeNodeCol.value = getTreeNodeColumnCol();
  }, {
    immediate: true
  });

  function getTreeNodeStyle(level) {
    var _props$tree2;

    if (level === void 0) return;
    var indent = ((_props$tree2 = props.tree) === null || _props$tree2 === void 0 ? void 0 : _props$tree2.indent) || 24;
    return {
      paddingLeft: "".concat(level * indent || 1, "px")
    };
  }

  function toggleExpandData(p) {
    dataSource.value = store.value.toggleExpandData(p, dataSource.value, rowDataKeys.value);
  }

  function getTreeNodeColumnCol() {
    var _props$tree3;

    var columns2 = props.columns;
    var treeNodeColumnIndex = ((_props$tree3 = props.tree) === null || _props$tree3 === void 0 ? void 0 : _props$tree3.treeNodeColumnIndex) || 0;

    while ((_columns2$treeNodeCol = columns2[treeNodeColumnIndex]) !== null && _columns2$treeNodeCol !== void 0 && _columns2$treeNodeCol.type || ((_columns2$treeNodeCol2 = columns2[treeNodeColumnIndex]) === null || _columns2$treeNodeCol2 === void 0 ? void 0 : _columns2$treeNodeCol2.colKey) === "__EXPAND_ROW_ICON_COLUMN__") {
      var _columns2$treeNodeCol, _columns2$treeNodeCol2;

      treeNodeColumnIndex += 1;
    }

    return columns2[treeNodeColumnIndex];
  }

  function formatTreeColum(col) {
    if (!props.tree || !Object.keys(props.tree).length || col.colKey !== treeNodeCol.value.colKey) return col;

    var newCol = _objectSpread({}, treeNodeCol.value);

    newCol.cell = function (h, p) {
      var cellInfo = renderCell(_objectSpread(_objectSpread({}, p), {}, {
        col: _objectSpread({}, treeNodeCol.value)
      }), context.slots);
      var currentState = store.value.treeDataMap.get(get(p.row, rowDataKeys.value.rowKey));
      var colStyle = getTreeNodeStyle(currentState === null || currentState === void 0 ? void 0 : currentState.level);

      var classes = _defineProperty({}, tableTreeClasses.inlineCol, !!col.ellipsis);

      var childrenNodes = get(p.row, rowDataKeys.value.childrenKey);

      if (childrenNodes && childrenNodes instanceof Array) {
        var _store$value$treeData2;

        var IconNode = (_store$value$treeData2 = store.value.treeDataMap.get(get(p.row, rowDataKeys.value.rowKey))) !== null && _store$value$treeData2 !== void 0 && _store$value$treeData2.expanded ? MinusRectangleIcon : AddRectangleIcon;
        return h("div", {
          "class": [tableTreeClasses.col, classes],
          "style": colStyle
        }, [!!childrenNodes.length && h(IconNode, {
          "class": tableTreeClasses.icon,
          "on": {
            "click": function click() {
              return toggleExpandData(p);
            }
          }
        }), cellInfo]);
      }

      return h("div", {
        "style": colStyle,
        "class": classes
      }, [cellInfo]);
    };

    if (newCol.ellipsis === true) {
      newCol.ellipsis = function (h, p) {
        return renderCell(_objectSpread(_objectSpread({}, p), {}, {
          col: _objectSpread({}, treeNodeCol.value)
        }), context.slots);
      };
    }

    return newCol;
  }

  function setData(key, newRowData) {
    var rowIndex = store.value.updateData(key, newRowData, dataSource.value, rowDataKeys.value);

    var newData = _toConsumableArray(dataSource.value);

    newData[rowIndex] = newRowData;
    dataSource.value = newData;
  }

  function getData(key) {
    return store.value.getData(key);
  }

  function remove(key) {
    dataSource.value = store.value.remove(key, dataSource.value, rowDataKeys.value);
  }

  function appendTo(key, newData) {
    dataSource.value = store.value.appendTo(key, newData, dataSource.value, rowDataKeys.value);
  }

  return {
    store: store,
    rowDataKeys: rowDataKeys,
    dataSource: dataSource,
    setData: setData,
    getData: getData,
    remove: remove,
    appendTo: appendTo,
    formatTreeColum: formatTreeColum
  };
}

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