import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React, { useMemo, useState, useEffect } from 'react';
import Tooltip from '../tooltip';
import { ConfigContext } from '../config-provider/context';
import Button from '../button/button';
var Popconfirm = /*#__PURE__*/React.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 = _objectWithoutProperties(props, ["title", "content", "color", "cancelText", "okText", "icon", "visible", "width", "onConfirm", "onCancel", "okButtonProps", "cancelButtonProps", "overlayInnerStyle"]);

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      confirmLoading = _useState2[0],
      setConfirmLoading = _useState2[1];

  var popconfirmPrefix = getPrefixClassName('popconfirm');

  var _useState3 = useState(visible),
      _useState4 = _slicedToArray(_useState3, 2),
      isShow = _useState4[0],
      setIsShow = _useState4[1];

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

  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 = useMemo(function () {
    return /*#__PURE__*/React.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.createElement("div", {
      className: "".concat(popconfirmPrefix, "-icon")
    }, icon), /*#__PURE__*/React.createElement("div", {
      className: icon ? "".concat(popconfirmPrefix, "-iconExit") : ''
    }, title && /*#__PURE__*/React.createElement("div", {
      className: "".concat(popconfirmPrefix, "-title")
    }, title), content && /*#__PURE__*/React.createElement("div", {
      className: "".concat(popconfirmPrefix, "-content")
    }, content), /*#__PURE__*/React.createElement("div", {
      className: "".concat(popconfirmPrefix, "-footer")
    }, /*#__PURE__*/React.createElement(Button, Object.assign({}, cancelButtonProps, {
      style: _objectSpread({
        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.createElement(Button, Object.assign({
      type: "primary"
    }, okButtonProps, {
      style: _objectSpread({
        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.createElement(Tooltip, Object.assign({
    ref: ref,
    visible: isShow,
    overlayClassName: popconfirmPrefix,
    color: color,
    trigger: ['click'],
    overlayInnerStyle: _objectSpread({
      maxWidth: 'none'
    }, overlayInnerStyle),
    onVisibleChange: function onVisibleChange(status) {
      if (typeof visible === 'undefined') {
        setIsShow(status);
      }
    },
    title: popconfirmContent
  }, reset));
});
Popconfirm.defaultProps = {
  cancelText: '取消',
  okText: '确定',
  color: '#ffffff'
};
export default Popconfirm;