"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _context = require("../config-provider/context");

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

var Popconfirm = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var title = props.title,
      content = props.content,
      color = props.color,
      cancelText = props.cancelText,
      okText = props.okText,
      icon = props.icon,
      visible = props.visible,
      width = props.width,
      onConfirm = props.onConfirm,
      onCancel = props.onCancel,
      okButtonProps = props.okButtonProps,
      cancelButtonProps = props.cancelButtonProps,
      overlayInnerStyle = props.overlayInnerStyle,
      reset = (0, _objectWithoutProperties2.default)(props, ["title", "content", "color", "cancelText", "okText", "icon", "visible", "width", "onConfirm", "onCancel", "okButtonProps", "cancelButtonProps", "overlayInnerStyle"]);

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      confirmLoading = _useState2[0],
      setConfirmLoading = _useState2[1];

  var popconfirmPrefix = getPrefixClassName('popconfirm');

  var _useState3 = (0, _react.useState)(visible),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      isShow = _useState4[0],
      setIsShow = _useState4[1];

  if (visible !== undefined && visible !== isShow) {
    setIsShow(visible);
    if (!visible) setConfirmLoading(false);
  }

  (0, _react.useEffect)(function () {
    if ((okButtonProps === null || okButtonProps === void 0 ? void 0 : okButtonProps.loading) !== undefined) {
      setConfirmLoading(okButtonProps.loading);
    }
  }, [okButtonProps === null || okButtonProps === void 0 ? void 0 : okButtonProps.loading]);
  var popconfirmContent = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react.default.createElement("div", {
      style: typeof width !== 'undefined' ? {
        width: width,
        maxWidth: 'none',
        backgroundColor: color
      } : {
        backgroundColor: color
      },
      className: "".concat(popconfirmPrefix, "-wrapper ").concat(title ? 'with-title' : '')
    }, icon && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(popconfirmPrefix, "-icon")
    }, icon), /*#__PURE__*/_react.default.createElement("div", {
      className: icon ? "".concat(popconfirmPrefix, "-iconExit") : ''
    }, title && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(popconfirmPrefix, "-title")
    }, title), content && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(popconfirmPrefix, "-content")
    }, content), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(popconfirmPrefix, "-footer")
    }, /*#__PURE__*/_react.default.createElement(_button.default, Object.assign({}, cancelButtonProps, {
      style: (0, _objectSpread2.default)({
        width: '68px',
        height: '28px',
        paddingTop: '0',
        paddingBottom: '0'
      }, cancelButtonProps === null || cancelButtonProps === void 0 ? void 0 : cancelButtonProps.style),
      className: "btn-cancel",
      onClick: function onClick(e) {
        setIsShow(false);
        onCancel === null || onCancel === void 0 ? void 0 : onCancel(e);
      }
    }), cancelText), /*#__PURE__*/_react.default.createElement(_button.default, Object.assign({
      type: "primary"
    }, okButtonProps, {
      style: (0, _objectSpread2.default)({
        width: '68px',
        height: '28px',
        paddingTop: '0',
        paddingBottom: '0'
      }, okButtonProps === null || okButtonProps === void 0 ? void 0 : okButtonProps.style),
      loading: confirmLoading,
      onClick: function onClick(e) {
        var result = onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(e);

        if (visible === undefined) {
          if (result && typeof result.then === 'function') {
            setConfirmLoading(true);
            result.then(function () {
              setIsShow(false);
            }).finally(function () {
              setConfirmLoading(false);
            });
          } else {
            setIsShow(false);
          }
        }
      }
    }), okText))));
  }, [width, title, content, icon, cancelText, okText, onCancel, onConfirm, visible, confirmLoading, color, popconfirmPrefix]);
  return /*#__PURE__*/_react.default.createElement(_tooltip.default, Object.assign({
    ref: ref,
    visible: isShow,
    overlayClassName: popconfirmPrefix,
    color: color,
    trigger: ['click'],
    overlayInnerStyle: (0, _objectSpread2.default)({
      maxWidth: 'none'
    }, overlayInnerStyle),
    onVisibleChange: function onVisibleChange(status) {
      if (typeof visible === 'undefined') {
        setIsShow(status);
      }
    },
    title: popconfirmContent
  }, reset));
});

Popconfirm.defaultProps = {
  cancelText: '取消',
  okText: '确定',
  color: '#ffffff'
};
var _default = Popconfirm;
exports.default = _default;