"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "EuiConfirmModal", {
  enumerable: true,
  get: function get() {
    return _confirm_modal.EuiConfirmModal;
  }
});
exports.OuiConfirmModal = exports.CANCEL_BUTTON = exports.CONFIRM_BUTTON = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _modal = require("./modal");
var _modal_footer = require("./modal_footer");
var _modal_header = require("./modal_header");
var _modal_header_title = require("./modal_header_title");
var _modal_body = require("./modal_body");
var _button = require("../button");
var _text = require("../text");
var _confirm_modal = require("../../eui_components/modal/confirm_modal");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) { "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); } return f; })(e, t); }
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 *
 * Modifications Copyright OpenSearch Contributors. See
 * GitHub history for details.
 */

/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var CONFIRM_BUTTON = 'confirm';
exports.CONFIRM_BUTTON = CONFIRM_BUTTON;
var CANCEL_BUTTON = 'cancel';
exports.CANCEL_BUTTON = CANCEL_BUTTON;
var OuiConfirmModal = function OuiConfirmModal(_ref) {
  var children = _ref.children,
    title = _ref.title,
    onCancel = _ref.onCancel,
    onConfirm = _ref.onConfirm,
    cancelButtonText = _ref.cancelButtonText,
    confirmButtonText = _ref.confirmButtonText,
    confirmButtonDisabled = _ref.confirmButtonDisabled,
    className = _ref.className,
    _ref$buttonColor = _ref.buttonColor,
    buttonColor = _ref$buttonColor === void 0 ? 'primary' : _ref$buttonColor,
    defaultFocusedButton = _ref.defaultFocusedButton,
    isLoading = _ref.isLoading,
    rest = (0, _objectWithoutProperties2.default)(_ref, ["children", "title", "onCancel", "onConfirm", "cancelButtonText", "confirmButtonText", "confirmButtonDisabled", "className", "buttonColor", "defaultFocusedButton", "isLoading"]);
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    cancelButton = _useState2[0],
    setCancelButton = _useState2[1];
  var _useState3 = (0, _react.useState)(null),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    confirmButton = _useState4[0],
    setConfirmButton = _useState4[1];
  (0, _react.useEffect)(function () {
    // We have to do this instead of using `autoFocus` because React's polyfill for auto-focusing
    // elements conflicts with the focus-trap logic we have on OuiModal.
    // Wait a beat for the focus-trap to complete, and then set focus to the right button. Check that
    // the buttons exist first, because it's possible the modal has been closed already.
    requestAnimationFrame(function () {
      if (defaultFocusedButton === CANCEL_BUTTON && cancelButton) {
        cancelButton.focus();
      } else if (defaultFocusedButton === CONFIRM_BUTTON && confirmButton) {
        confirmButton.focus();
      }
    });
  });
  var confirmRef = function confirmRef(node) {
    return setConfirmButton(node);
  };
  var cancelRef = function cancelRef(node) {
    return setCancelButton(node);
  };
  var classes = (0, _classnames.default)('ouiModal--confirmation', className);
  var modalTitle;
  if (title) {
    modalTitle = /*#__PURE__*/_react.default.createElement(_modal_header.OuiModalHeader, null, /*#__PURE__*/_react.default.createElement(_modal_header_title.OuiModalHeaderTitle, {
      "data-test-subj": "confirmModalTitleText"
    }, title));
  }
  var message;
  if (typeof children === 'string' && children.length > 0) {
    message = /*#__PURE__*/_react.default.createElement("p", null, children);
  } else {
    message = children;
  }
  return /*#__PURE__*/_react.default.createElement(_modal.OuiModal, (0, _extends2.default)({
    className: classes,
    onClose: onCancel
  }, rest), modalTitle, message && /*#__PURE__*/_react.default.createElement(_modal_body.OuiModalBody, null, /*#__PURE__*/_react.default.createElement(_text.OuiText, {
    "data-test-subj": "confirmModalBodyText"
  }, message)), /*#__PURE__*/_react.default.createElement(_modal_footer.OuiModalFooter, null, /*#__PURE__*/_react.default.createElement(_button.OuiButtonEmpty, {
    "data-test-subj": "confirmModalCancelButton",
    onClick: onCancel,
    buttonRef: cancelRef
  }, cancelButtonText), /*#__PURE__*/_react.default.createElement(_button.OuiButton, {
    "data-test-subj": "confirmModalConfirmButton",
    onClick: onConfirm,
    isLoading: isLoading,
    fill: true,
    buttonRef: confirmRef,
    color: buttonColor,
    isDisabled: confirmButtonDisabled
  }, confirmButtonText)));
};

/* OUI -> EUI Aliases: Build-Time */
exports.OuiConfirmModal = OuiConfirmModal;
OuiConfirmModal.propTypes = {
  /**
     * ReactNode to render as this component's content
     */
  children: _propTypes.default.node,
  title: _propTypes.default.node,
  cancelButtonText: _propTypes.default.node,
  confirmButtonText: _propTypes.default.node,
  onCancel: _propTypes.default.func.isRequired,
  onConfirm: _propTypes.default.func,
  confirmButtonDisabled: _propTypes.default.bool,
  className: _propTypes.default.string,
  defaultFocusedButton: _propTypes.default.oneOfType([_propTypes.default.any.isRequired, _propTypes.default.any.isRequired]),
  buttonColor: _propTypes.default.oneOf(["primary", "accent", "secondary", "success", "warning", "danger", "ghost", "text"]),
  // For docs only, will get passed with ...rest
  /**
     * Sets the max-width of the modal.
     * Set to `true` to use the default (`ouiBreakpoints 'm'`),
     * set to `false` to not restrict the width,
     * set to a number for a custom width in px,
     * set to a string for a custom width in custom measurement.
     */
  maxWidth: _propTypes.default.oneOfType([_propTypes.default.bool.isRequired, _propTypes.default.number.isRequired, _propTypes.default.string.isRequired]),
  /**
     * Passes `isLoading` prop to the confirm button
     */
  isLoading: _propTypes.default.bool
};
/* OUI -> EUI Aliases: Build-Time */