"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _getLocale = require("../../utils/getLocale");

var _success = _interopRequireDefault(require("./img/success.svg"));

var _failed = _interopRequireDefault(require("./img/failed.svg"));

var _toast = _interopRequireDefault(require("../toast"));

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var prefix = 'titaui-auto-save';

var AutoSave = function AutoSave(_ref) {
  var _classNames;

  var _ref$isSuccess = _ref.isSuccess,
      isSuccess = _ref$isSuccess === void 0 ? true : _ref$isSuccess,
      _ref$loading = _ref.loading,
      loading = _ref$loading === void 0 ? false : _ref$loading;
  var renderSaveIcon = (0, _react.useMemo)(function () {
    var imgSrc = _success["default"];

    if (!isSuccess) {
      imgSrc = _failed["default"];
    }

    return /*#__PURE__*/_react["default"].createElement("img", {
      className: "".concat(prefix, "__icon"),
      src: imgSrc
    });
  }, [isSuccess, loading]);
  var renderText = (0, _react.useMemo)(function () {
    if (loading) {
      return "".concat((0, _getLocale.getLocale)('Rtx_Saving'), "...");
    } else if (isSuccess) {
      return (0, _getLocale.getLocale)('Rep_NewS_SaveDraft');
    } else {
      _toast["default"].Error((0, _getLocale.getLocale)('Mod_Netexcfailed'));

      return (0, _getLocale.getLocale)('Mod_SaveFailure');
    }
  }, [loading, isSuccess]);
  return /*#__PURE__*/_react["default"].createElement("span", {
    className: prefix
  }, !loading && /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(prefix, "__icon-wrapper"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "__icon-wrapper--success"), isSuccess), _defineProperty(_classNames, "".concat(prefix, "__icon-wrapper--failed"), !isSuccess), _classNames))
  }, renderSaveIcon), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(prefix, "__text"), _defineProperty({}, "".concat(prefix, "__text--loading"), loading))
  }, renderText));
};

AutoSave.defaultProps = {
  isSuccess: true,
  loading: false
};
var _default = AutoSave;
exports["default"] = _default;