import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import React from 'react';
import classNames from 'classnames';
import Button from '../button';
import { Icons, RcNotification } from '../components';
import { getCurIcon } from '../utils/getTypedIcon';
import { getPrefixClassName } from '../config-provider/context';
var CloseLine = Icons.CloseLine;
var prefixCls = getPrefixClassName('notification');
var defaultShowBtnsNotiKey = 'isShowBtns';
var notifyTypes = ['success', 'info', 'warning', 'error'];

var getRcNotiInsStyle = function getRcNotiInsStyle(placement) {
  var style = {};
  var defaultDistance = 24;

  switch (placement) {
    case 'topLeft':
      style = {
        top: defaultDistance,
        left: defaultDistance
      };
      break;

    case 'topRight':
      style = {
        top: defaultDistance,
        right: defaultDistance
      };
      break;

    case 'bottomRight':
      style = {
        bottom: defaultDistance,
        right: defaultDistance
      };
      break;

    case 'bottomLeft':
      style = {
        bottom: defaultDistance,
        left: defaultDistance
      };
      break;

    default:
      break;
  }

  return style;
};

var getPlacementCls = function getPlacementCls(classname, customPlacement) {
  var placement = customPlacement || Notification.defaultProps.placement;
  var cls = "".concat(classname, "-").concat(placement);
  return {
    cls: cls,
    placement: placement
  };
};

var getNotificationIns = function getNotificationIns(props, callback) {
  var customPrefix = props.prefix,
      customPlacement = props.placement,
      insStyle = props.insStyle;
  prefixCls = getPrefixClassName('notification', customPrefix, Notification.globalProps.prefix);

  var _getPlacementCls = getPlacementCls(prefixCls, customPlacement),
      cls = _getPlacementCls.cls,
      placement = _getPlacementCls.placement;

  var notificationIns = Notification.instanceMap[cls];

  var rRcNotiInsStyle = _objectSpread(_objectSpread({}, getRcNotiInsStyle(placement)), insStyle);

  if (notificationIns) {
    return callback(notificationIns);
  }

  RcNotification.newInstance({
    prefixCls: prefixCls,
    getContainer: props.getContainer || Notification.globalProps.getContainer,
    style: rRcNotiInsStyle
  }, function (notification) {
    Notification.instanceMap[cls] = notification;
    callback(notification);
  });
};

var handleOperation = function handleOperation(props, operKey) {
  var _props$operKey;

  var key = props.key;
  if (!key) return;
  (_props$operKey = props[operKey]) === null || _props$operKey === void 0 ? void 0 : _props$operKey.call(props);
  NotificationApi.close(key || defaultShowBtnsNotiKey);
};

var NotiContent = function NotiContent(props) {
  var type = props.type,
      showBtns = props.showBtns,
      btn = props.btn,
      showColorBorder = props.showColorBorder;
  var contentPrefix = "".concat(prefixCls, "-content");
  var colorBorderPrefixCls = "".concat(prefixCls, "-color-border");
  var statusCls = '';

  if (type) {
    statusCls = "".concat(prefixCls, "-color-").concat(type);
  }

  var contentStatusCls = classNames("".concat(contentPrefix, "-status"), "".concat(statusCls), _defineProperty({}, "".concat(contentPrefix, "-reset"), showColorBorder));
  var CurIcon = getCurIcon(Icons, type, true);
  return /*#__PURE__*/React.createElement(React.Fragment, null, showColorBorder ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(colorBorderPrefixCls, " ").concat(statusCls)
  }) : null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(contentPrefix)
  }, type ? /*#__PURE__*/React.createElement("div", {
    className: contentStatusCls
  }, CurIcon) : null, /*#__PURE__*/React.createElement("div", {
    style: type ? {
      marginLeft: 31
    } : undefined
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(contentPrefix, "-message")
  }, props.message), /*#__PURE__*/React.createElement("div", {
    className: "".concat(contentPrefix, "-desc")
  }, props.description)), showBtns ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(contentPrefix, "-btns")
  }, btn || /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
    className: "".concat(contentPrefix, "-btns-cancel"),
    size: "small",
    onClick: function onClick() {
      return handleOperation(props, 'cancel');
    }
  }, "\u53D6\u6D88"), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    size: "small",
    onClick: function onClick() {
      return handleOperation(props, 'confirm');
    }
  }, "\u786E\u5B9A"))) : null));
};

var getNotiKey = function getNotiKey(props) {
  if (props.key) {
    return props.key;
  }

  if (props.showBtns && !props.key) {
    return defaultShowBtnsNotiKey;
  }
};

var commonNotice = function commonNotice(props) {
  var dur = Notification.defaultProps.duration;

  var _getPlacementCls2 = getPlacementCls(prefixCls, props.placement),
      cls = _getPlacementCls2.cls;

  var className = classNames(props.className, cls);

  if ('duration' in Notification.globalProps) {
    dur = Notification.globalProps.duration;
  }

  if ('duration' in props) {
    dur = props.duration;
  }

  getNotificationIns(props, function (notification) {
    notification.notice({
      content: NotiContent(props),
      closeIcon: /*#__PURE__*/React.createElement(CloseLine, null),
      closable: true,
      onClose: function onClose() {
        handleOperation(props, 'cancel');
      },
      key: getNotiKey(props),
      duration: dur,
      className: className,
      style: props.style
    });
  });
};

var defaultProps = {
  duration: 4.5,
  style: {
    right: 24,
    top: 24
  },
  placement: 'topRight'
};
var Notification = {
  defaultProps: defaultProps,
  globalProps: {},
  instanceMap: {}
};
var NotificationApi = {
  config: function config(globalProps) {
    Notification.globalProps = globalProps;
  },
  open: function open(props) {
    return commonNotice(props);
  },
  close: function close(key) {
    if (!key) return;
    Object.keys(Notification.instanceMap).forEach(function (insKey) {
      var ins = Notification.instanceMap[insKey];
      ins === null || ins === void 0 ? void 0 : ins.removeNotice(key);
    });
  },
  destroy: function destroy() {
    Object.keys(Notification.instanceMap).forEach(function (insKey) {
      var ins = Notification.instanceMap[insKey];
      ins === null || ins === void 0 ? void 0 : ins.destroy();
      Notification.instanceMap[insKey] = null;
    });
  }
};
notifyTypes.forEach(function (type) {
  NotificationApi[type] = function (args) {
    NotificationApi.open(_objectSpread(_objectSpread({}, args), {}, {
      type: type
    }));
  };
}); // 欺骗一下doc-typescript，自动生成md文档

var NotificationDoczCheat = function NotificationDoczCheat() {
  return null;
};

NotificationDoczCheat.defaultProps = defaultProps;
export { NotificationDoczCheat };
export default NotificationApi;