var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellToast/squareToast/renderImperatively.tsx
var renderImperatively_exports = {};
__export(renderImperatively_exports, {
  render: () => render,
  renderImperatively: () => renderImperatively,
  renderToBody: () => renderToBody,
  unmount: () => unmount,
  unstableSetRender: () => unstableSetRender
});
module.exports = __toCommonJS(renderImperatively_exports);
var ReactDOM = __toESM(require("react-dom"));
var import_client = require("react-dom/client");
var import_react = __toESM(require("react"));
var MARK = "__pisell_react_root__";
function render(node, container) {
  const root = container[MARK] || (0, import_client.createRoot)(container);
  root.render(node);
  container[MARK] = root;
}
async function unmount(container) {
  return Promise.resolve().then(() => {
    var _a;
    (_a = container[MARK]) == null ? void 0 : _a.unmount();
    delete container[MARK];
  });
}
var defaultReactRender = (node, container) => {
  if (process.env.NODE_ENV !== "production") {
    const majorVersion = parseInt(import_react.default.version.split(".")[0], 10);
    const fullKeys = Object.keys(ReactDOM);
    console.warn(
      majorVersion < 19 || fullKeys.includes("createRoot"),
      `[Compatible] Toast support React is 16 ~ 18.`
    );
  }
  render(node, container);
  return () => {
    return unmount(container);
  };
};
var unstableRender = defaultReactRender;
function unstableSetRender(render2) {
  if (render2) {
    unstableRender = render2;
  }
  return unstableRender;
}
function renderToBody(element) {
  const container = document.createElement("div");
  document.body.appendChild(container);
  return unstableSetRender()(element, container);
}
function renderImperatively(element) {
  const Wrapper = import_react.default.forwardRef((_, ref) => {
    const [open, setOpen] = (0, import_react.useState)(false);
    const closedRef = (0, import_react.useRef)(false);
    const [elementToRender, setElementToRender] = (0, import_react.useState)(element);
    const keyRef = (0, import_react.useRef)(0);
    (0, import_react.useEffect)(() => {
      if (!closedRef.current) {
        setOpen(true);
      } else {
        afterClose();
      }
    }, []);
    function onCancel() {
      var _a, _b;
      closedRef.current = true;
      setOpen(false);
      (_b = (_a = elementToRender.props).onCancel) == null ? void 0 : _b.call(_a);
    }
    function afterClose() {
      var _a, _b;
      unmount2();
      (_b = (_a = elementToRender.props).afterClose) == null ? void 0 : _b.call(_a);
    }
    (0, import_react.useImperativeHandle)(ref, () => ({
      close: onCancel,
      replace: (element2) => {
        var _a, _b;
        keyRef.current++;
        (_b = (_a = elementToRender.props).afterClose) == null ? void 0 : _b.call(_a);
        setElementToRender(element2);
      }
    }));
    return import_react.default.cloneElement(elementToRender, {
      ...elementToRender.props,
      key: keyRef.current,
      open,
      onCancel,
      afterClose
    });
  });
  const wrapperRef = import_react.default.createRef();
  const unmount2 = renderToBody(/* @__PURE__ */ import_react.default.createElement(Wrapper, { ref: wrapperRef }));
  return {
    close: async () => {
      var _a, _b, _c;
      if (!wrapperRef.current) {
        unmount2();
        (_b = (_a = element.props).afterClose) == null ? void 0 : _b.call(_a);
      } else {
        (_c = wrapperRef.current) == null ? void 0 : _c.close();
      }
    },
    replace: (element2) => {
      var _a;
      (_a = wrapperRef.current) == null ? void 0 : _a.replace(element2);
    },
    isRendered: () => !!wrapperRef.current
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  render,
  renderImperatively,
  renderToBody,
  unmount,
  unstableSetRender
});
