var F = Object.defineProperty;
var N = (r, e, o) => e in r ? F(r, e, { enumerable: !0, configurable: !0, writable: !0, value: o }) : r[e] = o;
var c = (r, e, o) => N(r, typeof e != "symbol" ? e + "" : e, o);
import { jsx as n, jsxs as v } from "react/jsx-runtime";
import { mt2 as W } from "./index-general.module.js";
import { classNames as A } from "./index-classNames.js";
import { uuidv4 as R } from "./index-generate-uid.js";
import { pubSubService as I, PUB_SUB_EVENTS as T } from "./index-pub-sub.js";
import { deviceService as $ } from "./index-device.service.js";
import { PublicWidgetData as j } from "./index-public-widgets-data.store.js";
import { Modal as k } from "./index-Modal.js";
import { ModalHeader as U } from "./index-ModalHeader.js";
import { OverlayStackingService as C } from "./index-OverlayStackingService.js";
import { Render as y } from "./index-Render.js";
import { AcceptModal as V } from "./index-AcceptModal.js";
import { ErrorModal as D } from "./index-ErrorModal.js";
import { Text as x, FontWeights as E, TextTypes as G } from "./index-Text.js";
import L from "react";
import { PhotoCarouselModal as q } from "./index-PhotoCarouselModal.js";
import { ConfirmModal as z } from "./index-ConfirmModal.js";
import { LoadingModal as J } from "./index-LoadingModal.js";
const K = (r) => {
  const {
    close: e,
    children: o,
    title: l,
    headerType: t,
    message: i,
    hideHeaderCloseBtn: a,
    padding: s
  } = r;
  return v(k, {
    ...r,
    children: [v(U, {
      closeFn: e,
      hideCloseButtonOnMobile: !1,
      hideHeaderCloseBtn: a,
      type: t,
      children: [n(y, {
        if: !!l,
        children: n(x, {
          type: G.heading4,
          fontWeight: E.bold,
          children: l
        })
      }), n(y, {
        if: !!i && typeof i == "string",
        children: n("div", {
          className: A(W),
          children: n(x, {
            fontWeight: E.regular,
            children: i
          })
        })
      })]
    }), n("span", {
      style: {
        padding: s
      },
      children: o
    })]
  });
};
class Q {
  constructor() {
    c(this, "loadingModalId");
    c(this, "confirmModalId");
    c(this, "errorModalId");
    c(this, "modalCloseListeners");
    c(this, "modalComponents");
    c(this, "openErrorModal", ({
      err: e,
      source: o,
      message: l,
      title: t,
      ctaPositive: i,
      onClose: a
    }) => {
      this.errorModalId = this.open(n(D, {
        close: () => {
          a && a();
        },
        err: e,
        source: o,
        message: l,
        title: t,
        ctaPositive: i
      }));
    });
    this.loadingModalId = null, this.confirmModalId = null, this.errorModalId = null, this.modalCloseListeners = /* @__PURE__ */ new Map(), this.modalComponents = /* @__PURE__ */ new Map();
  }
  on(e, o) {
    document.addEventListener(e, (l) => o(l.detail));
  }
  onModalClose(e, o, l) {
    var t, i;
    this.modalCloseListeners.has(e) || this.modalCloseListeners.set(e, {
      cbs: [],
      data: l
    }), o && ((i = (t = this.modalCloseListeners.get(e)) == null ? void 0 : t.cbs) == null || i.push(o));
  }
  cleanModalClose(e) {
    this.modalCloseListeners.delete(e);
  }
  getModalComponent(e) {
    return this.modalComponents.get(e);
  }
  removeModalComponent(e) {
    this.modalComponents.delete(e), C.unregister(e);
  }
  openConfirmModal(e, o = "", l = "", t = "", i = "", a = "default", s = void 0, d = void 0, m, h, p) {
    let f;
    typeof e == "function" ? f = {
      closeFn: e,
      title: o,
      message: l,
      positiveCtaText: t,
      negativeCtaText: i,
      headerType: a,
      positiveIconLeft: s,
      replacements: d,
      hideHeaderCloseBtn: m,
      customNegativeFn: h,
      hideNegativeBtn: p
    } : f = e;
    const {
      closeFn: M,
      title: u = "",
      message: g = "",
      positiveCtaText: H = "",
      negativeCtaText: P = "",
      headerType: b = "default",
      positiveIconLeft: w,
      replacements: B,
      hideHeaderCloseBtn: S,
      customNegativeFn: _
    } = f;
    return this.confirmModalId = this.open(n(z, {
      close: M,
      message: g,
      title: u,
      positiveCtaText: H,
      positiveIconLeft: w,
      negativeCtaText: P,
      headerType: b,
      replacements: B,
      hideHeaderCloseBtn: S,
      customNegativeFn: _
    })), this.confirmModalId;
  }
  openAcceptModal(e, o = "", l = "", t = "default", i = "", a = void 0, s) {
    let d;
    typeof e == "function" ? d = {
      closeFn: e,
      title: o,
      message: l,
      headerType: t,
      positiveCtaText: i,
      replacements: a,
      hideHeaderCloseBtn: s
    } : d = e;
    const {
      closeFn: m,
      title: h = "",
      message: p = "",
      headerType: f = "default",
      positiveCtaText: M = "",
      replacements: u,
      hideHeaderCloseBtn: g
    } = d;
    return this.confirmModalId = this.open(n(V, {
      close: m,
      message: p,
      title: h,
      positiveCtaText: M,
      headerType: f,
      replacements: u,
      hideHeaderCloseBtn: g
    })), this.confirmModalId;
  }
  closeConfirmModal() {
    this.closeEv(this.confirmModalId), this.confirmModalId = null;
  }
  openLoadingModal(e, o, l, t) {
    let i;
    typeof e == "object" && e !== null ? i = e : i = {
      title: e,
      message: o,
      loadingAnimation: l,
      headerTitle: t
    };
    const {
      title: a,
      message: s,
      loadingAnimation: d = "default",
      headerTitle: m
    } = i;
    return this.loadingModalId = this.open(n(J, {
      title: a,
      message: s,
      loadingAnimation: d,
      headerTitle: m,
      close: () => {
      }
    }), {
      fullscreen: !1,
      maxWidth: "650px",
      modalPosition: "center",
      backgroundColor: "var(--n000)"
    }), this.loadingModalId;
  }
  closeLoadingModal() {
    this.closeEv(this.loadingModalId), this.loadingModalId = null;
  }
  openPhotoGalleryModal(e, o) {
    return this.open(n(q, {
      defaultMediaItem: o,
      gallery: e,
      close: () => {
      }
    }), {
      modalPosition: "center",
      maxWidth: "996px",
      width: "100%",
      wrapInModal: !1
    });
  }
  openSlide(e, o) {
    return o = {
      ...o,
      animation: "slide"
    }, this.open(e, o);
  }
  closeErrorModal() {
    this.closeEv(this.errorModalId), this.errorModalId = null;
  }
  closeAllModals() {
    this.modalComponents.forEach((e, o) => {
      this.closeEv(o);
    });
  }
  /** Test-only: wipe internal state. Not for production code paths. */
  __resetForTests() {
    this.loadingModalId = null, this.confirmModalId = null, this.errorModalId = null, this.modalCloseListeners.clear(), this.modalComponents.clear(), C.__resetForTests();
  }
  open(e, o) {
    let l = R();
    const t = j.getInstance(), {
      widgetId: i
    } = t.get();
    i && (l = `${l}_${i}`), $.isMobile() && (!o || !o.hasOwnProperty("fullscreen")) ? o = {
      fullscreen: !1,
      animation: "pop",
      ...o
    } : o = {
      fullscreen: !1,
      animation: "grow",
      ...o
    };
    const a = {
      ...e
    }, s = (m) => {
      a.props.close && a.props.close(m ?? {
        isOk: !1
      }), this.closeEv(l, m);
    };
    o != null && o.wrapInModal && (e = n(K, {
      close: s,
      padding: o.padding,
      headerType: o.type,
      title: o.title,
      message: o.message,
      children: L.isValidElement(e) ? L.cloneElement(e, {
        modalId: l,
        close: s
      }) : e
    }));
    const d = {
      ...e.props,
      close: s,
      ...o
    };
    return this.modalComponents.set(l, {
      ...e,
      props: {
        ...d
      },
      modalId: l
    }), C.register(l, "modal"), I.$pub(T.reactModalOpen, {
      modalId: l,
      animation: (o == null ? void 0 : o.animation) || "grow"
    }), l;
  }
  closeEv(e, o) {
    return e ? new Promise((l) => {
      setTimeout(() => {
        I.$pub(T.reactModalClose, {
          modalId: e
        });
        const t = this.modalCloseListeners.get(e);
        t && t.cbs.forEach((i) => i(o)), this.cleanModalClose(e), this.removeModalComponent(e), l(t == null ? void 0 : t.data);
      }, 0);
    }) : new Promise((l) => {
      l(void 0);
    });
  }
}
const Me = new Q();
export {
  Me as ModalService
};
