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

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