import y, { useContext as w, useEffect as M, useCallback as c, useMemo as P } from "react";
var _ = globalThis && globalThis.__assign || function() {
  return _ = Object.assign || function(e) {
    for (var r, n = 1, o = arguments.length; n < o; n++) {
      r = arguments[n];
      for (var i in r)
        Object.prototype.hasOwnProperty.call(r, i) && (e[i] = r[i]);
    }
    return e;
  }, _.apply(this, arguments);
}, E = globalThis && globalThis.__rest || function(e, r) {
  var n = {};
  for (var o in e)
    Object.prototype.hasOwnProperty.call(e, o) && r.indexOf(o) < 0 && (n[o] = e[o]);
  if (e != null && typeof Object.getOwnPropertySymbols == "function")
    for (var i = 0, o = Object.getOwnPropertySymbols(e); i < o.length; i++)
      r.indexOf(o[i]) < 0 && Object.prototype.propertyIsEnumerable.call(e, o[i]) && (n[o[i]] = e[o[i]]);
  return n;
}, m = Symbol("NiceModalId"), N = {}, j = y.createContext(N), x = y.createContext(null), C = {}, S = 0, k = function() {
  throw new Error("No dispatch method detected, did you embed your app with NiceModal.Provider?");
}, T = function() {
  return "_nice_modal_" + S++;
}, v = {}, p = {}, O = function(e) {
  return typeof e == "string" ? e : (e[m] || (e[m] = T()), e[m]);
};
function H(e, r) {
  var n = O(e);
  if (typeof e != "string" && !C[n] && V(n, e), k(), !v[n]) {
    var o, i, l = new Promise(function(t, a) {
      o = t, i = a;
    });
    v[n] = {
      resolve: o,
      reject: i,
      promise: l
    };
  }
  return v[n].promise;
}
function D(e) {
  var r = O(e);
  if (k(), delete v[r], !p[r]) {
    var n, o, i = new Promise(function(l, t) {
      n = l, o = t;
    });
    p[r] = {
      resolve: n,
      reject: o,
      promise: i
    };
  }
  return p[r].promise;
}
var U = function(e) {
  var r = O(e);
  k(), delete v[r], delete p[r];
}, A = function(e, r) {
  k();
};
function F(e, r) {
  var n = w(j), o = w(x), i = null, l = e && typeof e != "string";
  if (e ? i = O(e) : i = o, !i)
    throw new Error("No modal id found in NiceModal.useModal.");
  var t = i;
  M(function() {
    l && !C[t] && V(t, e, r);
  }, [l, t, e, r]);
  var a = n[t], s = c(function(f) {
    return H(t);
  }, [t]), d = c(function() {
    return D(t);
  }, [t]), b = c(function() {
    return U(t);
  }, [t]), h = c(function(f) {
    var u;
    (u = v[t]) === null || u === void 0 || u.resolve(f), delete v[t];
  }, [t]), g = c(function(f) {
    var u;
    (u = v[t]) === null || u === void 0 || u.reject(f), delete v[t];
  }, [t]), I = c(function(f) {
    var u;
    (u = p[t]) === null || u === void 0 || u.resolve(f), delete p[t];
  }, [t]);
  return P(function() {
    return {
      id: t,
      args: a == null ? void 0 : a.args,
      visible: !!(a != null && a.visible),
      keepMounted: !!(a != null && a.keepMounted),
      show: s,
      hide: d,
      remove: b,
      resolve: h,
      reject: g,
      resolveHide: I
    };
  }, [
    t,
    a == null ? void 0 : a.args,
    a == null ? void 0 : a.visible,
    a == null ? void 0 : a.keepMounted,
    s,
    d,
    b,
    h,
    g,
    I
  ]);
}
var q = function(e) {
  return function(r) {
    var n, o = r.defaultVisible, i = r.keepMounted, l = r.id, t = E(r, ["defaultVisible", "keepMounted", "id"]), a = F(l), s = a.args, d = a.show, b = w(j), h = !!b[l];
    M(function() {
      return o && d(), function() {
      };
    }, [l, d, o]), M(function() {
      i && A();
    }, [l, i]);
    var g = (n = b[l]) === null || n === void 0 ? void 0 : n.delayVisible;
    return M(function() {
      g && d(s);
    }, [g, s, d]), h ? y.createElement(
      x.Provider,
      { value: l },
      y.createElement(e, _({}, t, s))
    ) : null;
  };
}, V = function(e, r, n) {
  C[e] ? C[e].props = n : C[e] = { comp: r, props: n };
}, G = function(e) {
  return {
    visible: e.visible,
    onOk: function() {
      return e.hide();
    },
    onCancel: function() {
      return e.hide();
    },
    afterClose: function() {
      e.resolveHide(), e.keepMounted || e.remove();
    }
  };
}, z = function(e) {
  var r = G(e), n = r.onOk, o = r.onCancel, i = r.afterClose;
  return {
    open: e.visible,
    onOk: n,
    onCancel: o,
    afterClose: i
  };
}, L = function(e) {
  return {
    visible: e.visible,
    onClose: function() {
      return e.hide();
    },
    afterVisibleChange: function(r) {
      r || e.resolveHide(), !r && !e.keepMounted && e.remove();
    }
  };
}, B = function(e) {
  var r = L(e), n = r.onClose, o = r.afterVisibleChange;
  return {
    open: e.visible,
    onClose: n,
    afterOpenChange: o
  };
};
export {
  j as NiceModalContext,
  L as antdDrawer,
  B as antdDrawerV5,
  G as antdModal,
  z as antdModalV5,
  q as create,
  D as hide,
  V as register,
  U as remove,
  H as show,
  F as useModal
};
