import hoistNonReactStatic from "hoist-non-react-statics";
import { forwardRef, useEffect } from "@/import/react";
import useCloneStore from "@/shared/hook/useCloneStore";
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "StoreElement";
}
/**
* @param Component
* @returns {function({readOnly: *, [p: string]: *}): *}
*/
function withCloneStore(Component) {
function WithCloneStore(
{
storeType,
storeId,
cloneStoreRef,
updateStore,
formStateStore$,
patchPaths$, // Ensure that these params from the original store are not sent any further.
...props
},
ref
) {
const defaultCloneStoreValues = useCloneStore(storeType, storeId);
useEffect(() => {
if (cloneStoreRef) {
cloneStoreRef.current = {
...defaultCloneStoreValues,
};
}
}, [cloneStoreRef, defaultCloneStoreValues]);
return <Component storeType={storeType} storeId={storeId} {...props} {...defaultCloneStoreValues} ref={ref} />;
}
hoistNonReactStatic(WithCloneStore, Component);
WithCloneStore.displayName = `WithCloneStore(${getDisplayName(Component)})`;
return forwardRef(WithCloneStore);
}
export default withCloneStore;