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 __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, {
  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_getContainer = require("../utils/dom/getContainer");
var import_lock_click = require("./lock-click");
var import_Toast = __toESM(require("./Toast"));
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 defaultOptions = {
  icon: "",
  message: "",
  className: "",
  type: "info",
  position: "middle",
  forbidClick: false,
  duration: 2e3,
  teleport: () => document.body
};
const toastQueue = [];
let allowMultiple = false;
let currentOptions = (0, import_utils.extend)({}, defaultOptions);
const defaultOptionsMap = /* @__PURE__ */ new Map();
function parseOptions(message) {
  if ((0, import_utils.isObject)(message)) {
    return message;
  }
  return {
    message
  };
}
const syncClear = () => {
  let fn = toastQueue.pop();
  while (fn) {
    fn();
    fn = toastQueue.pop();
  }
};
const nextTickClear = () => {
  setTimeout(syncClear);
};
const Toast = (toastProps) => {
  const props = parseOptions(toastProps);
  const instance = {
    config: () => {
    },
    clear: () => null
  };
  let timer = 0;
  const container = document.createElement("div");
  container.className = "toast-contanier";
  const bodyContainer = (0, import_getContainer.resolveContainer)(props.teleport);
  bodyContainer.appendChild(container);
  const TempToast = () => {
    const options = __spreadValues({
      duration: 2e3
    }, props);
    const [visible, setVisible] = (0, import_react.useState)(false);
    const [state, setState] = (0, import_react.useState)(__spreadValues({}, options));
    const unmountComponent = (0, import_react.useCallback)(() => {
      if (state.forbidClick) {
        (0, import_lock_click.lockClick)(false);
      }
      const unmountResult = import_react_dom.default.unmountComponentAtNode(container);
      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    }, [container]);
    const handleClose = (0, import_react.useCallback)(() => {
      var _a;
      setVisible(false);
      (_a = props.onClose) == null ? void 0 : _a.call(props);
    }, []);
    instance.clear = unmountComponent;
    instance.config = (0, import_react.useCallback)((nextState) => {
      setState((prev) => typeof nextState === "function" ? __spreadValues(__spreadValues({}, prev), nextState(prev)) : __spreadValues(__spreadValues({}, prev), nextState));
    }, [setState]);
    (0, import_react.useEffect)(() => {
      setVisible(true);
      if (!allowMultiple)
        syncClear();
      toastQueue.push(unmountComponent);
      if (state.duration !== 0 && "duration" in state) {
        timer = window.setTimeout(handleClose, +state.duration);
      }
      return () => {
        if (timer !== 0) {
          window.clearTimeout(timer);
        }
      };
    }, []);
    return /* @__PURE__ */ import_react.default.createElement(import_Toast.default, _extends({}, state, {
      visible,
      teleport: () => container,
      onClose: handleClose,
      onClosed: unmountComponent
    }));
  };
  import_react_dom.default.render(/* @__PURE__ */ import_react.default.createElement(TempToast, null), container);
  return instance;
};
const createMethod = (type) => (options) => {
  Toast((0, import_utils.extend)({}, currentOptions, defaultOptionsMap.get(type), parseOptions(options), {
    type
  }));
};
["info", "loading", "success", "fail"].forEach((method) => {
  Toast[method] = createMethod(method);
});
Toast.allowMultiple = (value = true) => {
  allowMultiple = value;
};
Toast.clear = nextTickClear;
const setDefaultOptions = (type, options) => {
  if (typeof type === "string") {
    defaultOptionsMap.set(type, options);
  } else {
    (0, import_utils.extend)(currentOptions, type);
  }
};
Toast.setDefaultOptions = setDefaultOptions;
Toast.resetDefaultOptions = (type) => {
  if (typeof type === "string") {
    defaultOptionsMap.delete(type);
  } else {
    currentOptions = (0, import_utils.extend)({}, defaultOptions);
    defaultOptionsMap.clear();
  }
};
var stdin_default = Toast;
