"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Confirm = 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 _ConfirmEvent = _interopRequireDefault(require("../../lib/util/events/ConfirmEvent"));

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 ConfirmDialog = _ref => {
  let {
    t
  } = _ref;
  const [detail, setDetail] = (0, _react.useState)(null);
  const {
    resolve,
    title: rawTitle,
    body: rawBody,
    cancel: rawCancel = 'ui.common.no',
    confirm: rawConfirm = 'ui.common.yes',
    color = 'primary'
  } = detail !== null && detail !== void 0 ? detail : {};
  const onConfirmMessage = (0, _react.useCallback)(event => {
    setDetail(event.detail);
  }, []);
  (0, _window.useWindowEvent)('confirm', onConfirmMessage);
  const handleClose = (0, _react.useCallback)(() => {
    setDetail(null);
  }, []);
  const handleCancel = (0, _react.useCallback)(() => {
    resolve === null || resolve === void 0 ? void 0 : resolve(false);
    handleClose();
  }, [handleClose, resolve]);
  const handleConfirm = (0, _react.useCallback)(() => {
    resolve === null || resolve === void 0 ? void 0 : resolve(true);
    handleClose();
  }, [handleClose, 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 cancel = (0, _react.useMemo)(() => typeof rawCancel === 'string' ? t(rawCancel) : t(rawCancel.key, rawCancel.options), [rawCancel]);
  const confirm = (0, _react.useMemo)(() => typeof rawConfirm === 'string' ? t(rawConfirm) : t(rawConfirm.key, rawConfirm.options), [rawConfirm]);

  if (!detail) {
    return null;
  }

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

const Confirm = (0, _reactPolyglot.translate)()(ConfirmDialog);
exports.Confirm = Confirm;

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

var _default = confirm;
exports.default = _default;