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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var tdesignIconsVue = require('tdesign-icons-vue');
var cloneDeep = require('lodash/cloneDeep');
var get = require('lodash/get');
var _common_js_table_treeStore = require('../../_common/js/table/tree-store.js');
var table_hooks_useClassName = require('./useClassName.js');
var table_tr = require('../tr.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
var hooks_tnode = require('../../hooks/tnode.js');
var hooks_useGlobalIcon = require('../../hooks/useGlobalIcon.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('../../_common/js/table/utils.js');
require('lodash/isFunction');
require('lodash/isNumber');
require('../../_common/js/log/log.js');
require('../../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('../../_chunks/dep-d7a1e02c.js');
require('@babel/runtime/helpers/typeof');
require('lodash/upperFirst');
require('lodash/isString');
require('lodash/pick');
require('../utils.js');
require('lodash/isObject');
require('./useFixed.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/debounce');
require('../../_common/js/utils/getScrollbarWidth.js');
require('../../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../../utils/dom.js');
require('vue');
require('raf');
require('../../utils/easing.js');
require('../ellipsis.js');
require('../../utils/render-tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../tooltip/index.js');
require('../../tooltip/tooltip.js');
require('../../tooltip/props.js');
require('../../popup/props.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../utils/helper.js');
require('../../_common/js/utils/set-style.js');
require('../../popup/container.js');
require('../../config-provider/config-receiver.js');
require('../../utils/mixins.js');
require('../../utils/map-props.js');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../base-table-props.js');
require('./useRowspanAndColspan.js');
require('../../hooks/useLazyLoad.js');
require('../../_common/js/utils/observe.js');
require('lodash/isEmpty');
require('../../hooks/render-tnode.js');

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

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

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__default["default"](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 = VueCompositionAPI.toRefs(props),
      data = _toRefs.data,
      columns = _toRefs.columns;

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

  var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
    AddRectangleIcon: tdesignIconsVue.AddRectangleIcon,
    MinusRectangleIcon: tdesignIconsVue.MinusRectangleIcon
  }),
      AddRectangleIcon = _useGlobalIcon.AddRectangleIcon,
      MinusRectangleIcon = _useGlobalIcon.MinusRectangleIcon;

  var store = VueCompositionAPI.ref(new _common_js_table_treeStore["default"]());
  var treeNodeCol = VueCompositionAPI.ref();
  var dataSource = VueCompositionAPI.ref([]);

  var _useClassName = table_hooks_useClassName["default"](),
      tableTreeClasses = _useClassName.tableTreeClasses;

  var renderTNode = hooks_tnode.useTNodeDefault();
  var rowDataKeys = VueCompositionAPI.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"
    };
  });
  var checkedColumn = VueCompositionAPI.computed(function () {
    return columns.value.find(function (col) {
      return col.colKey === "row-select";
    });
  });
  VueCompositionAPI.watch(checkedColumn, function (column) {
    if (!store.value || !checkedColumn.value) return;
    store.value.updateDisabledState(dataSource.value, column, rowDataKeys.value);
  });

  function getFoldIcon(h, context2) {
    var params = _objectSpread(_objectSpread({}, context2), {}, {
      type: "fold"
    });

    var defaultFoldIcon = t(global.value.treeExpandAndFoldIcon, h, params) || h(MinusRectangleIcon);
    return renderTNode("treeExpandAndFoldIcon", {
      defaultNode: defaultFoldIcon,
      params: params
    });
  }

  function getExpandIcon(h, context2) {
    var params = _objectSpread(_objectSpread({}, context2), {}, {
      type: "expand"
    });

    var defaultExpandIcon = t(global.value.treeExpandAndFoldIcon, h, params) || h(AddRectangleIcon);
    return renderTNode("treeExpandAndFoldIcon", {
      defaultNode: defaultExpandIcon,
      params: params
    });
  }

  var uniqueKeys = VueCompositionAPI.computed(function () {
    var _store$value, _store$value$getAllUn;

    return ((_store$value = store.value) === null || _store$value === void 0 ? void 0 : (_store$value$getAllUn = _store$value.getAllUniqueKeys(data.value, rowDataKeys.value)) === null || _store$value$getAllUn === void 0 ? void 0 : _store$value$getAllUn.join()) || "";
  });
  VueCompositionAPI.watch([uniqueKeys], function () {
    if (!data.value) return [];

    if (!props.tree) {
      dataSource.value = data.value;
      return;
    }

    resetData(data.value);
  }, {
    immediate: true
  });
  VueCompositionAPI.onUnmounted(function () {
    var _store$value$treeData;

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

  function resetData(data2) {
    var _props$tree2;

    var newVal = cloneDeep__default["default"](data2);
    store.value.initialTreeStore(newVal, props.columns, rowDataKeys.value);

    if ((_props$tree2 = props.tree) !== null && _props$tree2 !== void 0 && _props$tree2.defaultExpandAll) {
      newVal = store.value.expandAll(newVal, rowDataKeys.value);
    }

    dataSource.value = newVal;
  }

  function getTreeNodeStyle(level) {
    var _props$tree3, _props$tree4;

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

  function toggleExpandData(p, trigger) {
    var _store$value2, _store$value2$treeDat, _props$onTreeExpandCh;

    dataSource.value = store.value.toggleExpandData(p, dataSource.value, rowDataKeys.value);
    var rowValue = get__default["default"](p.row, rowDataKeys.value.rowKey);
    var rowState = (_store$value2 = store.value) === null || _store$value2 === void 0 ? void 0 : (_store$value2$treeDat = _store$value2.treeDataMap) === null || _store$value2$treeDat === void 0 ? void 0 : _store$value2$treeDat.get(rowValue);
    var params = {
      row: p.row,
      rowIndex: p.rowIndex,
      rowState: rowState,
      trigger: trigger
    };
    (_props$onTreeExpandCh = props.onTreeExpandChange) === null || _props$onTreeExpandCh === void 0 ? void 0 : _props$onTreeExpandCh.call(props, params);
    context.emit("tree-expand-change", params);
  }

  function getTreeNodeColumnCol() {
    var _props$tree5;

    var columns2 = props.columns;
    var treeNodeColumnIndex = ((_props$tree5 = props.tree) === null || _props$tree5 === void 0 ? void 0 : _props$tree5.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 formatTreeColumn(col) {
    if (!props.tree || col.colKey !== treeNodeCol.value.colKey) return col;

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

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

      var classes = _defineProperty__default["default"]({}, tableTreeClasses.inlineCol, !!col.ellipsis);

      var childrenNodes = get__default["default"](p.row, rowDataKeys.value.childrenKey);

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

        var iconNode = (_store$value$treeData2 = store.value.treeDataMap.get(get__default["default"](p.row, rowDataKeys.value.rowKey))) !== null && _store$value$treeData2 !== void 0 && _store$value$treeData2.expanded ? getFoldIcon(h, p) : getExpandIcon(h, p);
        return h("div", {
          "class": [tableTreeClasses.col, classes],
          "style": colStyle
        }, [!!(childrenNodes.length || childrenNodes === true) && h("span", {
          "class": tableTreeClasses.icon,
          "on": {
            "click": function click() {
              return toggleExpandData(p, "expand-fold-icon");
            }
          }
        }, [iconNode]), cellInfo]);
      }

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

    if (newCol.ellipsis === true) {
      newCol.ellipsis = function (h, p) {
        return table_tr.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__default["default"](dataSource.value);

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

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

  function remove(key) {
    dataSource.value = _toConsumableArray__default["default"](store.value.remove(key, dataSource.value, rowDataKeys.value));
  }

  function appendTo() {
    var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
    var newData = arguments.length > 1 ? arguments[1] : undefined;

    if (!key) {
      dataSource.value = store.value.appendToRoot(newData, dataSource.value, rowDataKeys.value);
      return;
    }

    dataSource.value = _toConsumableArray__default["default"](store.value.appendTo(key, newData, dataSource.value, rowDataKeys.value));
  }

  function insertAfter(rowValue, newData) {
    dataSource.value = _toConsumableArray__default["default"](store.value.insertAfter(rowValue, newData, dataSource.value, rowDataKeys.value));
  }

  function insertBefore(rowValue, newData) {
    dataSource.value = _toConsumableArray__default["default"](store.value.insertBefore(rowValue, newData, dataSource.value, rowDataKeys.value));
  }

  function expandAll() {
    dataSource.value = _toConsumableArray__default["default"](store.value.expandAll(dataSource.value, rowDataKeys.value));
  }

  function foldAll() {
    dataSource.value = _toConsumableArray__default["default"](store.value.foldAll(dataSource.value, rowDataKeys.value));
  }

  function swapData(params) {
    var r = store.value.swapData(dataSource.value, params, rowDataKeys.value);

    if (r.result) {
      dataSource.value = _toConsumableArray__default["default"](r.dataSource);
    } else {
      var _props$onAbnormalDrag;

      var params2 = {
        code: r.code,
        reason: r.reason
      };
      (_props$onAbnormalDrag = props.onAbnormalDragSort) === null || _props$onAbnormalDrag === void 0 ? void 0 : _props$onAbnormalDrag.call(props, params2);
      context.emit("abnormal-drag-sort", params2);
    }
  }

  function getTreeNode() {
    return store.value.getTreeNode(dataSource.value, rowDataKeys.value);
  }

  function getTreeExpandedRow() {
    var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "data";
    return store.value.getTreeExpandedRow(dataSource.value, rowDataKeys.value, type);
  }

  return {
    store: store,
    rowDataKeys: rowDataKeys,
    dataSource: dataSource,
    swapData: swapData,
    setData: setData,
    getData: getData,
    remove: remove,
    appendTo: appendTo,
    insertAfter: insertAfter,
    insertBefore: insertBefore,
    formatTreeColumn: formatTreeColumn,
    toggleExpandData: toggleExpandData,
    expandAll: expandAll,
    foldAll: foldAll,
    getTreeNode: getTreeNode,
    resetData: resetData,
    getTreeExpandedRow: getTreeExpandedRow
  };
}

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