"use strict";

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});

var jsx_runtime_1 = require("react/jsx-runtime");

var classnames_1 = __importDefault(require("classnames"));

var rc_notification_1 = __importDefault(require("rc-notification"));

var button_1 = require("../button");

var NotificationContainer_1 = __importDefault(require("./NotificationContainer"));

require("./style/notification.css");

var notificationInstance = {};
var defaultDuration = 4.5;
var defaultTop = 24;
var defaultBottom = 24;
var defaultPlacement = 'topRight';
var rtl = false;

var setNotificationConfig = function setNotificationConfig(options) {
  var duration = options.duration,
      placement = options.placement,
      bottom = options.bottom,
      top = options.top;

  if (duration !== undefined) {
    defaultDuration = duration;
  }

  if (placement !== undefined) {
    defaultPlacement = placement;
  }

  if (bottom !== undefined) {
    defaultBottom = bottom;
  }

  if (top !== undefined) {
    defaultTop = top;
  }

  if (options.rtl !== undefined) {
    rtl = options.rtl;
  }
};

var getPlacementStyle = function getPlacementStyle(placement, top, bottom) {
  if (top === void 0) {
    top = defaultTop;
  }

  if (bottom === void 0) {
    bottom = defaultBottom;
  }

  var style;

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

    case 'topRight':
      style = {
        right: 0,
        top: top,
        bottom: 'auto'
      };
      break;

    case 'bottomLeft':
      style = {
        left: 0,
        top: 'auto',
        bottom: bottom
      };
      break;

    default:
      style = {
        right: 0,
        top: 'auto',
        bottom: bottom
      };
      break;
  }

  return style;
};

var getNotificationInstance = function getNotificationInstance(args, callback) {
  var _a;

  var top = args.top,
      bottom = args.bottom,
      _b = args.placement,
      placement = _b === void 0 ? defaultPlacement : _b;
  var outerPrefixCls = 'dmc-notification';
  var cacheKey = outerPrefixCls + "-" + placement;
  var cacheInstance = notificationInstance[cacheKey];

  if (cacheInstance) {
    Promise.resolve(cacheInstance).then(function (instance) {
      callback({
        instance: instance
      });
    });
    return;
  }

  var closeIconToRender = jsx_runtime_1.jsx(button_1.IconButton, {
    type: 'only-icon',
    icon: 'close',
    className: outerPrefixCls + "-close-icon"
  }, void 0);
  var notificationClass = classnames_1["default"](outerPrefixCls + "-" + placement, (_a = {}, _a[outerPrefixCls + "-rtl"] = rtl === true, _a));
  notificationInstance[cacheKey] = new Promise(function (resolve) {
    rc_notification_1["default"].newInstance({
      prefixCls: outerPrefixCls,
      className: notificationClass,
      style: getPlacementStyle(placement, top, bottom),
      closeIcon: closeIconToRender
    }, function (notification) {
      resolve(notification);
      callback({
        instance: notification
      });
    });
  });
};

var typeToIcon = {
  success: 'success',
  info: 'info',
  error: 'alert',
  warning: 'help-2'
};

var getRCNoticeProps = function getRCNoticeProps(args) {
  var duration = args.duration === undefined ? defaultDuration : args.duration;
  return {
    content: jsx_runtime_1.jsx(NotificationContainer_1["default"], {
      message: args.message,
      description: args.description,
      footer: args.footer,
      emotion: typeToIcon[args.type] || args.emotion
    }, void 0),
    duration: duration,
    closable: true,
    onClose: args.onClose,
    onClick: args.onClick,
    key: args.key,
    style: args.style || {},
    className: args.className
  };
};

var api = {
  open: function open(args) {
    getNotificationInstance(args, function (_a) {
      var instance = _a.instance;
      instance.notice(getRCNoticeProps(args));
    });
  },
  close: function close(key) {
    Object.keys(notificationInstance).forEach(function (cacheKey) {
      return Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
        instance.removeNotice(key);
      });
    });
  },
  config: setNotificationConfig,
  destroy: function destroy() {
    Object.keys(notificationInstance).forEach(function (cacheKey) {
      Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
        instance.destroy();
      });
      delete notificationInstance[cacheKey]; // lgtm[js/missing-await]
    }); // 配置还原

    defaultDuration = 4.5;
    defaultTop = 24;
    defaultBottom = 24;
    defaultPlacement = 'topRight';
    rtl = false;
  }
};
['success', 'info', 'warning', 'error'].forEach(function (type) {
  api[type] = function (args) {
    return api.open(__assign(__assign({}, args), {
      type: type
    }));
  };
});
exports["default"] = api;

