'use strict';

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
require('react');
var i18next = require('i18next');
var CheckCircle = require('@bigbinary/neeto-icons/CheckCircle');
var Warning = require('@bigbinary/neeto-icons/Warning');
var Info = require('@bigbinary/neeto-icons/Info');
var Close = require('@bigbinary/neeto-icons/Close');
var reactToastify = require('react-toastify');
var index = require('./index-CNZKW5X0.js');
var Linkify = require('linkify-react');
var jsxRuntime = require('react/jsx-runtime');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('@bigbinary/neeto-cist');
require('qs');
require('ramda');
require('./en-B1i-pQJm.js');
require('./index-BgzkP01c.js');
require('@babel/runtime/helpers/toConsumableArray');
require('dayjs');
require('dayjs/plugin/localeData');
require('dayjs/plugin/utc');
require('dayjs/plugin/weekday');
require('dayjs/plugin/weekOfYear');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _typeof__default = /*#__PURE__*/_interopDefault(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var CheckCircle__default = /*#__PURE__*/_interopDefault(CheckCircle);
var Warning__default = /*#__PURE__*/_interopDefault(Warning);
var Info__default = /*#__PURE__*/_interopDefault(Info);
var Close__default = /*#__PURE__*/_interopDefault(Close);
var Linkify__default = /*#__PURE__*/_interopDefault(Linkify);

var Toast = function Toast(_ref) {
  var type = _ref.type,
    message = _ref.message,
    _ref$buttonLabel = _ref.buttonLabel,
    buttonLabel = _ref$buttonLabel === void 0 ? "" : _ref$buttonLabel,
    _ref$onClick = _ref.onClick,
    _onClick = _ref$onClick === void 0 ? index.noop : _ref$onClick;
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    "data-testid": "toastr-".concat(type, "-container"),
    children: [/*#__PURE__*/jsxRuntime.jsx(Linkify__default.default, {
      options: {
        defaultProtocol: "https",
        rel: "noreferrer",
        target: "_blank"
      },
      children: /*#__PURE__*/jsxRuntime.jsx("p", {
        children: message
      })
    }), buttonLabel && /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "toastr-message-container__btn-wrapper",
      children: /*#__PURE__*/jsxRuntime.jsx("button", {
        className: "neeto-ui-btn neeto-ui-btn--style-tertiary",
        "data-testid": "toastr-".concat(type, "-button"),
        onClick: function onClick(e) {
          e.stopPropagation();
          _onClick();
        },
        children: buttonLabel
      })
    })]
  });
};

