import E from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as a from "react";
import { render as F } from "../../../rc-util/es/React/render.mjs";
import O, { warnContext as N, globalConfig as b } from "../config-provider/index.mjs";
import w from "./PurePanel.mjs";
import M, { useInternalMessage as j } from "./useMessage.mjs";
import { wrapPromiseFn as h } from "./util.mjs";
let r = null, l = (e) => e(), f = [], u = {};
function v() {
  const {
    prefixCls: e,
    getContainer: t,
    duration: s,
    rtl: n,
    maxCount: o,
    top: i
  } = u, c = e ?? b().getPrefixCls("message"), d = (t == null ? void 0 : t()) || document.body;
  return {
    prefixCls: c,
    getContainer: () => d,
    duration: s,
    rtl: n,
    maxCount: o,
    top: i
  };
}
const D = /* @__PURE__ */ a.forwardRef((e, t) => {
  const [s, n] = a.useState(v), [o, i] = j(s), c = b(), d = c.getRootPrefixCls(), y = c.getIconPrefixCls(), P = c.getTheme(), m = () => {
    n(v);
  };
  return a.useEffect(m, []), a.useImperativeHandle(t, () => {
    const p = Object.assign({}, o);
    return Object.keys(p).forEach((C) => {
      p[C] = function() {
        return m(), o[C].apply(o, arguments);
      };
    }), {
      instance: p,
      sync: m
    };
  }), /* @__PURE__ */ a.createElement(O, {
    prefixCls: d,
    iconPrefixCls: y,
    theme: P
  }, i);
});
function g() {
  if (!r) {
    const e = document.createDocumentFragment(), t = {
      fragment: e
    };
    r = t, l(() => {
      F(/* @__PURE__ */ a.createElement(D, {
        ref: (s) => {
          const {
            instance: n,
            sync: o
          } = s || {};
          Promise.resolve().then(() => {
            !t.instance && n && (t.instance = n, t.sync = o, g());
          });
        }
      }), e);
    });
    return;
  }
  r.instance && (f.forEach((e) => {
    const {
      type: t,
      skipped: s
    } = e;
    if (!s)
      switch (t) {
        case "open": {
          l(() => {
            const n = r.instance.open(Object.assign(Object.assign({}, u), e.config));
            n == null || n.then(e.resolve), e.setCloseFn(n);
          });
          break;
        }
        case "destroy":
          l(() => {
            r == null || r.instance.destroy(e.key);
          });
          break;
        default:
          l(() => {
            var n;
            const o = (n = r.instance)[t].apply(n, E(e.args));
            o == null || o.then(e.resolve), e.setCloseFn(o);
          });
      }
  }), f = []);
}
function G(e) {
  u = Object.assign(Object.assign({}, u), e), l(() => {
    var t;
    (t = r == null ? void 0 : r.sync) === null || t === void 0 || t.call(r);
  });
}
function I(e) {
  const t = h((s) => {
    let n;
    const o = {
      type: "open",
      config: e,
      resolve: s,
      setCloseFn: (i) => {
        n = i;
      }
    };
    return f.push(o), () => {
      n ? l(() => {
        n();
      }) : o.skipped = !0;
    };
  });
  return g(), t;
}
function _(e, t) {
  process.env.NODE_ENV !== "production" && N("message");
  const s = h((n) => {
    let o;
    const i = {
      type: e,
      args: t,
      resolve: n,
      setCloseFn: (c) => {
        o = c;
      }
    };
    return f.push(i), () => {
      o ? l(() => {
        o();
      }) : i.skipped = !0;
    };
  });
  return g(), s;
}
function R(e) {
  f.push({
    type: "destroy",
    key: e
  }), g();
}
const V = ["success", "info", "warning", "error", "loading"], A = {
  open: I,
  destroy: R,
  config: G,
  useMessage: M,
  _InternalPanelDoNotUseOrYouWillBeFired: w
}, x = A;
V.forEach((e) => {
  x[e] = function() {
    for (var t = arguments.length, s = new Array(t), n = 0; n < t; n++)
      s[n] = arguments[n];
    return _(e, s);
  };
});
process.env.NODE_ENV;
process.env.NODE_ENV;
const U = x;
export {
  U as default
};
