import i, { Component as c, createRef as u, PureComponent as m } from "react";
import { r as a } from "../../../react-dom/index.mjs";
import { Calendar as h } from "../../core/index.mjs";
import { cy as l } from "../../core/internal-common.mjs";
const p = parseInt(String(i.version).split(".")[0]), g = p < 18;
class R extends c {
  constructor() {
    super(...arguments), this.elRef = u(), this.isUpdating = !1, this.isUnmounting = !1, this.state = {
      customRenderingMap: /* @__PURE__ */ new Map()
    }, this.requestResize = () => {
      this.isUnmounting || (this.cancelResize(), this.resizeId = requestAnimationFrame(() => {
        this.doResize();
      }));
    };
  }
  render() {
    const e = [];
    for (const t of this.state.customRenderingMap.values())
      e.push(i.createElement(f, { key: t.id, customRendering: t }));
    return i.createElement("div", { ref: this.elRef }, e);
  }
  componentDidMount() {
    const e = new l();
    this.handleCustomRendering = e.handle.bind(e), this.calendar = new h(this.elRef.current, Object.assign(Object.assign({}, this.props), { handleCustomRendering: this.handleCustomRendering })), this.calendar.render();
    let t;
    e.subscribe((s) => {
      const r = Date.now(), o = !t;
      // don't call flushSync if React version already does sync rendering by default
      // guards against fatal errors:
      // https://github.com/fullcalendar/fullcalendar/issues/7448
      (g || //
      o || this.isUpdating || this.isUnmounting || r - t < 100 ? d : a.flushSync)(() => {
        this.setState({ customRenderingMap: s }, () => {
          t = r, o ? this.doResize() : this.requestResize();
        });
      });
    });
  }
  componentDidUpdate() {
    this.isUpdating = !0, this.calendar.resetOptions(Object.assign(Object.assign({}, this.props), { handleCustomRendering: this.handleCustomRendering })), this.isUpdating = !1;
  }
  componentWillUnmount() {
    this.isUnmounting = !0, this.cancelResize(), this.calendar.destroy();
  }
  doResize() {
    this.calendar.updateSize();
  }
  cancelResize() {
    this.resizeId !== void 0 && (cancelAnimationFrame(this.resizeId), this.resizeId = void 0);
  }
  getApi() {
    return this.calendar;
  }
}
R.act = d;
class f extends m {
  render() {
    const { customRendering: e } = this.props, { generatorMeta: t } = e, s = typeof t == "function" ? t(e.renderProps) : t;
    return a.createPortal(s, e.containerEl);
  }
}
function d(n) {
  n();
}
export {
  R as default
};
