import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import React from 'react';
import isPromise from 'is-promise';
import noop from 'lodash/noop';
import isString from 'lodash/isString';
import { MessageManager } from 'choerodon-ui/shared';
import Icon from '../icon';
import Progress from '../progress';
import { ProgressType } from '../progress/enum';
import { Size } from '../_util/enum';
import { newNotificationInstance } from '../notification/Notification';
import { getPlacementStyle, getPlacementTransitionName } from './util';
import { getPrefixCls } from '../configure/utils';
var _config = MessageManager.config;

function getCustomizePrefixCls() {
  return getPrefixCls('message', _config.prefixCls);
}

function getMessageInstance(placement, callback, contentClassName) {
  var instance = MessageManager.instance;

  if (instance) {
    if (isPromise(instance)) {
      instance.then(callback);
      return instance;
    }

    callback(instance);
    return Promise.resolve(instance);
  }

  var promise = new Promise(function (resolve) {
    newNotificationInstance({
      prefixCls: getCustomizePrefixCls(),
      style: getPlacementStyle(placement, _config.top, _config.bottom),
      transitionName: getPlacementTransitionName(placement, _config.transitionName),
      getContainer: _config.getContainer,
      contentClassName: contentClassName,
      maxCount: _config.maxCount
    }, function (instance) {
      resolve(instance);
      MessageManager.instance = instance;
      callback(instance);
    });
  });
  MessageManager.instance = promise;
  return promise;
}

function notice(content) {
  var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _config.duration;
  var type = arguments.length > 2 ? arguments[2] : undefined;

  var _onClose = arguments.length > 3 ? arguments[3] : undefined;

  var placement = arguments.length > 4 ? arguments[4] : undefined;
  var iconType = _config.icons[type];

  if (isString(_onClose)) {
    placement = _onClose;
    _onClose = noop;
  }

  if (typeof duration === 'function') {
    _onClose = duration;
    duration = _config.duration;
  } else if (isString(duration)) {
    placement = duration;
  }

  var target = MessageManager.getUuid();
  var prefixCls = getCustomizePrefixCls();
  var icon = iconType === 'loading' ? /*#__PURE__*/React.createElement(Progress, {
    type: ProgressType.loading,
    size: Size.small
  }) : /*#__PURE__*/React.createElement(Icon, {
    type: iconType
  });
  var promise;
  var closePromise = new Promise(function (resolve) {
    promise = getMessageInstance(placement || _config.placement, function (instance) {
      instance.notice({
        key: target,
        duration: duration,
        style: {},
        contentClassName: "".concat(prefixCls, "-content-").concat(type),
        children: /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-custom-content ").concat(prefixCls, "-").concat(type)
        }, icon, /*#__PURE__*/React.createElement("span", null, content)),
        onClose: function onClose() {
          if (typeof _onClose === 'function') {
            _onClose();
          }

          resolve();
        }
      });
    }, "".concat(prefixCls, "-content-").concat(type));
  });

  var result = function result() {
    if (promise) {
      promise.then(function (ins) {
        return ins.removeNotice(target);
      });
    }
  };

  result.then = function (filled, rejected) {
    return closePromise.then(filled, rejected);
  };

  result.promise = closePromise;
  return result;
}

export default {
  info: function info(content, duration, onClose, placement) {
    return notice(content, duration, 'info', onClose, placement);
  },
  success: function success(content, duration, onClose, placement) {
    return notice(content, duration, 'success', onClose, placement);
  },
  error: function error(content, duration, onClose, placement) {
    return notice(content, duration, 'error', onClose, placement);
  },
  // Departed usage, please use warning()
  warn: function warn(content, duration, onClose, placement) {
    return notice(content, duration, 'warning', onClose, placement);
  },
  warning: function warning(content, duration, onClose, placement) {
    return notice(content, duration, 'warning', onClose, placement);
  },
  loading: function loading(content, duration, onClose, placement) {
    return notice(content, duration, 'loading', onClose, placement);
  },
  config: function config(options) {
    if (options.top !== undefined) {
      _config.top = options.top;
      MessageManager.clear();
    }

    if (options.bottom !== undefined) {
      _config.bottom = options.bottom;
      MessageManager.clear();
    }

    if (options.duration !== undefined) {
      _config.duration = options.duration;
    }

    if (options.prefixCls !== undefined) {
      _config.prefixCls = options.prefixCls;
    }

    if (options.getContainer !== undefined) {
      _config.getContainer = options.getContainer;
    }

    if (options.transitionName !== undefined) {
      _config.transitionName = options.transitionName;
      MessageManager.clear();
    }

    if (options.placement !== undefined) {
      _config.placement = options.placement;
    }

    if (options.maxCount !== undefined) {
      _config.maxCount = options.maxCount;
    }

    if (options.icons !== undefined) {
      _config.icons = _objectSpread(_objectSpread({}, _config.icons), options.icons);
    }
  },
  destroy: function destroy() {
    MessageManager.clear();
  }
};
//# sourceMappingURL=index.js.map
