import { jsx as T } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { createContext as z } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { memo as G } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/compat/dist/compat.module.js";
import { useContext as H, useState as c, useCallback as a, useEffect as J } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import g from "../../../../../config/disputes/defenseDocumentConfig.json.js";
import x from "../../../../../config/disputes/defenseReasonConfig.json.js";
import K from "../../../../../core/Context/preact/useCoreContext.js";
const D = (f) => {
  const l = new FormData();
  for (const [r, o] of f.entries())
    o instanceof File ? l.set(r, o, o.name) : l.set(r, o);
  return l;
}, F = z(void 0), Z = G(({ dispute: f, setDispute: l, children: r }) => {
  const [o, d] = c("details"), [u, b] = c(null), [k, p] = c([]), [y, i] = c(null), [P, R] = c(null), C = a(() => {
    i((e) => {
      if (e) {
        const t = [...e.keys()].filter((n) => n !== "defenseReason");
        if (t.length > 0) {
          const n = D(e);
          return t.forEach((s) => n.delete(s)), n;
        }
      }
      return e;
    });
  }, []), S = a(() => {
    switch (o) {
      case "defendReasonSelectionView":
        d("details");
        break;
      case "accept":
        d("details");
        break;
      case "uploadDefenseFilesView":
        C(), d("defendReasonSelectionView");
        break;
      default:
        d("details");
        break;
    }
  }, [C, o]), h = a(() => {
    b(null), p(null), i(null), R(null), l(void 0);
  }, [l]), V = a((e, t) => {
    i((n) => {
      const s = n ? D(n) : new FormData();
      return s.set(e, t, t.name), s;
    });
  }, []), E = a((e, t) => {
    i((n) => {
      if (n && n.has(e)) {
        const s = n.get(e), m = D(n);
        return m.delete(e), s instanceof File ? m.set(t, s, s.name) : m.set(t, s), m;
      }
      return n;
    });
  }, []), j = a((e) => {
    i((t) => {
      if (t && t.has(e)) {
        const n = D(t);
        return n.delete(e), n;
      }
      return t;
    });
  }, []), q = a((e) => {
    R(e);
  }, []);
  J(() => {
    i(() => {
      if (u) {
        const e = new FormData();
        return e.set("defenseReason", u), e;
      }
      return null;
    });
  }, [u]);
  const { getCdnConfig: w } = K(), [A, B] = c(x), [I, M] = c(g), Q = a(async () => {
    const e = await w?.({
      subFolder: "disputes",
      name: "defenseReasonConfig",
      fallback: x
    }), t = await w?.({
      subFolder: "disputes",
      name: "defenseDocumentConfig",
      fallback: g
    });
    B(e ?? x), M(t ?? g);
  }, [w]);
  return /* @__PURE__ */ T(
    F.Provider,
    {
      value: {
        addFileToDefendPayload: V,
        applicableDocuments: k,
        clearFiles: C,
        clearStates: h,
        defendResponse: P,
        dispute: f,
        flowState: o,
        goBack: S,
        setApplicableDocuments: p,
        setFlowState: d,
        setDispute: l,
        selectedDefenseReason: u,
        setSelectedDefenseReason: b,
        defendDisputePayload: y,
        onDefendSubmit: q,
        moveFieldInDefendPayload: E,
        removeFieldFromDefendPayload: j,
        getDisputesConfig: Q,
        defenseReasonConfig: A,
        defenseDocumentConfig: I
      },
      children: r
    }
  );
}), _ = () => {
  const f = H(F);
  if (!f) throw new Error("useDisputeFlow must be used within DisputeFlowProvider");
  return f;
};
export {
  Z as DisputeContextProvider,
  F as DisputeFlowContext,
  _ as useDisputeFlow
};
