"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.promise.js");
var _react = require("react");
var _useMergedState = _interopRequireDefault(require("rc-util/es/hooks/useMergedState"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _wauProvider = require("../../wau-provider");
var _message = _interopRequireDefault(require("../../message"));
var _useEditableArray = require("../useEditableArray");
require("../../message/style");
/**
 * 使用map 来删除数据，性能一般 但是准确率比较高
 *
 * @param params
 * @param action
 */
function editableRowByKey({
  data,
  row
}) {
  return {
    ...data,
    ...row
  };
}
/**
 * 一个方便的hooks 用于维护编辑的状态
 *
 * @param props
 */
function useEditableMap(props) {
  const editableType = props.type || 'single';
  const [editableKeys, setEditableRowKeys] = (0, _useMergedState.default)([], {
    value: props.editableKeys,
    onChange: props.onChange ? keys => {
      var _props$onChange;
      props === null || props === void 0 ? void 0 : (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props,
      // 计算编辑的key
      keys, props.dataSource);
    } : undefined
  });
  /** 一个用来标志的set 提供了方便的 api 来去重什么的 */
  const editableKeysSet = (0, _react.useMemo)(() => {
    const keys = editableType === 'single' ? editableKeys.slice(0, 1) : editableKeys;
    return new Set(keys);
  }, [editableKeys.join(','), editableType]);

  /** 这行是不是编辑状态 */
  const isEditable = (0, _react.useCallback)(recordKey => {
    if (editableKeys.includes((0, _useEditableArray.recordKeyToString)(recordKey))) return true;
    return false;
  }, [editableKeys.join(',')]);

  /**
   * 进入编辑状态
   *
   * @param recordKey
   */
  const startEditable = recordKey => {
    // 如果是单行的话，不允许多行编辑
    if (editableKeysSet.size > 0 && editableType === 'single') {
      _message.default.warn(props.onlyOneLineEditorAlertMessage || '只能同时编辑一行');
      return false;
    }
    editableKeysSet.add((0, _useEditableArray.recordKeyToString)(recordKey));
    setEditableRowKeys(Array.from(editableKeysSet));
    return true;
  };

  /**
   * 退出编辑状态
   *
   * @param recordKey
   */
  const cancelEditable = recordKey => {
    // 防止多次渲染
    _reactDom.default.unstable_batchedUpdates(() => {
      editableKeysSet.delete((0, _useEditableArray.recordKeyToString)(recordKey));
      setEditableRowKeys(Array.from(editableKeysSet));
    });
    return true;
  };
  const onCancel = async (recordKey, editRow, isNewLine) => {
    var _props$onCancel;
    const success = await (props === null || props === void 0 ? void 0 : (_props$onCancel = props.onCancel) === null || _props$onCancel === void 0 ? void 0 : _props$onCancel.call(props, recordKey, editRow, isNewLine));
    if (success === false) {
      return false;
    }
    return true;
  };
  const onSave = async (recordKey, editRow) => {
    var _props$onSave;
    const success = await (props === null || props === void 0 ? void 0 : (_props$onSave = props.onSave) === null || _props$onSave === void 0 ? void 0 : _props$onSave.call(props, recordKey, editRow));
    if (success === false) {
      return false;
    }
    cancelEditable(recordKey);
    const actionProps = {
      data: props.dataSource,
      row: editRow,
      key: recordKey,
      childrenColumnName: props.childrenColumnName || 'children'
    };
    props.setDataSource(editableRowByKey(actionProps));
    return true;
  };

  // Internationalization
  const intl = (0, _wauProvider.useIntl)();
  const saveText = intl.getMessage('editableTable.action.save', '保存');
  const deleteText = intl.getMessage('editableTable.action.delete', '删除');
  const cancelText = intl.getMessage('editableTable.action.cancel', '取消');
  const actionRender = (0, _react.useCallback)((key, form, config) => {
    const renderConfig = {
      recordKey: (0, _useEditableArray.recordKeyToString)(key),
      cancelEditable,
      onCancel,
      onSave,
      editableKeys,
      setEditableRowKeys,
      form,
      saveText,
      cancelText,
      deleteText,
      deletePopconfirmMessage: '删除此行？',
      editorType: 'Map',
      ...config
    };
    const defaultDoms = (0, _useEditableArray.defaultActionRender)(props.dataSource, renderConfig);
    if (props.actionRender) return props.actionRender(props.dataSource, renderConfig, {
      save: defaultDoms[0],
      delete: defaultDoms[1],
      cancel: defaultDoms[2]
    });
    return defaultDoms;
  }, [editableKeys.join(',')]);
  return {
    editableKeys,
    setEditableRowKeys,
    isEditable,
    actionRender,
    startEditable,
    cancelEditable
  };
}
var _default = useEditableMap;
exports.default = _default;