"use strict";

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

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

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

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

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

var _classnames = _interopRequireDefault(require("classnames"));

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

var _components = require("../components");

var _getTypedIcon = require("../utils/getTypedIcon");

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

var CloseLine = _components.Icons.CloseLine;
var prefixCls = (0, _context.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 = (0, _context.getPrefixClassName)('notification', customPrefix, Notification.globalProps.prefix);

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

  var notificationIns = Notification.instanceMap[cls];
  var rRcNotiInsStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, getRcNotiInsStyle(placement)), insStyle);

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

  _components.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 = (0, _classnames.default)("".concat(contentPrefix, "-status"), "".concat(statusCls), (0, _defineProperty2.default)({}, "".concat(contentPrefix, "-reset"), showColorBorder));
  var CurIcon = (0, _getTypedIcon.getCurIcon)(_components.Icons, type, true);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showColorBorder ? /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(colorBorderPrefixCls, " ").concat(statusCls)
  }) : null, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(contentPrefix)
  }, type ? /*#__PURE__*/_react.default.createElement("div", {
    className: contentStatusCls
  }, CurIcon) : null, /*#__PURE__*/_react.default.createElement("div", {
    style: type ? {
      marginLeft: 31
    } : undefined
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(contentPrefix, "-message")
  }, props.message), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(contentPrefix, "-desc")
  }, props.description)), showBtns ? /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(contentPrefix, "-btns")
  }, btn || /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_button.default, {
    className: "".concat(contentPrefix, "-btns-cancel"),
    size: "small",
    onClick: function onClick() {
      return handleOperation(props, 'cancel');
    }
  }, "\u53D6\u6D88"), /*#__PURE__*/_react.default.createElement(_button.default, {
    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 = (0, _classnames.default)(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.default.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((0, _objectSpread2.default)((0, _objectSpread2.default)({}, args), {}, {
      type: type
    }));
  };
}); // 欺骗一下doc-typescript，自动生成md文档

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

exports.NotificationDoczCheat = NotificationDoczCheat;
NotificationDoczCheat.defaultProps = defaultProps;
var _default = NotificationApi;
exports.default = _default;