import { format, parse } from 'date-fns';
import { getDateFromValue } from '../../ui/components/data-entry/date-picker/utils.js';
import { getDateFormat, normalizeMonthFormat, parseInputString, } from '../../ui/components/data-entry/date-time-picker/utils.js';
export const castFunctions = {
    BigInt: (value) => BigInt(value),
    Number: (value) => Number(value),
    URLTrim: (value) => value?.trim(),
    AmountNumber: (value) => {
        if (typeof value === 'object' && 'unit' in value) {
            return {
                ...value,
                value: value.value !== undefined ? Number(value.value) : null,
                unit: value.unit !== '' ? value.unit : '',
            };
        }
        if (typeof value === 'object') {
            return {
                ...value,
                value: value.value !== undefined ? Number(value.value) : null,
            };
        }
        return value !== undefined ? Number(value) : null;
    },
    AmountBigInt: (value) => {
        if (typeof value === 'object' && 'unit' in value) {
            return {
                ...value,
                value: value.value !== undefined ? BigInt(value.value) : null,
                unit: value.unit !== '' ? value.unit : '',
            };
        }
    },
    DateString: (value, dateFormat = 'yyyy-MM-dd') => {
        const momentDate = getDateFromValue(value);
        const date = normalizeMonthFormat(momentDate);
        const correctFormat = getDateFormat(dateFormat);
        const newValue = parseInputString(date, correctFormat);
        const fechaUTC = parse(newValue, correctFormat, new Date());
        const isoDate = format(fechaUTC, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return isoDate;
    },
    DateTimeString: (value, dateFormat = 'yyyy-MM-dd') => {
        const [datePart, timePart] = value.split('T');
        const unFormattedDate = getDateFromValue(datePart);
        const date = normalizeMonthFormat(unFormattedDate);
        const correctFormat = getDateFormat(dateFormat);
        const normalizedDate = parseInputString(date, correctFormat);
        const parsedDate = parse(normalizedDate, correctFormat, new Date());
        const isoDate = format(parsedDate, 'yyyy-MM-dd');
        return `${isoDate}T${timePart}`;
    },
};
export function castValue(value, cast) {
    const [castType, ...params] = cast.split(':');
    return castFunctions[castType](value, ...params);
}
//# sourceMappingURL=value-cast.js.map