"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.cachedTypeIntlMap = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _mobxReactLite = require("mobx-react-lite");

var _mobx = require("mobx");

var _enum = require("../core/enum");

var _enum2 = require("../data-set/enum");

var _localeContext = require("../locale-context");

var _TableContext = _interopRequireDefault(require("./TableContext"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _Button = _interopRequireDefault(require("../button/Button"));

var _Select = _interopRequireDefault(require("../select/Select"));

var _enum3 = require("../button/enum");

var _utils = require("./utils");

var _cachedTypeIntlMap;

var Option = _Select["default"].Option;
var cachedTypeIntlMap = (_cachedTypeIntlMap = {}, (0, _defineProperty2["default"])(_cachedTypeIntlMap, _enum2.RecordCachedType.selected, 'cached_type_selected'), (0, _defineProperty2["default"])(_cachedTypeIntlMap, _enum2.RecordCachedType.add, 'cached_type_created'), (0, _defineProperty2["default"])(_cachedTypeIntlMap, _enum2.RecordCachedType.update, 'cached_type_updated'), (0, _defineProperty2["default"])(_cachedTypeIntlMap, _enum2.RecordCachedType["delete"], 'cached_type_destroyed'), _cachedTypeIntlMap);
exports.cachedTypeIntlMap = cachedTypeIntlMap;

var CachedTips = function CachedTip(props) {
  var dataSet = props.dataSet,
      tableStore = props.tableStore,
      prefixCls = props.prefixCls;
  var recordCachedType = tableStore.recordCachedType,
      defaultRecordCachedType = tableStore.defaultRecordCachedType;
  var options = [];
  var selectedLength = dataSet.selected.length,
      createdLength = dataSet.cachedCreated.length,
      updatedLength = dataSet.cachedUpdated.length,
      destroyedLength = dataSet.cachedDestroyed.length;
  var lengths = new Map([[_enum2.RecordCachedType.selected, selectedLength], [_enum2.RecordCachedType.add, createdLength], [_enum2.RecordCachedType.update, updatedLength], [_enum2.RecordCachedType["delete"], destroyedLength]]);
  lengths.forEach(function (length, key) {
    if (length) {
      options.push( /*#__PURE__*/_react["default"].createElement(Option, {
        key: String(key),
        value: key
      }, (0, _localeContext.$l)('Table', cachedTypeIntlMap[key])));

      if (!defaultRecordCachedType || key === _enum2.RecordCachedType.add) {
        defaultRecordCachedType = key;
      }
    } else {
      if (recordCachedType === key) {
        recordCachedType = undefined;
      }

      if (defaultRecordCachedType === key) {
        defaultRecordCachedType = undefined;
      }
    }
  });
  var optionsRenderer = (0, _react.useCallback)(function (_ref) {
    var text = _ref.text,
        value = _ref.value;
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, text, /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(prefixCls, "-cached-type-count")
    }, (0, _utils.getCount)(dataSet, value)));
  }, [prefixCls, dataSet]);
  var handleChangeRecordCachedType = (0, _react.useCallback)((0, _mobx.action)(function (newRecordCachedType) {
    tableStore.recordCachedType = newRecordCachedType;
  }), [tableStore]);
  (0, _react.useEffect)((0, _mobx.action)(function () {
    if (tableStore.recordCachedType !== recordCachedType) {
      tableStore.recordCachedType = recordCachedType;
    }
  }), [tableStore, recordCachedType]);
  (0, _react.useEffect)((0, _mobx.action)(function () {
    if (tableStore.defaultRecordCachedType !== defaultRecordCachedType) {
      tableStore.defaultRecordCachedType = defaultRecordCachedType;

      if (defaultRecordCachedType === _enum2.RecordCachedType.add) {
        tableStore.showCachedSelection = true;
      } else if (!defaultRecordCachedType) {
        tableStore.showCachedSelection = false;
      }
    }
  }), [tableStore, defaultRecordCachedType]);

  if (options.length) {
    return /*#__PURE__*/_react["default"].createElement("span", null, (0, _localeContext.$l)('Table', 'cached_tips', {
      type: options.length > 1 ? /*#__PURE__*/_react["default"].createElement(_Select["default"], {
        className: "".concat(prefixCls, "-cached-type"),
        optionRenderer: optionsRenderer,
        isFlat: true,
        value: recordCachedType || defaultRecordCachedType,
        onChange: handleChangeRecordCachedType,
        size: _enum.Size.small,
        clearButton: false
      }, options) : (0, _localeContext.$l)('Table', cachedTypeIntlMap[recordCachedType || defaultRecordCachedType || _enum2.RecordCachedType.selected]),
      count: (0, _utils.getCount)(dataSet, recordCachedType || defaultRecordCachedType)
    }));
  }

  return null;
};

var ObserverCachedTips = (0, _mobxReactLite.observer)(CachedTips);

var SelectionTips = function SelectionTips() {
  var _useContext = (0, _react.useContext)(_TableContext["default"]),
      prefixCls = _useContext.prefixCls,
      dataSet = _useContext.dataSet,
      tableStore = _useContext.tableStore,
      showSelectionCachedButton = _useContext.showSelectionCachedButton,
      onShowCachedSelectionChange = _useContext.onShowCachedSelectionChange;

  var showCachedSelection = tableStore.showCachedSelection;

  var getTitle = function getTitle() {
    return (0, _localeContext.$l)('Table', showCachedSelection ? 'hide_cached_records' : 'show_cached_records');
  };

  var cachedButton = showSelectionCachedButton && (dataSet.cacheSelectionKeys || dataSet.cacheModifiedKeys) && dataSet.cachedRecords.length > 0 ? /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
    title: getTitle
  }, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    size: _enum.Size.small,
    className: "".concat(prefixCls, "-switch"),
    funcType: _enum3.FuncType.flat,
    icon: showCachedSelection ? 'visibility_off' : 'visibility',
    onClick: (0, _mobx.action)(function () {
      var newShowCachedSelection = !showCachedSelection;
      tableStore.showCachedSelection = newShowCachedSelection;

      if (onShowCachedSelectionChange) {
        onShowCachedSelectionChange(newShowCachedSelection);
      }
    })
  })) : null;

  if (tableStore.showCachedTips) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefixCls, "-selection-tips")
    }, /*#__PURE__*/_react["default"].createElement(ObserverCachedTips, {
      dataSet: dataSet,
      tableStore: tableStore,
      prefixCls: prefixCls
    }), cachedButton);
  }

  return tableStore.showSelectionTips ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-selection-tips")
  }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _localeContext.$l)('Table', 'selection_tips', {
    count: (0, _utils.getCount)(dataSet, _enum2.RecordCachedType.selected)
  })), cachedButton) : cachedButton;
};

SelectionTips.displayName = 'SelectionTips';

var _default = (0, _mobxReactLite.observer)(SelectionTips);

exports["default"] = _default;
//# sourceMappingURL=SelectionTips.js.map
