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

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