"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _vue = require("vue");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _conf = _interopRequireDefault(require("../../v-x-e-table/src/conf"));
var _vXETable = require("../../v-x-e-table");
var _utils = require("../../tools/utils");
var _util = require("../../table/src/util");
var _dom = require("../../tools/dom");
var _log = require("../../tools/log");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from, pack) {
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
    if (ar || !(i in from)) {
      if (!ar) ar = Array.prototype.slice.call(from, 0, i);
      ar[i] = from[i];
    }
  }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var tableEditMethodKeys = ['insert', 'insertAt', 'insertNextAt', 'remove', 'removeCheckboxRow', 'removeRadioRow', 'removeCurrentRow', 'getRecordset', 'getInsertRecords', 'getRemoveRecords', 'getUpdateRecords', 'getEditRecord', 'getActiveRecord', 'getSelectedCell', 'clearEdit', 'clearActived', 'clearSelected', 'isEditByRow', 'isActiveByRow', 'setEditRow', 'setActiveRow', 'setEditCell', 'setActiveCell', 'setSelectCell'];
var editHook = {
  setupTable: function ($xetable) {
    var props = $xetable.props,
      reactData = $xetable.reactData,
      internalData = $xetable.internalData;
    var refElem = $xetable.getRefMaps().refElem;
    var _a = $xetable.getComputeMaps(),
      computeMouseOpts = _a.computeMouseOpts,
      computeEditOpts = _a.computeEditOpts,
      computeCheckboxOpts = _a.computeCheckboxOpts,
      computeTreeOpts = _a.computeTreeOpts;
    var editMethods = {};
    var editPrivateMethods = {};
    var getEditColumnModel = function (row, column) {
      var model = column.model,
        editRender = column.editRender;
      if (editRender) {
        model.value = (0, _util.getCellValue)(row, column);
        model.update = false;
      }
    };
    var setEditColumnModel = function (row, column) {
      var model = column.model,
        editRender = column.editRender;
      if (editRender && model.update) {
        (0, _util.setCellValue)(row, column, model.value);
        model.update = false;
        model.value = null;
      }
    };
    var removeCellSelectedClass = function () {
      var el = refElem.value;
      if (el) {
        var cell = el.querySelector('.col--selected');
        if (cell) {
          (0, _dom.removeClass)(cell, 'col--selected');
        }
      }
    };
    function syncActivedCell() {
      var editStore = reactData.editStore,
        tableColumn = reactData.tableColumn;
      var editOpts = computeEditOpts.value;
      var actived = editStore.actived;
      var row = actived.row,
        column = actived.column;
      if (row || column) {
        if (editOpts.mode === 'row') {
          tableColumn.forEach(function (column) {
            return setEditColumnModel(row, column);
          });
        } else {
          setEditColumnModel(row, column);
        }
      }
    }
    function insertTreeRow(newRecords, isAppend) {
      var tableFullTreeData = internalData.tableFullTreeData,
        afterFullData = internalData.afterFullData,
        fullDataRowIdData = internalData.fullDataRowIdData,
        fullAllDataRowIdData = internalData.fullAllDataRowIdData;
      var treeOpts = computeTreeOpts.value;
      var rowField = treeOpts.rowField,
        parentField = treeOpts.parentField,
        mapChildrenField = treeOpts.mapChildrenField;
      var childrenField = treeOpts.children || treeOpts.childrenField;
      var funcName = isAppend ? 'push' : 'unshift';
      newRecords.forEach(function (item) {
        var parentRowId = item[parentField];
        var rowid = (0, _util.getRowid)($xetable, item);
        var matchObj = parentRowId ? _xeUtils.default.findTree(tableFullTreeData, function (item) {
          return parentRowId === item[rowField];
        }, {
          children: mapChildrenField
        }) : null;
        if (matchObj) {
          var parentRow = matchObj.item;
          var parentRest = fullAllDataRowIdData[(0, _util.getRowid)($xetable, parentRow)];
          var parentLevel = parentRest ? parentRest.level : 0;
          var parentChilds = parentRow[childrenField];
          var mapChilds = parentRow[mapChildrenField];
          if (!_xeUtils.default.isArray(parentChilds)) {
            parentChilds = parentRow[childrenField] = [];
          }
          if (!_xeUtils.default.isArray(mapChilds)) {
            mapChilds = parentRow[childrenField] = [];
          }
          parentChilds[funcName](item);
          mapChilds[funcName](item);
          var rest = {
            row: item,
            rowid: rowid,
            seq: -1,
            index: -1,
            _index: -1,
            $index: -1,
            items: parentChilds,
            parent: parentRow,
            level: parentLevel + 1
          };
          fullDataRowIdData[rowid] = rest;
          fullAllDataRowIdData[rowid] = rest;
        } else {
          if (process.env.NODE_ENV === 'development') {
            if (parentRowId) {
              (0, _log.warnLog)('vxe.error.unableInsert');
            }
          }
          afterFullData[funcName](item);
          tableFullTreeData[funcName](item);
          var rest = {
            row: item,
            rowid: rowid,
            seq: -1,
            index: -1,
            _index: -1,
            $index: -1,
            items: tableFullTreeData,
            parent: null,
            level: 0
          };
          fullDataRowIdData[rowid] = rest;
          fullAllDataRowIdData[rowid] = rest;
        }
      });
    }
    var handleInsertRowAt = function (records, row, isInsertNextRow) {
      var treeConfig = props.treeConfig;
      var mergeList = reactData.mergeList,
        editStore = reactData.editStore;
      var tableFullTreeData = internalData.tableFullTreeData,
        afterFullData = internalData.afterFullData,
        tableFullData = internalData.tableFullData,
        fullDataRowIdData = internalData.fullDataRowIdData,
        fullAllDataRowIdData = internalData.fullAllDataRowIdData;
      var treeOpts = computeTreeOpts.value;
      var transform = treeOpts.transform,
        rowField = treeOpts.rowField,
        mapChildrenField = treeOpts.mapChildrenField;
      var childrenField = treeOpts.children || treeOpts.childrenField;
      if (!_xeUtils.default.isArray(records)) {
        records = [records];
      }
      var newRecords = (0, _vue.reactive)($xetable.defineField(records.map(function (record) {
        var _a;
        return Object.assign(treeConfig && transform ? (_a = {}, _a[mapChildrenField] = [], _a[childrenField] = [], _a) : {}, record);
      })));
      if (_xeUtils.default.eqNull(row)) {
        // 如果为虚拟树
        if (treeConfig && transform) {
          insertTreeRow(newRecords, false);
        } else {
          afterFullData.unshift.apply(afterFullData, newRecords);
          tableFullData.unshift.apply(tableFullData, newRecords);
          // 刷新单元格合并
          mergeList.forEach(function (mergeItem) {
            var mergeRowIndex = mergeItem.row;
            if (mergeRowIndex > 0) {
              mergeItem.row = mergeRowIndex + newRecords.length;
            }
          });
        }
      } else {
        if (row === -1) {
          // 如果为虚拟树
          if (treeConfig && transform) {
            insertTreeRow(newRecords, true);
          } else {
            afterFullData.push.apply(afterFullData, newRecords);
            tableFullData.push.apply(tableFullData, newRecords);
            // 刷新单元格合并
            mergeList.forEach(function (mergeItem) {
              var mergeRowIndex = mergeItem.row,
                mergeRowspan = mergeItem.rowspan;
              if (mergeRowIndex + mergeRowspan > afterFullData.length) {
                mergeItem.rowspan = mergeRowspan + newRecords.length;
              }
            });
          }
        } else {
          // 如果为虚拟树
          if (treeConfig && transform) {
            var matchMapObj_1 = _xeUtils.default.findTree(tableFullTreeData, function (item) {
              return row[rowField] === item[rowField];
            }, {
              children: mapChildrenField
            });
            if (matchMapObj_1) {
              var parentRow_1 = matchMapObj_1.parent;
              var parentMapChilds_1 = parentRow_1 ? parentRow_1[mapChildrenField] : tableFullTreeData;
              var parentRest = fullAllDataRowIdData[(0, _util.getRowid)($xetable, parentRow_1)];
              var parentLevel_1 = parentRest ? parentRest.level : 0;
              newRecords.forEach(function (item, i) {
                var rowid = (0, _util.getRowid)($xetable, item);
                if (process.env.NODE_ENV === 'development') {
                  if (item[treeOpts.parentField]) {
                    if (parentRow_1 && item[treeOpts.parentField] !== parentRow_1[rowField]) {
                      (0, _log.errLog)('vxe.error.errProp', ["".concat(treeOpts.parentField, "=").concat(item[treeOpts.parentField]), "".concat(treeOpts.parentField, "=").concat(parentRow_1[rowField])]);
                    }
                  }
                }
                if (parentRow_1) {
                  item[treeOpts.parentField] = parentRow_1[rowField];
                }
                var targetIndex = matchMapObj_1.index + i;
                if (isInsertNextRow) {
                  targetIndex = targetIndex + 1;
                }
                parentMapChilds_1.splice(targetIndex, 0, item);
                var rest = {
                  row: item,
                  rowid: rowid,
                  seq: -1,
                  index: -1,
                  _index: -1,
                  $index: -1,
                  items: parentMapChilds_1,
                  parent: parentRow_1,
                  level: parentLevel_1 + 1
                };
                fullDataRowIdData[rowid] = rest;
                fullAllDataRowIdData[rowid] = rest;
              });
              // 源
              if (parentRow_1) {
                var matchObj = _xeUtils.default.findTree(tableFullTreeData, function (item) {
                  return row[rowField] === item[rowField];
                }, {
                  children: childrenField
                });
                if (matchObj) {
                  var parentChilds = matchObj.items;
                  var targetIndex = matchObj.index;
                  if (isInsertNextRow) {
                    targetIndex = targetIndex + 1;
                  }
                  parentChilds.splice.apply(parentChilds, __spreadArray([targetIndex, 0], newRecords, false));
                }
              }
            } else {
              if (process.env.NODE_ENV === 'development') {
                (0, _log.warnLog)('vxe.error.unableInsert');
              }
              insertTreeRow(newRecords, true);
            }
          } else {
            if (treeConfig) {
              throw new Error((0, _log.getLog)('vxe.error.noTree', ['insert']));
            }
            var afIndex_1 = -1;
            // 如果是可视索引
            if (_xeUtils.default.isNumber(row)) {
              if (row < afterFullData.length) {
                afIndex_1 = row;
              }
            } else {
              afIndex_1 = $xetable.findRowIndexOf(afterFullData, row);
            }
            // 如果是插入指定行的下一行
            if (isInsertNextRow) {
              afIndex_1 = Math.min(afterFullData.length, afIndex_1 + 1);
            }
            if (afIndex_1 === -1) {
              throw new Error((0, _log.errLog)('vxe.error.unableInsert'));
            }
            afterFullData.splice.apply(afterFullData, __spreadArray([afIndex_1, 0], newRecords, false));
            tableFullData.splice.apply(tableFullData, __spreadArray([$xetable.findRowIndexOf(tableFullData, row), 0], newRecords, false));
            // 刷新单元格合并
            mergeList.forEach(function (mergeItem) {
              var mergeRowIndex = mergeItem.row,
                mergeRowspan = mergeItem.rowspan;
              if (mergeRowIndex > afIndex_1) {
                mergeItem.row = mergeRowIndex + newRecords.length;
              } else if (mergeRowIndex + mergeRowspan > afIndex_1) {
                mergeItem.rowspan = mergeRowspan + newRecords.length;
              }
            });
          }
        }
      }
      var insertMaps = editStore.insertMaps;
      newRecords.forEach(function (newRow) {
        var rowid = (0, _util.getRowid)($xetable, newRow);
        insertMaps[rowid] = newRow;
      });
      $xetable.cacheRowMap();
      $xetable.updateScrollYStatus();
      $xetable.handleTableData(treeConfig && transform);
      if (!(treeConfig && transform)) {
        $xetable.updateAfterDataIndex();
      }
      $xetable.updateFooter();
      $xetable.checkSelectionStatus();
      if (reactData.scrollYLoad) {
        $xetable.updateScrollYSpace();
      }
      return (0, _vue.nextTick)().then(function () {
        $xetable.updateCellAreas();
        return $xetable.recalculate();
      }).then(function () {
        return {
          row: newRecords.length ? newRecords[newRecords.length - 1] : null,
          rows: newRecords
        };
      });
    };
    editMethods = {
      /**
       * 往表格中插入临时数据
       *
       * @param {*} records
       */
      insert: function (records) {
        return handleInsertRowAt(records, null);
      },
      /**
       * 往表格指定行中插入临时数据
       * 如果 row 为空则从插入到顶部，如果为树结构，则插入到目标节点顶部
       * 如果 row 为 -1 则从插入到底部，如果为树结构，则插入到目标节点底部
       * 如果 row 为有效行则插入到该行的位置，如果为树结构，则有插入到效的目标节点该行的位置
       * @param {Object/Array} records 新的数据
       * @param {Row} row 指定行
       */
      insertAt: function (records, row) {
        return handleInsertRowAt(records, row);
      },
      insertNextAt: function (records, row) {
        return handleInsertRowAt(records, row, true);
      },
      /**
       * 删除指定行数据
       * 如果传 row 则删除一行
       * 如果传 rows 则删除多行
       * 如果为空则删除所有
       */
      remove: function (rows) {
        var treeConfig = props.treeConfig;
        var mergeList = reactData.mergeList,
          editStore = reactData.editStore,
          selectCheckboxMaps = reactData.selectCheckboxMaps;
        var tableFullTreeData = internalData.tableFullTreeData,
          afterFullData = internalData.afterFullData,
          tableFullData = internalData.tableFullData;
        var checkboxOpts = computeCheckboxOpts.value;
        var treeOpts = computeTreeOpts.value;
        var transform = treeOpts.transform,
          mapChildrenField = treeOpts.mapChildrenField;
        var childrenField = treeOpts.children || treeOpts.childrenField;
        var actived = editStore.actived,
          removeMaps = editStore.removeMaps,
          insertMaps = editStore.insertMaps;
        var checkField = checkboxOpts.checkField;
        var delList = [];
        if (!rows) {
          rows = tableFullData;
        } else if (!_xeUtils.default.isArray(rows)) {
          rows = [rows];
        }
        // 如果是新增，则保存记录
        rows.forEach(function (row) {
          if (!$xetable.isInsertByRow(row)) {
            var rowid = (0, _util.getRowid)($xetable, row);
            removeMaps[rowid] = row;
          }
        });
        // 如果绑定了多选属性，则更新状态
        if (!checkField) {
          var selectRowMaps_1 = __assign({}, selectCheckboxMaps);
          rows.forEach(function (row) {
            var rowid = (0, _util.getRowid)($xetable, row);
            if (selectRowMaps_1[rowid]) {
              delete selectRowMaps_1[rowid];
            }
          });
          reactData.selectCheckboxMaps = selectRowMaps_1;
        }
        // 从数据源中移除
        if (tableFullData === rows) {
          rows = delList = tableFullData.slice(0);
          internalData.tableFullData = [];
          internalData.afterFullData = [];
          $xetable.clearMergeCells();
        } else {
          // 如果为虚拟树
          if (treeConfig && transform) {
            rows.forEach(function (row) {
              var rowid = (0, _util.getRowid)($xetable, row);
              var matchMapObj = _xeUtils.default.findTree(tableFullTreeData, function (item) {
                return rowid === (0, _util.getRowid)($xetable, item);
              }, {
                children: mapChildrenField
              });
              if (matchMapObj) {
                var rItems = matchMapObj.items.splice(matchMapObj.index, 1);
                delList.push(rItems[0]);
              }
              var matchObj = _xeUtils.default.findTree(tableFullTreeData, function (item) {
                return rowid === (0, _util.getRowid)($xetable, item);
              }, {
                children: childrenField
              });
              if (matchObj) {
                matchObj.items.splice(matchObj.index, 1);
              }
              var afIndex = $xetable.findRowIndexOf(afterFullData, row);
              if (afIndex > -1) {
                afterFullData.splice(afIndex, 1);
              }
            });
          } else {
            rows.forEach(function (row) {
              var tfIndex = $xetable.findRowIndexOf(tableFullData, row);
              if (tfIndex > -1) {
                var rItems = tableFullData.splice(tfIndex, 1);
                delList.push(rItems[0]);
              }
              var afIndex = $xetable.findRowIndexOf(afterFullData, row);
              if (afIndex > -1) {
                // 刷新单元格合并
                mergeList.forEach(function (mergeItem) {
                  var mergeRowIndex = mergeItem.row,
                    mergeRowspan = mergeItem.rowspan;
                  if (mergeRowIndex > afIndex) {
                    mergeItem.row = mergeRowIndex - 1;
                  } else if (mergeRowIndex + mergeRowspan > afIndex) {
                    mergeItem.rowspan = mergeRowspan - 1;
                  }
                });
                afterFullData.splice(afIndex, 1);
              }
            });
          }
        }
        // 如果当前行被激活编辑，则清除激活状态
        if (actived.row && $xetable.findRowIndexOf(rows, actived.row) > -1) {
          editMethods.clearEdit();
        }
        // 从新增中移除已删除的数据
        rows.forEach(function (row) {
          var rowid = (0, _util.getRowid)($xetable, row);
          if (insertMaps[rowid]) {
            delete insertMaps[rowid];
          }
        });
        $xetable.updateFooter();
        $xetable.cacheRowMap();
        $xetable.handleTableData(treeConfig && transform);
        if (!(treeConfig && transform)) {
          $xetable.updateAfterDataIndex();
        }
        $xetable.checkSelectionStatus();
        if (reactData.scrollYLoad) {
          $xetable.updateScrollYSpace();
        }
        return (0, _vue.nextTick)().then(function () {
          $xetable.updateCellAreas();
          return $xetable.recalculate();
        }).then(function () {
          return {
            row: delList.length ? delList[delList.length - 1] : null,
            rows: delList
          };
        });
      },
      /**
       * 删除复选框选中的数据
       */
      removeCheckboxRow: function () {
        return editMethods.remove($xetable.getCheckboxRecords()).then(function (params) {
          $xetable.clearCheckboxRow();
          return params;
        });
      },
      /**
       * 删除单选框选中的数据
       */
      removeRadioRow: function () {
        var radioRecord = $xetable.getRadioRecord();
        return editMethods.remove(radioRecord || []).then(function (params) {
          $xetable.clearRadioRow();
          return params;
        });
      },
      /**
       * 删除当前行选中的数据
       */
      removeCurrentRow: function () {
        var currentRecord = $xetable.getCurrentRecord();
        return editMethods.remove(currentRecord || []).then(function (params) {
          $xetable.clearCurrentRow();
          return params;
        });
      },
      /**
       * 获取表格数据集，包含新增、删除、修改、标记
       */
      getRecordset: function () {
        return {
          insertRecords: editMethods.getInsertRecords(),
          removeRecords: editMethods.getRemoveRecords(),
          updateRecords: editMethods.getUpdateRecords(),
          pendingRecords: $xetable.getPendingRecords()
        };
      },
      /**
       * 获取新增的临时数据
       */
      getInsertRecords: function () {
        var editStore = reactData.editStore;
        var fullAllDataRowIdData = internalData.fullAllDataRowIdData;
        var insertMaps = editStore.insertMaps;
        var insertRecords = [];
        _xeUtils.default.each(insertMaps, function (row, rowid) {
          if (fullAllDataRowIdData[rowid]) {
            insertRecords.push(row);
          }
        });
        return insertRecords;
      },
      /**
       * 获取已删除的数据
       */
      getRemoveRecords: function () {
        var editStore = reactData.editStore;
        var removeMaps = editStore.removeMaps;
        var removeRecords = [];
        _xeUtils.default.each(removeMaps, function (row) {
          removeRecords.push(row);
        });
        return removeRecords;
      },
      /**
       * 获取更新数据
       * 只精准匹配 row 的更改
       * 如果是树表格，子节点更改状态不会影响父节点的更新状态
       */
      getUpdateRecords: function () {
        var keepSource = props.keepSource,
          treeConfig = props.treeConfig;
        var tableFullData = internalData.tableFullData;
        var treeOpts = computeTreeOpts.value;
        if (keepSource) {
          syncActivedCell();
          if (treeConfig) {
            return _xeUtils.default.filterTree(tableFullData, function (row) {
              return $xetable.isUpdateByRow(row);
            }, treeOpts);
          }
          return tableFullData.filter(function (row) {
            return $xetable.isUpdateByRow(row);
          });
        }
        return [];
      },
      getActiveRecord: function () {
        return this.getEditRecord();
      },
      getEditRecord: function () {
        var editStore = reactData.editStore;
        var afterFullData = internalData.afterFullData;
        var el = refElem.value;
        var _a = editStore.actived,
          args = _a.args,
          row = _a.row;
        if (args && $xetable.findRowIndexOf(afterFullData, row) > -1 && el.querySelectorAll('.vxe-body--column.col--active').length) {
          return Object.assign({}, args);
        }
        return null;
      },
      /**
       * 获取选中的单元格
       */
      getSelectedCell: function () {
        var editStore = reactData.editStore;
        var _a = editStore.selected,
          args = _a.args,
          column = _a.column;
        if (args && column) {
          return Object.assign({}, args);
        }
        return null;
      },
      clearActived: function (evnt) {
        // if (process.env.NODE_ENV === 'development') {
        //   warnLog('vxe.error.delFunc', ['clearActived', 'clearEdit'])
        // }
        // 即将废弃
        return this.clearEdit(evnt);
      },
      /**
       * 清除激活的编辑
       */
      clearEdit: function (evnt) {
        var editStore = reactData.editStore;
        var actived = editStore.actived;
        var row = actived.row,
          column = actived.column;
        if (row || column) {
          syncActivedCell();
          actived.args = null;
          actived.row = null;
          actived.column = null;
          $xetable.updateFooter();
          $xetable.dispatchEvent('edit-closed', {
            row: row,
            rowIndex: $xetable.getRowIndex(row),
            $rowIndex: $xetable.getVMRowIndex(row),
            column: column,
            columnIndex: $xetable.getColumnIndex(column),
            $columnIndex: $xetable.getVMColumnIndex(column)
          }, evnt || null);
        }
        if (_conf.default.cellVaildMode === 'obsolete') {
          if ($xetable.clearValidate) {
            return $xetable.clearValidate();
          }
        }
        return (0, _vue.nextTick)();
      },
      /**
       * 清除所选中源状态
       */
      clearSelected: function () {
        var editStore = reactData.editStore;
        var selected = editStore.selected;
        selected.row = null;
        selected.column = null;
        removeCellSelectedClass();
        return (0, _vue.nextTick)();
      },
      isActiveByRow: function (row) {
        // if (process.env.NODE_ENV === 'development') {
        //   warnLog('vxe.error.delFunc', ['isActiveByRow', 'isEditByRow'])
        // }
        // 即将废弃
        return this.isEditByRow(row);
      },
      /**
       * 判断行是否为激活编辑状态
       * @param {Row} row 行对象
       */
      isEditByRow: function (row) {
        var editStore = reactData.editStore;
        return editStore.actived.row === row;
      },
      setActiveRow: function (row) {
        // if (process.env.NODE_ENV === 'development') {
        //   warnLog('vxe.error.delFunc', ['setActiveRow', 'setEditRow'])
        // }
        // 即将废弃
        return editMethods.setEditRow(row);
      },
      /**
       * 激活行编辑
       */
      setEditRow: function (row, fieldOrColumn) {
        var visibleColumn = internalData.visibleColumn;
        var column = _xeUtils.default.find(visibleColumn, function (column) {
          return (0, _utils.isEnableConf)(column.editRender);
        });
        if (fieldOrColumn) {
          column = _xeUtils.default.isString(fieldOrColumn) ? $xetable.getColumnByField(fieldOrColumn) : fieldOrColumn;
        }
        return $xetable.setEditCell(row, column);
      },
      setActiveCell: function (row, fieldOrColumn) {
        // if (process.env.NODE_ENV === 'development') {
        //   warnLog('vxe.error.delFunc', ['setActiveCell', 'setEditCell'])
        // }
        // 即将废弃
        return editMethods.setEditCell(row, fieldOrColumn);
      },
      /**
       * 激活单元格编辑
       */
      setEditCell: function (row, fieldOrColumn) {
        var editConfig = props.editConfig;
        var column = _xeUtils.default.isString(fieldOrColumn) ? $xetable.getColumnByField(fieldOrColumn) : fieldOrColumn;
        if (row && column && (0, _utils.isEnableConf)(editConfig) && (0, _utils.isEnableConf)(column.editRender)) {
          return $xetable.scrollToRow(row, column).then(function () {
            var cell = $xetable.getCell(row, column);
            if (cell) {
              editPrivateMethods.handleActived({
                row: row,
                rowIndex: $xetable.getRowIndex(row),
                column: column,
                columnIndex: $xetable.getColumnIndex(column),
                cell: cell,
                $table: $xetable
              });
              internalData._lastCallTime = Date.now();
            }
            return (0, _vue.nextTick)();
          });
        }
        return (0, _vue.nextTick)();
      },
      /**
       * 只对 trigger=dblclick 有效，选中单元格
       */
      setSelectCell: function (row, fieldOrColumn) {
        var tableData = reactData.tableData;
        var editOpts = computeEditOpts.value;
        var column = _xeUtils.default.isString(fieldOrColumn) ? $xetable.getColumnByField(fieldOrColumn) : fieldOrColumn;
        if (row && column && editOpts.trigger !== 'manual') {
          var rowIndex = $xetable.findRowIndexOf(tableData, row);
          if (rowIndex > -1 && column) {
            var cell = $xetable.getCell(row, column);
            var params = {
              row: row,
              rowIndex: rowIndex,
              column: column,
              columnIndex: $xetable.getColumnIndex(column),
              cell: cell
            };
            $xetable.handleSelected(params, {});
          }
        }
        return (0, _vue.nextTick)();
      }
    };
    editPrivateMethods = {
      /**
       * 处理激活编辑
       */
      handleActived: function (params, evnt) {
        var editConfig = props.editConfig,
          mouseConfig = props.mouseConfig;
        var editStore = reactData.editStore,
          tableColumn = reactData.tableColumn;
        var editOpts = computeEditOpts.value;
        var mode = editOpts.mode;
        var actived = editStore.actived;
        var row = params.row,
          column = params.column;
        var editRender = column.editRender;
        var cell = params.cell || $xetable.getCell(row, column);
        var beforeEditMethod = editOpts.beforeEditMethod || editOpts.activeMethod;
        params.cell = cell;
        if ((0, _utils.isEnableConf)(editConfig) && (0, _utils.isEnableConf)(editRender) && !$xetable.hasPendingByRow(row) && cell) {
          if (actived.row !== row || (mode === 'cell' ? actived.column !== column : false)) {
            // 判断是否禁用编辑
            var type = 'edit-disabled';
            if (!beforeEditMethod || beforeEditMethod(__assign(__assign({}, params), {
              $table: $xetable,
              $grid: $xetable.xegrid
            }))) {
              if (mouseConfig) {
                editMethods.clearSelected();
                if ($xetable.clearCellAreas) {
                  $xetable.clearCellAreas();
                  $xetable.clearCopyCellArea();
                }
              }
              $xetable.closeTooltip();
              if (actived.column) {
                editMethods.clearEdit(evnt);
              }
              type = 'edit-activated';
              column.renderHeight = cell.offsetHeight;
              actived.args = params;
              actived.row = row;
              actived.column = column;
              if (mode === 'row') {
                tableColumn.forEach(function (column) {
                  return getEditColumnModel(row, column);
                });
              } else {
                getEditColumnModel(row, column);
              }
              (0, _vue.nextTick)(function () {
                editPrivateMethods.handleFocus(params, evnt);
              });
            }
            $xetable.dispatchEvent(type, {
              row: row,
              rowIndex: $xetable.getRowIndex(row),
              $rowIndex: $xetable.getVMRowIndex(row),
              column: column,
              columnIndex: $xetable.getColumnIndex(column),
              $columnIndex: $xetable.getVMColumnIndex(column)
            }, evnt);
            // v4已废弃
            if (type === 'edit-activated') {
              $xetable.dispatchEvent('edit-actived', {
                row: row,
                rowIndex: $xetable.getRowIndex(row),
                $rowIndex: $xetable.getVMRowIndex(row),
                column: column,
                columnIndex: $xetable.getColumnIndex(column),
                $columnIndex: $xetable.getVMColumnIndex(column)
              }, evnt);
            }
          } else {
            var oldColumn = actived.column;
            if (mouseConfig) {
              editMethods.clearSelected();
              if ($xetable.clearCellAreas) {
                $xetable.clearCellAreas();
                $xetable.clearCopyCellArea();
              }
            }
            if (oldColumn !== column) {
              var oldModel = oldColumn.model;
              if (oldModel.update) {
                (0, _util.setCellValue)(row, oldColumn, oldModel.value);
              }
              if ($xetable.clearValidate) {
                $xetable.clearValidate(row, column);
              }
            }
            column.renderHeight = cell.offsetHeight;
            actived.args = params;
            actived.column = column;
            setTimeout(function () {
              editPrivateMethods.handleFocus(params, evnt);
            });
          }
          $xetable.focus();
        }
        return (0, _vue.nextTick)();
      },
      /**
       * 处理聚焦
       */
      handleFocus: function (params) {
        var row = params.row,
          column = params.column,
          cell = params.cell;
        var editRender = column.editRender;
        if ((0, _utils.isEnableConf)(editRender)) {
          var compRender = _vXETable.renderer.get(editRender.name);
          var autofocus = editRender.autofocus,
            autoselect = editRender.autoselect;
          var inputElem = void 0;
          if (!autofocus && compRender) {
            autofocus = compRender.autofocus;
          }
          if (!autoselect && compRender) {
            autoselect = compRender.autoselect;
          }
          // 如果指定了聚焦 class
          if (_xeUtils.default.isFunction(autofocus)) {
            inputElem = autofocus.call(this, params);
          } else if (autofocus) {
            inputElem = cell.querySelector(autofocus);
            if (inputElem) {
              inputElem.focus();
            }
          }
          if (inputElem) {
            if (autoselect) {
              inputElem.select();
            } else {
              // 保持一致行为，光标移到末端
              if (_dom.browse.msie) {
                var textRange = inputElem.createTextRange();
                textRange.collapse(false);
                textRange.select();
              }
            }
          } else {
            // 显示到可视区中
            $xetable.scrollToRow(row, column);
          }
        }
      },
      /**
       * 处理选中源
       */
      handleSelected: function (params, evnt) {
        var mouseConfig = props.mouseConfig;
        var editStore = reactData.editStore;
        var mouseOpts = computeMouseOpts.value;
        var editOpts = computeEditOpts.value;
        var actived = editStore.actived,
          selected = editStore.selected;
        var row = params.row,
          column = params.column;
        var isMouseSelected = mouseConfig && mouseOpts.selected;
        var selectMethod = function () {
          if (isMouseSelected && (selected.row !== row || selected.column !== column)) {
            if (actived.row !== row || (editOpts.mode === 'cell' ? actived.column !== column : false)) {
              editMethods.clearEdit(evnt);
              editMethods.clearSelected();
              if ($xetable.clearCellAreas) {
                $xetable.clearCellAreas();
                $xetable.clearCopyCellArea();
              }
              selected.args = params;
              selected.row = row;
              selected.column = column;
              if (isMouseSelected) {
                editPrivateMethods.addCellSelectedClass();
              }
              $xetable.focus();
              if (evnt) {
                $xetable.dispatchEvent('cell-selected', params, evnt);
              }
            }
          }
          return (0, _vue.nextTick)();
        };
        return selectMethod();
      },
      addCellSelectedClass: function () {
        var editStore = reactData.editStore;
        var selected = editStore.selected;
        var row = selected.row,
          column = selected.column;
        removeCellSelectedClass();
        if (row && column) {
          var cell = $xetable.getCell(row, column);
          if (cell) {
            (0, _dom.addClass)(cell, 'col--selected');
          }
        }
      }
    };
    return __assign(__assign({}, editMethods), editPrivateMethods);
  },
  setupGrid: function ($xegrid) {
    return $xegrid.extendTableMethods(tableEditMethodKeys);
  }
};
var _default = editHook;
exports.default = _default;