import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import 'react';
import classnames from 'classnames';
import { t } from 'i18next';
import CheckCircle from '@bigbinary/neeto-icons/CheckCircle';
import Warning from '@bigbinary/neeto-icons/Warning';
import Info from '@bigbinary/neeto-icons/Info';
import Close from '@bigbinary/neeto-icons/Close';
import { Slide, toast } from 'react-toastify';
import { n as noop, U as UniqueArray, g as getContentDir } from './index-C1skvRll.js';
import Linkify from 'linkify-react';
import { jsxs, jsx } from 'react/jsx-runtime';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@bigbinary/neeto-cist';
import 'qs';
import 'ramda';
import './en-Dmu3B4Vs.js';
import './index-BGpWEjHR.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'dayjs';
import 'dayjs/plugin/localeData';
import 'dayjs/plugin/utc';
import 'dayjs/plugin/weekday';
import 'dayjs/plugin/weekOfYear';

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 ? noop : _ref$onClick;
  return /*#__PURE__*/jsxs("div", {
    "data-testid": "toastr-".concat(type, "-container"),
    children: [/*#__PURE__*/jsx(Linkify, {
      options: {
        defaultProtocol: "https",
        rel: "noreferrer",
        target: "_blank"
      },
      children: /*#__PURE__*/jsx("p", {
        children: message
      })
    }), buttonLabel && /*#__PURE__*/jsx("div", {
      className: "toastr-message-container__btn-wrapper",
      children: /*#__PURE__*/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(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: Slide,
  hideProgressBar: true,
  closeButton: function closeButton(_ref) {
    var closeToast = _ref.closeToast,
      props = _objectWithoutProperties(_ref, _excluded);
    return /*#__PURE__*/jsx(Close, _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__*/jsx(CheckCircle, {
    className: "neeto-ui-text-white",
    "data-testid": "success-toast-icon"
  }),
  warning: /*#__PURE__*/jsx(Warning, {
    className: "neeto-ui-text-white",
    "data-testid": "warning-toast-icon"
  }),
  error: /*#__PURE__*/jsx(Warning, {
    className: "neeto-ui-text-white",
    "data-testid": "error-toast-icon"
  }),
  info: /*#__PURE__*/jsx(Info, {
    className: "neeto-ui-text-white",
    "data-testid": "info-toast-icon"
  })
};
var toastrList = new UniqueArray();
var parseToastrConfig = function parseToastrConfig(config) {
  if (_typeof(config[0]) === "object") {
    var _config$ = config[0],
      _buttonLabel = _config$.buttonLabel,
      _onClick = _config$.onClick,
      _showIcon = _config$.showIcon,
      _customConfig = _objectWithoutProperties(_config$, _excluded2);
    return {
      buttonLabel: _buttonLabel,
      onClick: _onClick,
      showIcon: _showIcon,
      customConfig: _customConfig
    };
  }
  var _config = _slicedToArray(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(message) === "object" && message.noticeCode) {
    return t(message.noticeCode, message);
  } else if (message && _typeof(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(_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: getContentDir() === "rtl" ? toast.POSITION.BOTTOM_RIGHT : toast.POSITION.BOTTOM_LEFT
      }, customConfig), {}, {
        className: classnames(TOAST_CONFIG.className, customConfig === null || customConfig === void 0 ? void 0 : customConfig.className)
      });
      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 toast.success(/*#__PURE__*/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 toast.info(/*#__PURE__*/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 toast.warning(/*#__PURE__*/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(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(e) === "object" && "key" in e && "context" in e;
};
var errorCodeTranslation = function errorCodeTranslation(errorCode) {
  if (typeof errorCode === "string") {
    return t(errorCode);
  } else if (isErrorCodeObject(errorCode)) {
    return 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 && 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 = 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 toast.error(/*#__PURE__*/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
};

export { Toastr as default };
//# sourceMappingURL=Toastr.js.map
