import { jsx as ce } from "../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { memo as le } from "../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/compat/dist/compat.module.js";
import { createContext as me } from "../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { useContext as ue, useState as t, useMemo as S, useRef as fe, useEffect as l, useCallback as d } from "../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { MenuItem as c, DEFAULT_MENU_ITEM as Se } from "./constants.js";
import { useStores as ve } from "../../../../../../hooks/useStores.js";
import { useStoreTheme as he } from "../../../hooks/useStoreTheme.js";
import { useStoreTermsAndConditions as ge } from "../../../hooks/useStoreTermsAndConditions.js";
import { useSaveAction as Ce } from "../../../hooks/useSaveAction.js";
import { useResponsiveContainer as Ie, containerQueries as Ee } from "../../../../../../hooks/useResponsiveContainer.js";
import { useSettingsPermission as Ae } from "../../../hooks/useSettingsPermission.js";
import { noop as n } from "../../../../../../utils/common.js";
const N = me({
  activeMenuItem: c.theme,
  payload: void 0,
  setPayload: n,
  selectedStore: void 0,
  setSelectedMenuItem: n,
  saveActionCalled: void 0,
  setIsValid: n,
  getIsValid: () => !1,
  setSaveActionCalled: n,
  filteredStores: void 0,
  allStores: void 0,
  setSelectedStore: n,
  savedData: void 0,
  setSavedData: () => {
  },
  menuItems: void 0,
  isLoadingContent: !0,
  isSaveError: void 0,
  isSaveSuccess: void 0,
  isSaving: void 0,
  isShowingRequirements: !1,
  onSave: n,
  setIsSaveError: n,
  setIsSaveSuccess: n,
  setIsShowingRequirements: n,
  isLoadingStores: !1,
  storesError: void 0,
  termsAndConditionsError: void 0,
  themeError: void 0
}), Fe = le(
  ({
    children: z,
    selectedMenuItems: B,
    storeIds: G,
    embeddedInOverview: H,
    navigateBack: J
  }) => {
    const [K, O] = t(!1), [r] = t(B), [x, i] = t(!1), v = Ie(Ee.down.xs), { themeEnabled: y, termsAndConditionsEnabled: D } = Ae(), h = S(() => {
      if (!(v && r.length > 1))
        return r.length > 0 && r[0] ? r[0].value : Se;
    }, [r, v]), [e, w] = t(null), [R, m] = t(void 0), [W, a] = t(void 0), g = fe(!1), [X, C] = t(!1), { filteredStores: I, selectedStore: o, setSelectedStore: E, isFetching: A, error: Y, allStores: Z } = ve(G), [$, k] = t(!1), [ee, p] = t(!1), [te, P] = t(!1);
    l(() => {
      h && w(h);
    }, [h]);
    const F = d(() => g.current, []), { onSave: se } = Ce(
      k,
      p,
      P,
      o,
      R,
      e,
      F,
      C,
      a,
      m,
      J
    ), [oe, q] = t(!1), [ne, V] = t(!1), Q = d(() => {
      k(!1), p(!1), P(!1), C(!1), a(void 0), m(void 0);
    }, []);
    l(() => {
      switch (Q(), e) {
        case c.theme:
          i(!0), o && q(!0);
          break;
        case c.termsAndConditions:
          i(!0), o && V(!0);
          break;
      }
    }, [e, o, Q]);
    const T = d(
      (s) => {
        s && m(s);
      },
      [m]
    ), {
      theme: U,
      isFetching: u,
      error: L
    } = he(o, oe, q, T, i), {
      data: _,
      isFetching: f,
      error: M
    } = ge(o, ne, V, T, i), b = S(() => {
      switch (e) {
        case c.theme:
          return u ? void 0 : U;
        case c.termsAndConditions:
          return f ? void 0 : _;
        default:
          return;
      }
    }, [e, U, _, u, f]);
    l(() => {
      b && a(b);
    }, [b, a]), l(() => {
      o || E(I?.[0]?.id);
    }, [I, o, E]);
    const re = d(
      (s) => {
        a(s);
      },
      [a]
    );
    l(() => {
      (L || M) && i(!1);
    }, [L, M]);
    const ie = d((s) => {
      g.current !== s && (g.current = s);
    }, []), ae = d(
      (s) => {
        e !== s.value && (i(!0), w(s.value));
      },
      [e]
    ), j = S(() => e ? e === c.theme ? y : D : !1, [e, D, y]), de = S(() => j ? x || u || f || A : !1, [x, u, f, A, j]);
    return /* @__PURE__ */ ce(
      N.Provider,
      {
        value: {
          isLoadingContent: de,
          setPayload: T,
          menuItems: r,
          payload: R,
          activeMenuItem: e,
          selectedStore: o,
          setSelectedMenuItem: ae,
          getIsValid: F,
          setIsValid: ie,
          saveActionCalled: X,
          setSaveActionCalled: C,
          filteredStores: I,
          setSelectedStore: E,
          savedData: W,
          setSavedData: re,
          isSaveError: ee,
          isSaveSuccess: te,
          isSaving: $,
          isShowingRequirements: K,
          setIsShowingRequirements: O,
          setIsSaveError: p,
          setIsSaveSuccess: P,
          onSave: se,
          allStores: Z,
          isLoadingStores: A,
          storesError: Y,
          embeddedInOverview: H,
          themeError: L,
          termsAndConditionsError: M
        },
        children: !e && !v ? null : z
      }
    );
  }
), qe = () => ue(N);
export {
  N as PaymentLinkSettingsContext,
  Fe as PaymentLinkSettingsProvider,
  qe as default,
  qe as usePaymentLinkSettingsContext
};
