@adyen/adyen-platform-experience-web
Version:

34 lines (33 loc) • 1.52 kB
JavaScript
import { useMemo as h, useRef as a, useReducer as w, useEffect as P } from "../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import V from "../useMounted.js";
import { EMPTY_OBJECT as F } from "../../utils/value/constants.js";
const C = (n = F, f = !0) => {
const d = h(() => Object.freeze({ ...n }), [n]), u = a(f), t = a(d), p = a(new Set(Object.keys(d))), s = a(/* @__PURE__ */ new Set()), O = V(), [z, E] = w((r, c) => {
if (c === "reset")
return s.current.clear(), t.current;
const o = { ...c }, i = [0];
Object.keys(o).forEach((e, b) => {
if (!p.current.has(e)) return;
const M = r[e] ?? void 0, j = t.current[e] ?? void 0, S = o[e] ?? j;
if (S === M) return;
const m = Math.floor(b / 32), v = 1 << b % 32;
o[e] = S, i[m] = (i[m] ?? 0) | v, s.current[S === j ? "delete" : "add"](e);
});
const R = i.some((e) => e) ? u.current && s.current.size === 0 ? t.current : Object.freeze({ ...r, ...o }) : r;
return u.current || (t.current = R, u.current = !0), R;
}, d), [l, T] = h(() => {
const r = (c) => {
O.current && E(c);
};
return [
() => r("reset"),
(c) => r(c)
];
}, [O]), $ = h(() => !!s.current.size, []), g = t.current;
return P(() => {
t.current = Object.freeze({ ...n }), p.current = new Set(Object.keys(t.current)), u.current = f, l();
}, [f, n, l]), { canResetState: $, defaultState: g, resetState: l, state: z, updateState: T };
};
export {
C as default
};