"use strict";

require("core-js/modules/web.dom-collections.iterator.js");

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _Dialog = _interopRequireDefault(require("@material-ui/core/Dialog"));

var _DialogActions = _interopRequireDefault(require("@material-ui/core/DialogActions"));

var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));

var _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _useConfirmDialog = _interopRequireDefault(require("./useConfirmDialog"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

// from https://medium.com/javascript-in-plain-english/creating-a-confirm-dialog-in-react-and-material-ui-3d7aaea1d799
const ConfirmDialog = props => {
  const {
    title,
    open = false,
    setOpen,
    onConfirm = () => {},
    onCancel = () => {},
    confirmText = 'Yes',
    cancelText = 'No',
    applyTimeout = null
  } = props; // eslint-disable-line

  let {
    children
  } = props; // eslint-disable-line

  const [timeout, setTimeout] = (0, _react.useState)(applyTimeout);
  (0, _react.useEffect)(() => {
    if (timeout === null && applyTimeout) {
      setTimeout(applyTimeout);
    }

    const interval = setInterval(() => {
      if (timeout) setTimeout(timeout - 1);
    }, 1000);

    if (!open || applyTimeout === null) {
      clearInterval(interval);
    }

    return () => clearInterval(interval);
  }, [timeout, applyTimeout, open]); // eslint-disable-line

  (0, _react.useEffect)(() => {
    if (timeout === 0) {
      onConfirm();
      setOpen(false);
    }
  }, [timeout]); // eslint-disable-line

  if (typeof children === 'string') {
    children = /*#__PURE__*/_react.default.createElement(_Typography.default, {
      variant: "body1"
    }, children);
  }

  return /*#__PURE__*/_react.default.createElement(_Dialog.default, {
    open: open,
    onClose: () => setOpen(false),
    "aria-labelledby": "confirm-dialog"
  }, /*#__PURE__*/_react.default.createElement(_DialogTitle.default, {
    id: "confirm-dialog"
  }, title), /*#__PURE__*/_react.default.createElement(_DialogContent.default, null, children), /*#__PURE__*/_react.default.createElement(_DialogActions.default, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
    variant: "contained",
    onClick: () => {
      setOpen(false);
      onCancel();
    },
    color: "default"
  }, cancelText), /*#__PURE__*/_react.default.createElement(_Button.default, {
    variant: "contained",
    onClick: () => {
      setOpen(false);
      onConfirm();
    },
    color: "primary"
  }, timeout ? "".concat(confirmText, " (").concat(timeout, ")") : confirmText)));
};

var _default = ConfirmDialog;
exports.default = _default;
ConfirmDialog.propTypes = {
  title: _propTypes.default.string,
  open: _propTypes.default.bool,
  setOpen: _propTypes.default.func,
  onConfirm: _propTypes.default.func,
  onCancel: _propTypes.default.func,
  confirmText: _propTypes.default.string,
  cancelText: _propTypes.default.string,
  applyTimeout: _propTypes.default.number
};

const ManagedConfirmDialog = props => {
  const {
    confirmTitle,
    confirmContent,
    confirmOpen,
    setConfirmOpen,
    applyConfirm,
    applyCancel,
    button1Label,
    button2Label,
    autoApplyTimeout
  } = (0, _useConfirmDialog.default)();
  const hookProps = {
    title: confirmTitle,
    children: confirmContent,
    open: confirmOpen,
    setOpen: setConfirmOpen,
    onConfirm: applyConfirm,
    onCancel: applyCancel,
    confirmText: button1Label,
    cancelText: button2Label,
    applyTimeout: autoApplyTimeout
  };
  return confirmOpen ? /*#__PURE__*/_react.default.createElement(ConfirmDialog, hookProps) : /*#__PURE__*/_react.default.createElement("span", null);
};

exports.ManagedConfirmDialog = ManagedConfirmDialog;