"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx2 = _interopRequireDefault(require("clsx"));

var _popup = _interopRequireDefault(require("../popup"));

var _button = _interopRequireDefault(require("../button"));

var _actionBar = _interopRequireDefault(require("../action-bar"));

var _utils = require("../utils");

var _constant = require("../utils/constant");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

var _excluded = ["width", "title", "theme", "visible", "message", "className", "messageAlign", "closeOnClickOverlay", "onClickCloseIcon"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Dialog = function Dialog(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

  var _createNamespace = createNamespace('dialog', prefixCls),
      bem = _createNamespace[0];

  var width = props.width,
      title = props.title,
      theme = props.theme,
      visible = props.visible,
      message = props.message,
      className = props.className,
      messageAlign = props.messageAlign,
      closeOnClickOverlay = props.closeOnClickOverlay,
      onClickCloseIcon = props.onClickCloseIcon,
      others = _objectWithoutPropertiesLoose(props, _excluded);

  var renderTitle = function renderTitle() {
    if (props.title) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx2.default)(bem('header', {
          isolated: !props.message && !props.children
        }))
      }, title);
    }

    return null;
  };

  var renderContent = function renderContent() {
    if (props.children) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx2.default)(bem('content'))
      }, props.children);
    }

    if (message) {
      var _bem;

      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx2.default)(bem('content', {
          isolated: !title
        }))
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx2.default)(bem('message', (_bem = {
          'has-title': title
        }, _bem[messageAlign] = messageAlign, _bem)))
      }, message));
    }

    return null;
  };

  var renderButtons = function renderButtons() {
    var _props$cancelProps, _props$cancelProps2, _props$cancelProps3, _clsx, _props$confirmProps, _props$confirmProps2, _props$confirmProps3;

    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx2.default)(_constant.BORDER_TOP, bem('footer'))
    }, props.showCancelButton && /*#__PURE__*/_react.default.createElement(_button.default, {
      size: "large",
      text: props.cancelButtonText || locale.cancel,
      className: (0, _clsx2.default)(bem('cancel')),
      style: {
        color: props.cancelButtonColor
      },
      loading: (_props$cancelProps = props.cancelProps) == null ? void 0 : _props$cancelProps.loading,
      disabled: (_props$cancelProps2 = props.cancelProps) == null ? void 0 : _props$cancelProps2.disabled,
      onClick: (_props$cancelProps3 = props.cancelProps) != null && _props$cancelProps3.loading ? _utils.noop : props.onCancel
    }), props.showConfirmButton && /*#__PURE__*/_react.default.createElement(_button.default, {
      size: "large",
      text: props.confirmButtonText || locale.confirm,
      className: (0, _clsx2.default)(bem('confirm'), (_clsx = {}, _clsx[_constant.BORDER_LEFT] = props.showCancelButton, _clsx)),
      round: theme === 'round-button',
      style: {
        color: props.confirmButtonColor
      },
      loading: (_props$confirmProps = props.confirmProps) == null ? void 0 : _props$confirmProps.loading,
      disabled: (_props$confirmProps2 = props.confirmProps) == null ? void 0 : _props$confirmProps2.disabled,
      onClick: (_props$confirmProps3 = props.confirmProps) != null && _props$confirmProps3.loading ? _utils.noop : props.onConfirm
    }));
  };

  var renderRoundButtons = function renderRoundButtons() {
    var _props$cancelProps4, _props$cancelProps5, _props$cancelProps6, _props$confirmProps4, _props$confirmProps5, _props$confirmProps6;

    return /*#__PURE__*/_react.default.createElement(_actionBar.default, {
      className: (0, _clsx2.default)(bem('footer'))
    }, props.showCancelButton && /*#__PURE__*/_react.default.createElement(_actionBar.default.Button, {
      type: "warning",
      text: props.cancelButtonText || locale.cancel,
      className: (0, _clsx2.default)(bem('cancel')),
      color: props.cancelButtonColor,
      loading: (_props$cancelProps4 = props.cancelProps) == null ? void 0 : _props$cancelProps4.loading,
      disabled: (_props$cancelProps5 = props.cancelProps) == null ? void 0 : _props$cancelProps5.disabled,
      onClick: (_props$cancelProps6 = props.cancelProps) != null && _props$cancelProps6.loading ? _utils.noop : props.onCancel
    }), props.showConfirmButton && /*#__PURE__*/_react.default.createElement(_actionBar.default.Button, {
      type: "danger",
      text: props.confirmButtonText || locale.confirm,
      className: (0, _clsx2.default)(bem('confirm')),
      color: props.confirmButtonColor,
      loading: (_props$confirmProps4 = props.confirmProps) == null ? void 0 : _props$confirmProps4.loading,
      disabled: (_props$confirmProps5 = props.confirmProps) == null ? void 0 : _props$confirmProps5.disabled,
      onClick: (_props$confirmProps6 = props.confirmProps) != null && _props$confirmProps6.loading ? _utils.noop : props.onConfirm
    }));
  };

  var renderFooter = function renderFooter() {
    if (props.footer) return props.footer;
    return props.theme === 'round-button' ? renderRoundButtons() : renderButtons();
  };

  return /*#__PURE__*/_react.default.createElement(_popup.default, _extends({}, others, {
    visible: visible,
    className: (0, _clsx2.default)(bem([theme]), className),
    style: {
      width: (0, _utils.addUnit)(width)
    },
    "aria-labelledby": title || message,
    closeOnClickOverlay: closeOnClickOverlay,
    onClose: props.onClose,
    onClosed: props.onClosed
  }), renderTitle(), renderContent(), renderFooter());
};

Dialog.defaultProps = {
  transition: 'rv-dialog-bounce',
  showConfirmButton: true,
  closeOnPopstate: true
};
var _default = Dialog;
exports.default = _default;