import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import React from "react";
import isFunction from "lodash/isFunction";
import commonTableCellRender from "../utils/render/commonTableCellRender";
import { safeAccess, safeWrite } from "../utils/chainAccess";
import InputField from "./field/inputField";
import SelectField from "./field/selectField";
import RadioField from "./field/radioField";
import DateField from "./field/dateField";
import CustomField from "./field/customField";
import DateRangeField from "./field/dateRangeField";
var fieldsMap = {
  select: SelectField,
  text: InputField,
  radio: RadioField,
  date: DateField,
  dateRange: DateRangeField,
  custom: CustomField
};
function canEditCell(canEdit, rowData) {
  if (typeof canEdit === "undefined" || canEdit === null) {
    return true;
  }
  if (isFunction(canEdit)) {
    return canEdit(rowData);
  }
  return !!canEdit;
}
var WebNextTableCell = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(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.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 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;
    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 (isFunction(editProps)) {
      editProps = editProps(rowData);
    }
    return /*#__PURE__*/React.createElement(FieldComponent, _extends({
      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.createElement("div", {
        onClick: function onClick(e) {
          e.stopPropagation();
        }
      }, this.renderCellEditInner());
    }
    return commonTableCellRender(safeAccess(rowData, dataKey), rowIndex, rowData, column);
  };
  return WebNextTableCell;
}(React.Component);
export { WebNextTableCell as default };