"use strict";

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

var _Button = _interopRequireDefault(require("@mui/material/Button"));

var _Dialog = _interopRequireDefault(require("@mui/material/Dialog"));

var _DialogActions = _interopRequireDefault(require("@mui/material/DialogActions"));

var _DialogContent = _interopRequireDefault(require("@mui/material/DialogContent"));

var _DialogContentText = _interopRequireDefault(require("@mui/material/DialogContentText"));

var _DialogTitle = _interopRequireDefault(require("@mui/material/DialogTitle"));

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

var _reactPolyglot = require("react-polyglot");

var _AlertEvent = _interopRequireDefault(require("../../lib/util/events/AlertEvent"));

var _window = require("../../lib/util/window.util");

var _react2 = require("@emotion/react");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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; }

const AlertDialog = _ref => {
  let {
    t
  } = _ref;
  const [detail, setDetail] = (0, _react.useState)(null);
  const {
    resolve,
    title: rawTitle,
    body: rawBody,
    okay: rawOkay = 'ui.common.okay',
    color = 'primary'
  } = detail !== null && detail !== void 0 ? detail : {};
  const onAlertMessage = (0, _react.useCallback)(event => {
    setDetail(event.detail);
  }, []);
  (0, _window.useWindowEvent)('alert', onAlertMessage);
  const handleClose = (0, _react.useCallback)(() => {
    setDetail(null);
    resolve === null || resolve === void 0 ? void 0 : resolve();
  }, [resolve]);
  const title = (0, _react.useMemo)(() => {
    if (!rawTitle) {
      return '';
    }

    return typeof rawTitle === 'string' ? t(rawTitle) : t(rawTitle.key, rawTitle.options);
  }, [rawTitle]);
  const body = (0, _react.useMemo)(() => {
    if (!rawBody) {
      return '';
    }

    return typeof rawBody === 'string' ? t(rawBody) : t(rawBody.key, rawBody.options);
  }, [rawBody]);
  const okay = (0, _react.useMemo)(() => typeof rawOkay === 'string' ? t(rawOkay) : t(rawOkay.key, rawOkay.options), [rawOkay]);

  if (!detail) {
    return null;
  }

  return (0, _react2.jsx)("div", null, (0, _react2.jsx)(_Dialog.default, {
    open: true,
    onClose: handleClose,
    "aria-labelledby": "alert-dialog-title",
    "aria-describedby": "alert-dialog-description"
  }, (0, _react2.jsx)(_DialogTitle.default, {
    id: "alert-dialog-title"
  }, t(title)), (0, _react2.jsx)(_DialogContent.default, null, (0, _react2.jsx)(_DialogContentText.default, {
    id: "alert-dialog-description"
  }, body)), (0, _react2.jsx)(_DialogActions.default, null, (0, _react2.jsx)(_Button.default, {
    onClick: handleClose,
    variant: "contained",
    color: color
  }, okay))));
};

const Alert = (0, _reactPolyglot.translate)()(AlertDialog);
exports.Alert = Alert;

const alert = props => {
  return new Promise(resolve => {
    window.dispatchEvent(new _AlertEvent.default(_objectSpread(_objectSpread({}, props), {}, {
      resolve
    })));
  });
};

var _default = alert;
exports.default = _default;