import hoistNonReactStatic from "hoist-non-react-statics";
import { parseAbsoluteToLocal, toZoned, parseDate } from "@internationalized/date";
import { forwardRef, useCallback, useMemo } from "@/import/react";
import {dateToISO} from "@/shared/helper/general/dateConverter";
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Element";
}
const convertValueIntoObject = (value, type) => {
if (!value) {
return null;
}
if (type === "date") {
return parseDate(value);
}
return parseAbsoluteToLocal(value);
}
const convertObjectIntoValue = (obj, type) => {
let parsedObj = obj;
if (parsedObj) {
if (type === "date") {
parsedObj = dateToISO(toZoned(parsedObj).toDate(), { toDate: true });
} else {
parsedObj = toZoned(parsedObj).toAbsoluteString();
}
}
return parsedObj;
}
/**
* Converts a date into YYYY-MM-DD
* @param Component
* @returns {function({readOnly: *, [p: string]: *}): *}
*/
function withDateConverter(Component) {
function WithDateConverter({ onChange, value, type, ...rest }, ref) {
const convertedValue = useMemo(() => {
return convertValueIntoObject(value, type);
}, [value, type]);
const convertOnChange = useCallback(
(obj, ...args) => {
const parsedObj = convertObjectIntoValue(obj, type);
onChange(parsedObj, ...args);
},
[onChange, type]
);
return (<Component {...rest} value={convertedValue} onChange={convertOnChange} ref={ref} />);
}
hoistNonReactStatic(WithDateConverter, Component);
WithDateConverter.displayName = `WithDateConverter(${getDisplayName(Component)})`;
return forwardRef(WithDateConverter);
}
export default withDateConverter;
export {
convertValueIntoObject,
convertObjectIntoValue,
}