/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import Vue from 'vue';
import { MinusRectangleIcon, AddRectangleIcon } from 'tdesign-icons-vue';
import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
import baseTableProps from '../base-table-props.js';
import primaryTableProps from '../primary-table-props.js';
import enhancedTableProps from '../enhanced-table-props.js';
import { getCell } from '../util/common.js';
import TableTreeStore from './tree-store.js';
import 'lodash/isFunction';
import 'lodash/isString';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';

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; }
var TreeData = Vue.extend({
  props: {
    rowKey: baseTableProps.rowKey,
    data: baseTableProps.data,
    columns: primaryTableProps.columns,
    tree: enhancedTableProps.tree
  },
  data: function data() {
    return {
      store: new TableTreeStore(),
      dataSource: []
    };
  },
  computed: {
    rowDataKeys: function rowDataKeys() {
      return {
        rowKey: this.rowKey,
        childrenKey: this.childrenKey
      };
    },
    childrenKey: function childrenKey() {
      var _this$tree;

      return ((_this$tree = this.tree) === null || _this$tree === void 0 ? void 0 : _this$tree.childrenKey) || "children";
    },
    columnsSource: function columnsSource() {
      var _this$tree2,
          _this$columns$treeNod,
          _this = this;

      var h = this.$createElement;
      var treeNodeColumnIndex = ((_this$tree2 = this.tree) === null || _this$tree2 === void 0 ? void 0 : _this$tree2.treeNodeColumnIndex) || 0;

      if ((_this$columns$treeNod = this.columns[treeNodeColumnIndex]) !== null && _this$columns$treeNod !== void 0 && _this$columns$treeNod.type) {
        treeNodeColumnIndex += 1;
      }

      var cols = _toConsumableArray(this.columns);

      var treeNodeCol = _objectSpread({}, this.columns[treeNodeColumnIndex]);

      treeNodeCol.cell = function (h, p) {
        var cellInfo = getCell(_this, _objectSpread(_objectSpread({}, p), {}, {
          col: _this.columns[treeNodeColumnIndex]
        }));

        var currentState = _this.store.treeDataMap.get(get(p.row, _this.rowKey));

        var colStyle = _this.getTreeNodeStyle(currentState === null || currentState === void 0 ? void 0 : currentState.level);

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

        if (childrenNodes && childrenNodes instanceof Array) {
          var _this$store$treeDataM;

          var IconNode = (_this$store$treeDataM = _this.store.treeDataMap.get(get(p.row, _this.rowKey))) !== null && _this$store$treeDataM !== void 0 && _this$store$treeDataM.expanded ? MinusRectangleIcon : AddRectangleIcon;
          return h("div", {
            "style": colStyle
          }, [!!childrenNodes.length && h(IconNode, {
            "style": {
              marginRight: "8px"
            },
            "on": {
              "click": function click() {
                return _this.toggleExpandData(p);
              }
            }
          }), cellInfo]);
        }

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

      cols[treeNodeColumnIndex] = treeNodeCol;
      return cols;
    }
  },
  watch: {
    data: {
      immediate: true,
      handler: function handler(val) {
        this.dataSource = _toConsumableArray(cloneDeep(val));
        this.store.initialTreeStore(this.dataSource, this.columns, this.rowDataKeys);
      }
    }
  },
  destroyed: function destroyed() {
    var _this$store$treeDataM2;

    (_this$store$treeDataM2 = this.store.treeDataMap) === null || _this$store$treeDataM2 === void 0 ? void 0 : _this$store$treeDataM2.clear();
    this.store = null;
  },
  methods: {
    getTreeNodeStyle: function getTreeNodeStyle(level) {
      var _this$tree3;

      if (!level) return;
      var indent = ((_this$tree3 = this.tree) === null || _this$tree3 === void 0 ? void 0 : _this$tree3.indent) || 24;
      return {
        paddingLeft: "".concat(level * indent, "px")
      };
    },
    toggleExpandData: function toggleExpandData(p) {
      this.store.toggleExpandData(p, this.dataSource, this.rowDataKeys);
    },
    setData: function setData(key, newRowData) {
      var rowIndex = this.store.updateData(key, newRowData, this.dataSource, this.rowDataKeys);
      this.$set(this.dataSource, rowIndex, newRowData);
    },
    getData: function getData(key) {
      return this.store.getData(key);
    },
    remove: function remove(key) {
      this.store.remove(key, this.dataSource, this.rowDataKeys);
    },
    appendTo: function appendTo(key, newData) {
      this.store.appendTo(key, newData, this.dataSource, this.rowDataKeys);
    }
  }
});

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