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;
};
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 } from "../utils";
import { resolveContainer } from "../utils/dom/getContainer";
import { lockClick } from "./lock-click";
import BaseToast from "./Toast";
const defaultOptions = {
  icon: "",
  message: "",
  className: "",
  type: "info",
  position: "middle",
  forbidClick: false,
  duration: 2e3,
  teleport: () => document.body
};
const toastQueue = [];
let allowMultiple = false;
let currentOptions = extend({}, defaultOptions);
const defaultOptionsMap = /* @__PURE__ */ new Map();
function parseOptions(message) {
  if (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 = resolveContainer(props.teleport);
  bodyContainer.appendChild(container);
  const TempToast = () => {
    const options = __spreadValues({
      duration: 2e3
    }, props);
    const [visible, setVisible] = useState(false);
    const [state, setState] = useState(__spreadValues({}, options));
    const unmountComponent = useCallback(() => {
      if (state.forbidClick) {
        lockClick(false);
      }
      const unmountResult = ReactDOM.unmountComponentAtNode(container);
      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    }, [container]);
    const handleClose = useCallback(() => {
      var _a;
      setVisible(false);
      (_a = props.onClose) == null ? void 0 : _a.call(props);
    }, []);
    instance.clear = unmountComponent;
    instance.config = useCallback((nextState) => {
      setState((prev) => typeof nextState === "function" ? __spreadValues(__spreadValues({}, prev), nextState(prev)) : __spreadValues(__spreadValues({}, prev), nextState));
    }, [setState]);
    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__ */ React.createElement(BaseToast, _extends({}, state, {
      visible,
      teleport: () => container,
      onClose: handleClose,
      onClosed: unmountComponent
    }));
  };
  ReactDOM.render(/* @__PURE__ */ React.createElement(TempToast, null), container);
  return instance;
};
const createMethod = (type) => (options) => {
  Toast(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 {
    extend(currentOptions, type);
  }
};
Toast.setDefaultOptions = setDefaultOptions;
Toast.resetDefaultOptions = (type) => {
  if (typeof type === "string") {
    defaultOptionsMap.delete(type);
  } else {
    currentOptions = extend({}, defaultOptions);
    defaultOptionsMap.clear();
  }
};
var stdin_default = Toast;
export {
  stdin_default as default
};
