import './style';
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { reactive, createVNode } from 'vue';
import getPrefixCls from '../_util/getPrefixCls';
import { createManager } from '../_util/noticeManager';
import Alert from '../alert/alert';
import PopupManager from '../_util/popupManager';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
const prefixCls = getPrefixCls('message');
const defaultConfig = {
  duration: 3,
  getContainer: () => document.body,
  top: '24px',
  colorful: false
};
let mergeConfig = defaultConfig;
let messageInstance = null;
const managerStyle = reactive({
  zIndex: 0,
  top: mergeConfig.top
});
function create(_ref) {
  var _messageInstance3, _messageInstance3$exi;
  let {
    type,
    content,
    duration,
    icon,
    closable,
    afterClose,
    colorful
  } = _ref;
  managerStyle.zIndex = PopupManager.nextZIndex();
  let item = null;
  function handleItemCloseClick() {
    var _messageInstance, _item;
    item && ((_messageInstance = messageInstance) === null || _messageInstance === void 0 ? void 0 : _messageInstance.remove((_item = item) === null || _item === void 0 ? void 0 : _item.key));
    item = null;
  }
  function destroyItem() {
    var _messageInstance2, _item2;
    item && ((_messageInstance2 = messageInstance) === null || _messageInstance2 === void 0 ? void 0 : _messageInstance2.remove((_item2 = item) === null || _item2 === void 0 ? void 0 : _item2.key));
    item = null;
  }
  function renderItem() {
    const classNames = [`${prefixCls}`];
    // 当前colorful判断优先
    if (!(colorful || colorful !== false && mergeConfig.colorful)) {
      classNames.push(`${prefixCls}-no-colorful`);
    }
    if (closable) {
      classNames.push(`${prefixCls}-close`);
    }
    const contentIsFunc = typeof content === 'function';
    const iconIsFunc = typeof icon === 'function';
    const scopedSlots = {
      default: contentIsFunc ? content : null,
      icon: iconIsFunc ? icon : null
    };
    item = messageInstance.append({
      afterRemove: afterClose,
      duration: duration != null && duration >= 0 ? duration : mergeConfig.duration,
      style: {
        zIndex: PopupManager.nextZIndex()
      },
      children: createVNode("div", {
        "class": `${prefixCls}-item`
      }, [createVNode(Alert, {
        "class": classNames,
        "type": type,
        "message": contentIsFunc ? '' : content,
        "showIcon": true,
        "closable": closable,
        "onClose": handleItemCloseClick
      }, scopedSlots)])
    });
  }
  if (!((_messageInstance3 = messageInstance) !== null && _messageInstance3 !== void 0 && (_messageInstance3$exi = _messageInstance3.exited) !== null && _messageInstance3$exi !== void 0 && _messageInstance3$exi.call(_messageInstance3))) {
    createManager({
      getContainer: mergeConfig.getContainer,
      transitionName: `${prefixCls}`,
      class: `${prefixCls}-wrapper`,
      maxCount: mergeConfig.maxCount,
      style: managerStyle
    }).then(instance => {
      messageInstance = instance;
      renderItem();
    });
  } else {
    renderItem();
  }
  return {
    destroy: destroyItem
  };
}
function message(type, options, duration) {
  const params = {
    type
  };
  if (typeof options === 'string') {
    params.content = options;
    params.duration = duration;
  } else {
    Object.assign(params, options);
  }
  return create(params);
}
const FMessage = {
  config(options) {
    if (options) {
      mergeConfig = _objectSpread(_objectSpread({}, defaultConfig), options);
    }
  },
  info: (content, duration) => message('info', content, duration),
  success: (content, duration) => message('success', content, duration),
  warning: (content, duration) => message('warning', content, duration),
  warn: (content, duration) => message('warning', content, duration),
  error: (content, duration) => message('error', content, duration),
  destroy() {
    if (messageInstance) {
      messageInstance.destroy();
    }
    messageInstance = null;
  }
};

export { FMessage, FMessage as default };
