"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
var _commonTableCellRender = _interopRequireDefault(require("../utils/render/commonTableCellRender"));
var _chainAccess = require("../utils/chainAccess");
var _inputField = _interopRequireDefault(require("./field/inputField"));
var _selectField = _interopRequireDefault(require("./field/selectField"));
var _radioField = _interopRequireDefault(require("./field/radioField"));
var _dateField = _interopRequireDefault(require("./field/dateField"));
var _customField = _interopRequireDefault(require("./field/customField"));
var _dateRangeField = _interopRequireDefault(require("./field/dateRangeField"));
var fieldsMap = {
  select: _selectField["default"],
  text: _inputField["default"],
  radio: _radioField["default"],
  date: _dateField["default"],
  dateRange: _dateRangeField["default"],
  custom: _customField["default"]
};
function canEditCell(canEdit, rowData) {
  if (typeof canEdit === "undefined" || canEdit === null) {
    return true;
  }
  if ((0, _isFunction["default"])(canEdit)) {
    return canEdit(rowData);
  }
  return !!canEdit;
}
var WebNextTableCell = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(WebNextTableCell, _React$Component);
  function WebNextTableCell(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _this.fieldComponentRef = void 0;
    _this.state = {
      editable: false,
      currentValue: _this.getRowValue()
    };
    _this.fieldComponentRef = /*#__PURE__*/_react["default"].createRef();
    return _this;
  }
  var _proto = WebNextTableCell.prototype;
  _proto.setEditable = function setEditable(v) {
    var rowData = this.props.rowData;
    rowData.__mode__ = v ? "EDIT" : "VIEW";
    this.setState({
      editable: !!v
    });
  };
  _proto.getRowValue = function getRowValue() {
    var _this$props = this.props,
      rowData = _this$props.rowData,
      column = _this$props.column;
    var dataKey = column.dataKey;
    return (0, _chainAccess.safeAccess)(rowData, dataKey);
  };
  _proto.saveCell = function saveCell() {
    var currentValue = this.state.currentValue;
    var _this$props2 = this.props,
      rowData = _this$props2.rowData,
      column = _this$props2.column;
    var dataKey = column.dataKey;
    (0, _chainAccess.safeWrite)(rowData, dataKey, currentValue);
    this.setEditable(false);
  };
  _proto.resetCell = function resetCell() {
    this.setState({
      currentValue: this.getRowValue()
    });
    this.setEditable(false);
  };
  _proto.validateCell = function validateCell() {
    var current = this.fieldComponentRef.current;
    var validate = current && current.validate;
    return validate && validate.call(current);
  };
  _proto._fillFields = function _fillFields(rowData, dataKey) {
    rowData.__fields__ = rowData.__fields__ || {};
    rowData.__fields__[dataKey] = this;
  };
  _proto.renderCellEditInner = function renderCellEditInner() {
    var _this2 = this;
    var currentValue = this.state.currentValue;
    var _this$props3 = this.props,
      nextTablePrefix = _this$props3.nextTablePrefix,
      column = _this$props3.column,
      rowData = _this$props3.rowData,
      rowIndex = _this$props3.rowIndex,
      onCellDataChange = _this$props3.onCellDataChange;
    var editType = column.editType,
      dataKey = column.dataKey;
    var FieldComponent = fieldsMap[editType];
    var editProps = column.editProps;
    if ((0, _isFunction["default"])(editProps)) {
      editProps = editProps(rowData);
    }
    return /*#__PURE__*/_react["default"].createElement(FieldComponent, (0, _extends2["default"])({
      ref: this.fieldComponentRef,
      nextTablePrefix: nextTablePrefix,
      name: dataKey,
      value: currentValue,
      onChange: function onChange(value) {
        // 兼容乐高 onChange 的返回格式 { value: value, ...rest }
        var realValue = value;
        if (value && typeof value === 'object' && Object.hasOwnProperty.call(value, 'value')) {
          realValue = value.value;
        }
        _this2.setState({
          currentValue: realValue
        });
        onCellDataChange && onCellDataChange({
          dataKey: dataKey,
          value: realValue,
          rowData: rowData,
          rowIndex: rowIndex
        });
      },
      rowData: rowData
    }, editProps));
  };
  _proto.render = function render() {
    var editable = this.state.editable;
    var _this$props4 = this.props,
      column = _this$props4.column,
      rowData = _this$props4.rowData,
      isRenderEditMode = _this$props4.isRenderEditMode,
      rowIndex = _this$props4.rowIndex;
    var editType = column.editType,
      canEdit = column.canEdit,
      dataKey = column.dataKey;
    this._fillFields(rowData, dataKey);
    if (isRenderEditMode && editable && editType && editType in fieldsMap && canEditCell(canEdit, rowData)) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        onClick: function onClick(e) {
          e.stopPropagation();
        }
      }, this.renderCellEditInner());
    }
    return (0, _commonTableCellRender["default"])((0, _chainAccess.safeAccess)(rowData, dataKey), rowIndex, rowData, column);
  };
  return WebNextTableCell;
}(_react["default"].Component);
exports["default"] = WebNextTableCell;