"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _assign = _interopRequireDefault(require("lodash/assign"));
var _isObject = _interopRequireDefault(require("lodash/isObject"));
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
var _dispatchResizeEvent = _interopRequireDefault(require("../utils/dispatchResizeEvent"));
var _getCleanRowData = _interopRequireDefault(require("../utils/getCleanRowData"));
function getDataItemByValue(rowData, instance) {
  var primaryKey = instance.props.primaryKey;
  var primaryValue = (0, _isObject["default"])(rowData) ? rowData[primaryKey] : rowData;
  return instance.getDataItem(primaryValue);
}
function getAndCloneDataItem(rowData, instance) {
  return (0, _getCleanRowData["default"])(getDataItemByValue(rowData, instance));
}
function callTableCell(rowData, instance, callback) {
  var item = getDataItemByValue(rowData, instance) || {};
  var __fields__ = item.__fields__;
  if (__fields__) {
    var promiseList = [];
    Object.keys(__fields__).forEach(function (name) {
      var field = __fields__[name];
      if (field && callback) {
        promiseList.push(Promise.resolve(callback(field)));
      }
    });
    return Promise.all(promiseList);
  }
  return Promise.resolve();
}

// editRow(rowData): 使指定的行切换到编辑模式。
// saveRow(rowData): 保存行至数据
// resetRow(rowData): 重置行到数据（若保存过，则为保存过后的数据）。
var methods = {
  init: function init() {
    var mergeState = {};
    this.state = this.state || {};
    (0, _assign["default"])(this.state, mergeState);
  },
  validateRow: function validateRow(rowData) {
    return callTableCell(rowData, this, function (field) {
      return field.validateCell();
    }).then(function (dataList) {
      var result = {};
      dataList && dataList.forEach(function (field) {
        field && Object.keys(field).forEach(function (name) {
          result[name] = field[name];
        });
      });
      return (0, _isEmpty["default"])(result) ? undefined : result;
    });
  },
  editRow: function editRow(rowData) {
    var _this = this;
    var onEditRow = this.props.onEditRow;
    var promise = callTableCell(rowData, this, function (field) {
      return field.setEditable(true);
    }).then(function () {
      var rowItem = getAndCloneDataItem(rowData, _this);
      rowItem && onEditRow && onEditRow(rowItem);
      return rowItem;
    });
    promise["finally"](function () {
      (0, _dispatchResizeEvent["default"])();
    });
    return promise;
  },
  saveRow: function saveRow(rowData) {
    var _this2 = this;
    var onSaveRow = this.props.onSaveRow;
    var promise = this.validateRow(rowData).then(function (error) {
      if (error) {
        return Promise.reject(error);
      } else {
        return callTableCell(rowData, _this2, function (field) {
          return field.saveCell();
        });
      }
    }).then(function () {
      var rowItem = getAndCloneDataItem(rowData, _this2);
      rowItem && onSaveRow && onSaveRow(rowItem);
      return rowItem;
    });
    promise["finally"](function () {
      (0, _dispatchResizeEvent["default"])();
    });
    return promise;
  },
  resetRow: function resetRow(rowData) {
    var _this3 = this;
    var onResetRow = this.props.onResetRow;
    var promise = callTableCell(rowData, this, function (field) {
      return field.resetCell();
    }).then(function () {
      var rowItem = getAndCloneDataItem(rowData, _this3);
      rowItem && onResetRow && onResetRow(rowItem);
      return rowItem;
    });
    promise["finally"](function () {
      (0, _dispatchResizeEvent["default"])();
    });
    return promise;
  }
};
var _default = methods;
exports["default"] = _default;