import { useState as j, useCallback as a, useMemo as c, useEffect as q } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { useFetch as l } from "../../../../../hooks/useFetch.js";
import { getFormSteps as J } from "../../utils.js";
import { useWizardForm as Q } from "../../../../../hooks/form/wizard/useWizardForm.js";
import W from "../../../../../core/Context/preact/useCoreContext.js";
import { useConfigContext as Y } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as f } from "../../../../../utils/value/constants.js";
const re = ({ storeIds: u, defaultValues: L, visibilityConfig: k }) => {
  const [t, B] = j(""), { i18n: n, getCdnDataset: y } = W(), {
    countries: p,
    getPayByLinkConfiguration: F,
    createPBLPaymentLink: A,
    getPayByLinkSettings: h,
    getPayByLinkStores: S
  } = Y().endpoints, { data: C, isFetching: P } = l({
    fetchOptions: { enabled: !!S },
    queryFn: a(async () => S?.(f, {}), [S])
  }), r = c(() => (C?.data ?? []).filter(({ storeId: s }) => u ? Array.isArray(u) && s ? u.includes(s) : u === s : !0).map(({ storeCode: s, storeId: $ }) => ({
    id: $ || "",
    name: s || ""
  })), [C, u]), {
    data: d,
    isFetching: E,
    error: x
  } = l({
    fetchOptions: { enabled: !!F && !!t },
    queryFn: a(async () => F?.(f, { path: { storeId: t } }), [F, t])
  }), {
    data: g,
    isFetching: v,
    error: R
  } = l({
    fetchOptions: { enabled: !!h && !!t },
    queryFn: a(async () => h?.(f, { path: { storeId: t } }), [h, t])
  }), b = c(() => !!g?.termsOfServiceUrl, [g?.termsOfServiceUrl]), i = a(
    (e) => d?.[e],
    [d]
  ), O = c(() => !!(i("deliveryAddress") || i("billingAddress") || i("countryCode")), [i]), { data: V, isFetching: T } = l({
    fetchOptions: { enabled: O && !!p },
    queryFn: a(async () => p?.(f), [p])
  }), { data: U, isFetching: w } = l({
    fetchOptions: { enabled: O },
    queryFn: a(async () => {
      const e = `${n.locale ?? "en-US"}`;
      return y ? await y({
        name: e,
        extension: "json",
        subFolder: "countries",
        fallback: []
      }) ?? [] : [];
    }, [y, n.locale])
  }), m = c(() => {
    const e = r.length === 1 && b;
    return J({ i18n: n, getFieldConfig: i, visibilityConfig: k }).filter((s) => !(s.id === "store" && e));
  }, [i, n, r, b, k]), z = c(() => m.map((e) => ({
    id: e.id,
    label: n.get(`payByLink.creation.form.steps.${e.id}`)
  })), [m, n]), M = c(() => n.get("payByLink.creation.steps.a11y.label"), [n]), o = Q({
    i18n: n,
    steps: m,
    defaultValues: {
      ...L,
      store: t || L?.store || ""
    },
    mode: "all",
    validateBeforeNext: !0
  });
  q(() => {
    r.length === 1 && (o.setValue("store", r[0]?.id), o.setFieldDisplayValue("store", r[0]?.name));
  }, [r, o]), q(() => o.control.subscribe(() => {
    const e = o.control.getValue("store");
    e && e !== t && B(e);
  }), [o.control, t]);
  const D = E || v || P, N = r.length === 1;
  return {
    // Query data
    storesData: C,
    configurationData: d,
    settingsData: g,
    // Store data
    selectedStore: t,
    storesSelectorItems: r,
    termsAndConditionsProvisioned: b,
    // Countries data
    countriesData: V,
    isFetchingCountries: T,
    // Countries dataset
    countryDatasetData: U,
    isFetchingCountryDataset: w,
    // Form steps
    formSteps: m,
    stepperItems: z,
    formStepsAriaLabel: M,
    // Wizard form
    wizardForm: o,
    // Endpoints
    createPaymentLink: A,
    // Loading state
    isDataLoading: D,
    isFirstLoadDone: !!(!P && (!N || !D && (d || x) && (!D && (g || R)))),
    setSelectedStore: B
  };
};
export {
  re as usePaymentLinkFormData
};
