var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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;
};
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);
}
import React, { useCallback, useEffect, useState } from "react";
import ReactDOM from "react-dom";
import { extend, isObject, noop, once } from "../utils";
import Notify from "./Notify";
import { resolveContainer } from "../utils/dom/getContainer";
import { lockClick } from "../toast/lock-click";
const NotifyNamespace = {};
function parseOptions(message) {
  return isObject(message) ? message : {
    message
  };
}
const notifyArray = [];
function syncClear() {
  let fn = notifyArray.pop();
  while (fn) {
    fn();
    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 = noop,
    duration
  } = _a, restProps = __objRest(_a, [
    "onClose",
    "duration"
  ]);
  let timer = 0;
  const userContainer = resolveContainer(props.teleport);
  const container = document.createElement("div");
  userContainer.appendChild(container);
  let destroy = noop;
  const TempNotify = () => {
    const [visible, setVisible] = useState(false);
    destroy = () => {
      setVisible(false);
      if (onClose)
        onClose();
    };
    const internalOnClosed = () => {
      const unmountResult = ReactDOM.unmountComponentAtNode(container);
      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    };
    const internalAfterClose = useCallback(once(() => {
      internalOnClosed();
    }), [onClose, container]);
    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__ */ React.createElement(Notify, _extends({}, restProps, {
      visible,
      teleport: () => container,
      onClose,
      onClosed: internalOnClosed
    }));
  };
  ReactDOM.render(/* @__PURE__ */ React.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) => {
  extend(NotifyNamespace.currentOptions, options);
};
const resetDefaultOptions = () => {
  NotifyNamespace.currentOptions = defaultOptions();
};
const clear = nextTickClear;
const exportNotifyNamespace = Object.assign(Notify, {
  show,
  setDefaultOptions,
  resetDefaultOptions,
  clear
});
var stdin_default = exportNotifyNamespace;
export {
  exportNotifyNamespace as Notify,
  NotifyNamespace,
  stdin_default as default
};