var _excluded = ["closeToast"],
  _excluded2 = ["buttonLabel", "onClick", "showIcon"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var TOAST_CONFIG = {
  autoClose: 3500,
  transition: reactToastify.Slide,
  hideProgressBar: true,
  closeButton: function closeButton(_ref) {
    var closeToast = _ref.closeToast,
      props = _objectWithoutProperties__default.default(_ref, _excluded);
    return /*#__PURE__*/jsxRuntime.jsx(Close__default.default, _objectSpread({
      className: "neeto-ui-toastr__close-button",
      color: "currentColor",
      "data-testid": "toastr-close-button",
      size: 16,
      onClick: closeToast
    }, props));
  },
  role: "log",
  className: "neeto-ui-toastr"
};
var TOAST_ICON = {
  success: /*#__PURE__*/jsxRuntime.jsx(CheckCircle__default.default, {
    className: "neeto-ui-text-white",
    "data-testid": "success-toast-icon"
  }),
  warning: /*#__PURE__*/jsxRuntime.jsx(Warning__default.default, {
    className: "neeto-ui-text-white",
    "data-testid": "warning-toast-icon"
  }),
  error: /*#__PURE__*/jsxRuntime.jsx(Warning__default.default, {
    className: "neeto-ui-text-white",
    "data-testid": "error-toast-icon"
  }),
  info: /*#__PURE__*/jsxRuntime.jsx(Info__default.default, {
    className: "neeto-ui-text-white",
    "data-testid": "info-toast-icon"
  })
};
var toastrList = new index.UniqueArray();
var parseToastrConfig = function parseToastrConfig(config) {
  if (_typeof__default.default(config[0]) === "object") {
    var _config$ = config[0],
      _buttonLabel = _config$.buttonLabel,
      _onClick = _config$.onClick,
      _showIcon = _config$.showIcon,
      _customConfig = _objectWithoutProperties__default.default(_config$, _excluded2);
    return {
      buttonLabel: _buttonLabel,
      onClick: _onClick,
      showIcon: _showIcon,
      customConfig: _customConfig
    };
  }
  var _config = _slicedToArray__default.default(config, 4),
    buttonLabel = _config[0],
    onClick = _config[1],
    customConfig = _config[2],
    showIcon = _config[3];
  return {
    buttonLabel: buttonLabel,
    onClick: onClick,
    customConfig: customConfig,
    showIcon: showIcon
  };
};
var getToastrMessage = function getToastrMessage(message) {
  if ((message === null || message === void 0 ? void 0 : message.noticeCode) === "custom_message" && "customMessage" in message) {
    return message.customMessage;
  } else if (message && _typeof__default.default(message) === "object" && message.noticeCode) {
    return i18next.t(message.noticeCode, message);
  } else if (message && _typeof__default.default(message) === "object" && message.notice) {
    return message.notice;
  }
  return message;
};
var withUniqueCheck = function withUniqueCheck(type, toastFunc) {
  return function (message) {
    message = getToastrMessage(message);
    for (var _len = arguments.length, toastrConfig = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      toastrConfig[_key - 1] = arguments[_key];
    }
    var _parseToastrConfig = parseToastrConfig(toastrConfig),
      buttonLabel = _parseToastrConfig.buttonLabel,
      onClick = _parseToastrConfig.onClick,
      _parseToastrConfig$sh = _parseToastrConfig.showIcon,
      showIcon = _parseToastrConfig$sh === void 0 ? false : _parseToastrConfig$sh,
      _parseToastrConfig$cu = _parseToastrConfig.customConfig,
      customConfig = _parseToastrConfig$cu === void 0 ? {} : _parseToastrConfig$cu;
    var toastId = null;
    if (toastrList.add({
      type: type,
      message: message,
      buttonLabel: buttonLabel
    })) {
      var config = _objectSpread(_objectSpread({}, TOAST_CONFIG), {}, {
        // The false value of the icon was ignored in the implementations before v9.0.2.
        // After 9.0.2, the implementation is such that the icon is rendered as passed in the config.
        // Since the `false` value used to show the default icon set internally, used a function that returns null
        // which solved the problem.
        // PR which addressed this issue: https://github.com/fkhadra/react-toastify/pull/758
        icon: showIcon ? TOAST_ICON[type] : function () {
          return null;
        },
        onClose: function onClose() {
          return toastrList.remove({
            type: type,
            message: message,
            buttonLabel: buttonLabel
          });
        },
        position: index.getContentDir() === "rtl" ? reactToastify.toast.POSITION.BOTTOM_RIGHT : reactToastify.toast.POSITION.BOTTOM_LEFT
      }, customConfig);
      toastId = toastFunc({
        message: message,
        buttonLabel: buttonLabel,
        onClick: onClick,
        config: config
      });
    }
    return toastId;
  };
};
var showSuccessToastr = withUniqueCheck("success", function (_ref2) {
  var message = _ref2.message,
    buttonLabel = _ref2.buttonLabel,
    onClick = _ref2.onClick,
    config = _ref2.config;
  return reactToastify.toast.success(/*#__PURE__*/jsxRuntime.jsx(Toast, {
    buttonLabel: buttonLabel,
    message: message,
    onClick: onClick,
    type: "success"
  }), config);
});
var showInfoToastr = withUniqueCheck("info", function (_ref3) {
  var message = _ref3.message,
    buttonLabel = _ref3.buttonLabel,
    onClick = _ref3.onClick,
    config = _ref3.config;
  return reactToastify.toast.info(/*#__PURE__*/jsxRuntime.jsx(Toast, {
    buttonLabel: buttonLabel,
    message: message,
    onClick: onClick,
    type: "info"
  }), config);
});
var showWarningToastr = withUniqueCheck("warning", function (_ref4) {
  var message = _ref4.message,
    buttonLabel = _ref4.buttonLabel,
    onClick = _ref4.onClick,
    config = _ref4.config;
  return reactToastify.toast.warning(/*#__PURE__*/jsxRuntime.jsx(Toast, {
    buttonLabel: buttonLabel,
    message: message,
    onClick: onClick,
    type: "warning"
  }), config);
});
var isError = function isError(e) {
  return e && e.stack && e.message;
};
var isAxiosError = function isAxiosError(e) {
  return e && _typeof__default.default(e) === "object" && e.isAxiosError === true;
};
var isString = function isString(s) {
  return typeof s === "string" || s instanceof String;
};
var isArray = function isArray(a) {
  return Array.isArray(a);
};
var isErrorCodeObject = function isErrorCodeObject(e) {
  return _typeof__default.default(e) === "object" && "key" in e && "context" in e;
};
var errorCodeTranslation = function errorCodeTranslation(errorCode) {
  if (typeof errorCode === "string") {
    return i18next.t(errorCode);
  } else if (isErrorCodeObject(errorCode)) {
    return i18next.t(errorCode.key, errorCode.context);
  }
  return undefined;
};
var getErrorMessage = function getErrorMessage(response) {
  var _response$error = response.error,
    error = _response$error === void 0 ? "" : _response$error,
    _response$errors = response.errors,
    errors = _response$errors === void 0 ? [] : _response$errors,
    _response$errorCode = response.errorCode,
    errorCode = _response$errorCode === void 0 ? "" : _response$errorCode,
    _response$errorCodes = response.errorCodes,
    errorCodes = _response$errorCodes === void 0 ? [] : _response$errorCodes;
  var errorMessages = "";
  if (error) errorMessages = error;else if (isArray(errors)) errorMessages = errors.join("\n");
  var errorCodeTranslations = errorCode && i18next.t(errorCode, response) || (errorCodes === null || errorCodes === void 0 ? void 0 : errorCodes.map(errorCodeTranslation).join("\n"));
  if (errorMessages && errorCodeTranslations) {
    return [errorMessages, errorCodeTranslations].join("\n");
  }
  return errorMessages || errorCodeTranslations;
};
var withParsedErrorMsg = function withParsedErrorMsg(toastrFunc) {
  return function (errorObject) {
    var _errorObject, _errorObject$response;
    var errorMessage;
    errorObject = getToastrMessage(errorObject);
    var responseData = ((_errorObject = errorObject) === null || _errorObject === void 0 ? void 0 : (_errorObject$response = _errorObject.response) === null || _errorObject$response === void 0 ? void 0 : _errorObject$response.data) || {};
    if (responseData.noticeCode === "custom_message" && "customMessage" in responseData) {
      errorMessage = responseData.customMessage;
    } else if (responseData !== null && responseData !== void 0 && responseData.noticeCode) {
      var data = errorObject.response.data;
      errorMessage = i18next.t(data.noticeCode, data);
    } else if (isAxiosError(errorObject)) {
      errorMessage = getErrorMessage(responseData) || errorObject.message;
    } else if (isError(errorObject)) errorMessage = errorObject.message;else if (isString(errorObject)) errorMessage = errorObject;else errorMessage = "Something went wrong.";
    for (var _len2 = arguments.length, toastrConfig = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
      toastrConfig[_key2 - 1] = arguments[_key2];
    }
    var _parseToastrConfig2 = parseToastrConfig(toastrConfig),
      buttonLabel = _parseToastrConfig2.buttonLabel,
      onClick = _parseToastrConfig2.onClick,
      customConfig = _parseToastrConfig2.customConfig,
      showIcon = _parseToastrConfig2.showIcon;
    return toastrFunc(errorMessage, _objectSpread({
      buttonLabel: buttonLabel,
      onClick: onClick,
      role: "alert",
      autoClose: false,
      showIcon: showIcon
    }, customConfig));
  };
};
var showErrorToastr = withParsedErrorMsg(withUniqueCheck("error", function (_ref5) {
  var message = _ref5.message,
    buttonLabel = _ref5.buttonLabel,
    onClick = _ref5.onClick,
    showIcon = _ref5.showIcon,
    config = _ref5.config;
  return reactToastify.toast.error(/*#__PURE__*/jsxRuntime.jsx(Toast, {
    buttonLabel: buttonLabel,
    message: message,
    onClick: onClick,
    showIcon: showIcon,
    type: "error"
  }), config);
}));
var Toastr = {
  info: showInfoToastr,
  show: showInfoToastr,
  success: showSuccessToastr,
  error: showErrorToastr,
  warning: showWarningToastr
};

module.exports = Toastr;
//# sourceMappingURL=Toastr.js.map
