import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["children", "customButton", "id", "onKeyDown", "buttonTriggerText", "buttonTriggerClassName", "triggerButtonKind", "disabled", "handleSubmit", "shouldCloseAfterSubmit"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import PropTypes from 'prop-types';
import React from 'react';
import Modal from '../Modal';
import Button from '../Button';
import { ButtonTypes } from '../../prop-types/types';
/** Modal Wrapper component to encapsulate your Modal within a button. */

var ModalWrapper = /*#__PURE__*/function (_React$Component) {
  _inherits(ModalWrapper, _React$Component);

  var _super = _createSuper(ModalWrapper);

  function ModalWrapper(props) {
    var _this;

    _classCallCheck(this, ModalWrapper);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "state", {
      isOpen: false
    });

    _defineProperty(_assertThisInitialized(_this), "handleOpen", function () {
      _this.setState({
        isOpen: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleClose", function () {
      _this.setState({
        isOpen: false
      }, function () {
        _this.triggerButton.current && _this.triggerButton.current.focus();
        if (_this.props.handleClose) _this.props.handleClose();
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleOnRequestSubmit", function () {
      var _this$props = _this.props,
          handleSubmit = _this$props.handleSubmit,
          shouldCloseAfterSubmit = _this$props.shouldCloseAfterSubmit;

      if (handleSubmit && handleSubmit()) {
        if (shouldCloseAfterSubmit) {
          _this.handleClose();
        }
      }
    });

    _this.triggerButton = /*#__PURE__*/React.createRef();
    return _this;
  }

  _createClass(ModalWrapper, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          children = _this$props2.children,
          customButton = _this$props2.customButton,
          id = _this$props2.id,
          _onKeyDown = _this$props2.onKeyDown,
          buttonTriggerText = _this$props2.buttonTriggerText,
          buttonTriggerClassName = _this$props2.buttonTriggerClassName,
          triggerButtonKind = _this$props2.triggerButtonKind,
          disabled = _this$props2.disabled,
          handleSubmit = _this$props2.handleSubmit,
          shouldCloseAfterSubmit = _this$props2.shouldCloseAfterSubmit,
          other = _objectWithoutProperties(_this$props2, _excluded);

      var props = _objectSpread(_objectSpread({}, other), {}, {
        open: this.state.isOpen,
        onRequestClose: this.handleClose,
        onRequestSubmit: this.handleOnRequestSubmit
      });

      var customButtonEl = customButton ? /*#__PURE__*/React.cloneElement(customButton, {
        disabled: disabled,
        onClick: this.handleOpen,
        inputref: this.triggerButton
      }) : undefined;
      return /*#__PURE__*/React.createElement("div", {
        role: "presentation",
        className: "wfp--modal__wrapper",
        onKeyDown: function onKeyDown(evt) {
          if (evt.which === 27) {
            _this2.handleClose();

            _onKeyDown(evt);
          }
        }
      }, customButton ? /*#__PURE__*/React.createElement(React.Fragment, null, customButtonEl) : /*#__PURE__*/React.createElement(Button, {
        id: id,
        className: buttonTriggerClassName,
        disabled: disabled,
        kind: triggerButtonKind,
        onClick: this.handleOpen,
        inputref: this.triggerButton
      }, buttonTriggerText), /*#__PURE__*/React.createElement(Modal, props, children));
    }
  }]);

  return ModalWrapper;
}(React.Component);

_defineProperty(ModalWrapper, "propTypes", {
  status: PropTypes.string,

  /**
   * Specify a function to open the Modal.
   */
  handleOpen: PropTypes.func,
  children: PropTypes.node,

  /**
   * Specify a custom trigger `Button`.
   */
  customButton: PropTypes.element,
  id: PropTypes.string,

  /**
   * Specify the text for the trigger `Button`.
   */
  buttonTriggerText: PropTypes.node,

  /**
   * Specify a `class` for the trigger `Button`.
   */
  buttonTriggerClassName: PropTypes.string,
  modalLabel: PropTypes.string,
  modalHeading: PropTypes.string,
  modalText: PropTypes.string,
  passiveModal: PropTypes.bool,
  withHeader: PropTypes.bool,
  modalBeforeContent: PropTypes.bool,
  primaryButtonText: PropTypes.string,
  secondaryButtonText: PropTypes.string,

  /**
   * Specify a `function` which is triggered when submitting the `Modal`.
   */
  handleSubmit: PropTypes.func,
  handleClose: PropTypes.func,
  disabled: PropTypes.bool,
  triggerButtonKind: ButtonTypes.buttonKind,

  /**
   * Specify if `Modal` should be closed after submitting.
   */
  shouldCloseAfterSubmit: PropTypes.bool
});

_defineProperty(ModalWrapper, "defaultProps", {
  primaryButtonText: 'Save',
  secondaryButtonText: 'Cancel',
  triggerButtonKind: 'primary',
  disabled: false,
  onKeyDown: function onKeyDown() {}
});

export { ModalWrapper as default };