var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  Notify: () => exportNotifyNamespace,
  NotifyNamespace: () => NotifyNamespace,
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_react_dom = __toESM(require("react-dom"));
var import_utils = require("../utils");
var import_Notify = __toESM(require("./Notify"));
var import_getContainer = require("../utils/dom/getContainer");
var import_lock_click = require("../toast/lock-click");
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
const NotifyNamespace = {};
function parseOptions(message) {
  return (0, import_utils.isObject)(message) ? message : {
    message
  };
}
const notifyArray = [];
function syncClear() {
  let fn = notifyArray.pop();
  while (fn) {
    fn();
    (0, import_lock_click.lockClick)(false);
    fn = notifyArray.pop();
  }
}
function nextTickClear() {
  setTimeout(syncClear);
}
const show = (p) => {
  const props = parseOptions(p);
  const interProps = __spreadValues(__spreadValues({}, NotifyNamespace.currentOptions), props);
  const _a = interProps, {
    onClose = import_utils.noop,
    duration
  } = _a, restProps = __objRest(_a, [
    "onClose",
    "duration"
  ]);
  let timer = 0;
  const userContainer = (0, import_getContainer.resolveContainer)(props.teleport);
  const container = document.createElement("div");
  userContainer.appendChild(container);
  let destroy = import_utils.noop;
  const TempNotify = () => {
    const [visible, setVisible] = (0, import_react.useState)(false);
    destroy = () => {
      setVisible(false);
      if (onClose)
        onClose();
    };
    const internalOnClosed = () => {
      const unmountResult = import_react_dom.default.unmountComponentAtNode(container);
      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    };
    const internalAfterClose = (0, import_react.useCallback)((0, import_utils.once)(() => {
      internalOnClosed();
    }), [onClose, container]);
    (0, import_react.useEffect)(() => {
      setVisible(true);
      syncClear();
      notifyArray.push(internalAfterClose);
      if (duration && +duration > 0) {
        timer = window.setTimeout(destroy, duration);
      }
      return () => {
        if (timer !== 0) {
          window.clearTimeout(timer);
        }
      };
    }, []);
    return /* @__PURE__ */ import_react.default.createElement(import_Notify.default, _extends({}, restProps, {
      visible,
      teleport: () => container,
      onClose,
      onClosed: internalOnClosed
    }));
  };
  import_react_dom.default.render(/* @__PURE__ */ import_react.default.createElement(TempNotify, null), container);
  return destroy;
};
function defaultOptions() {
  return {
    type: "danger",
    color: void 0,
    message: "",
    onClose: void 0,
    onClick: void 0,
    duration: 3e3,
    className: "",
    lockScroll: false,
    background: void 0
  };
}
NotifyNamespace.currentOptions = defaultOptions();
const setDefaultOptions = (options) => {
  (0, import_utils.extend)(NotifyNamespace.currentOptions, options);
};
const resetDefaultOptions = () => {
  NotifyNamespace.currentOptions = defaultOptions();
};
const clear = nextTickClear;
const exportNotifyNamespace = Object.assign(import_Notify.default, {
  show,
  setDefaultOptions,
  resetDefaultOptions,
  clear
});
var stdin_default = exportNotifyNamespace;
